find the MEMORY that an Object uses using Guava library - guava

I want to find the MEMORY that an Object uses, using Guava library. For that I have made the homework of searching and I have found out one class: CollectionUtils. It has a method size(Object). But my question is whether this method returns the size of the object or size of the memory that it uses?

If you want to find out how much memory something is consuming, then you want memory-measurer.googlecode.com. But I don't think it's oriented toward beginners.

It's not Guava but Apache commons and it returns the number of elements. For memory usage there are tools based on instrumentation. Or you can use reflection and count it yourself. Or
allocate a lot of such objects and measure the memory used via Runtime.totalMemory() and Runtime.freeMemory().

Related

How to delete memory usage during an Experiment?

I am constructing an experiment in Anylogic, which saves data in the Parameter variation tab under a custom-class list. The model needs to perform a lot of simulations, and repetitions to optimize for Setting variables in the model itself. After x amount of iterations, I use a Python connector to run some code in finding new possible parameters for the underlaying model.
The problem I am having right now, is that around Simulation-run number 200, the memory usage is maximum (4Gb), and it proceeds to run super-slow. I have found some interesting ways to cut on memory usage, but I believe there is only one thing that could help me right now: let the system delete memory that is used for past iterations. After each iteration, the data of a simulation is stored, so I am fine with anylogic deleting the logs of the specific simulation afterwards.
Is such a thing possible? If so, how can I implement that?
Java makes use of a Garbage collector to manage memory usage and you have no control over it. How it works is that every now and then, based on some internal logic, it will collect and remove all instances of classes in memory that do not contain any active references and remove them.
Thus to reduce memory you must ensure that any instances that are no longer needed are not referenced by any of the objects currently active in your model.
To identify these you must use a Java profiler like JProfiler, or some of the free alternatives - see here for more.
This will show you exactly what classes are using up all your memory and with some deep diving you should be able to identify who is keeping reference to them.

Multi threaded writes to a MappedFile / MappedBytes

Is it possible in theory to write using Chornicle Bytes from multiple threads in different locations of the same memory mapped file (using a MappedBytes / MappedFile)?
What do I need to pay attention to? I sometimes get segmentation faults.
The main thing to pay attention to is how resource are freed.
This has be refactored and improved lately so I would take the latest version.
However, you can have concurrent access as Chronicle Map and Queue does this.
You might find using Chronicle Map (or Queue) is easier for your use case.

Is there a possibility to create a memory-efficient sequence of bits in the JVM?

I've got a piece of code that takes into account a given amount of features, where each feature is Boolean. I'm looking for the most efficient way to store a set of such features. My initial thought was to try and store these as a BitSet. But then, I realized that this implementation is meant to be used to store numbers in bit format rather than manipulate each bit, which is something I'd like to do (see the effect of switching any feature on and off). I then thought of using a Boolean array, but apparently the JVM uses much more memory for each Boolean element than the one bit it actually needs.
I'm therefore left with the question: What is the most efficient way to store a set of bits that I'd like to treat as independent bits rather than the building blocks of some number?
Please refer to this question: boolean[] vs. BitSet: Which is more efficient?
According to the answer of Peter Lawrey, boolean[] (not Boolean[]) is your way to go since its values can be manipulated and it takes only one byte of memory per bit to store. Consider that there is no way for a JVM application to store one bit in only one bit of memory and let it be directly (array-like) manipulated because it needs a pointer to find the address of the bit and the smallest addressable unit is a byte.
The site you referenced already states that the mutable BitSet is the same as the java.util.BitSet. There is nothing you can do in Java that you can't do in Scala. But since you are using Scala, you probably want a safe implementation which is probably meant to be even multithreaded. Mutable datatypes are not suitable for that. Therefore, I would simply use an immutable BitSet and accept the memory cost.
However, BitSets have their limits (deriving from the maximum number of int). If you need larger data sizes, you may use LongBitSets, which are basically Map<Long, BitSet>. If you need even more space, you may nest them in another map Map<Long, LongBitSet>, but in that case you need to use two or more identifiers (longs).

Why do Eclipse APIs use Arrays instead of Collections?

In the Eclipse APIs, the return and argument types are mostly arrays instead of collections. An example is the members method on IContainer, which returns IResources[].
I am interested in why this is the case. Maybe it is one of the following:
The APIs were designed before generics generics were available, so IResource[] was better than just Collection or List
Memory concerns, e.g. ArrayList internally holds an array which has more space than is needed (to offer an efficient implementation of add), whereas an array is always constructed for just the needed target size
It's not possible to add/remove elements on an array, so it is safe for iterating (but defensive copying is still necessary, because one can still change elements, e.g. set them to null)
Does anyone have any insights or other ideas why the API was developed that way?
Posting this as an answer, so it can be accepted.
Eclipse predates generics and they are really serious about API stability. Also, at the low level of SWT passing arrays seems to be used to reflect the operating system APIs that are being wrapped. Once you have a bunch of tooling using Arrays I guess it makes sense to keep things consistent. Also note that arrays aren't subject to all of the type erasure issues when using reflection.
Yeah, I hear you as far as the collections api being generally much easier to work with for dynamic lists of items.

Programmatically checking memory usage (Cocoa)

I'm trying to find a way of limiting the amount of memory a particular custom object is using, based on how much memory is left. The most useful way of doing this would be some kind of method or function that checks to see how much memory a given C Object is using. This way the program can reject the creation of further data when the object reaches its preset limit, rather than going ahead and creating it then dealing with the memory warning in retrospect.
Does anyone know of Cocoa methods that:
A) Return how much memory is being used by a given object
B) Return how much more memory the system can use up before it generates a warning
-Ash
A. You can get size of an object using malloc_size(myObject) (you need to traverse all the nested objects too). Also check docs for NSCache.
B. No, there is no documented way to do that