I have an ArrayList<Obj>
and I wish to know how much memory it is using.
The Obj
is variant so, it is not as easy as multiply the number of elements in the array per the size of an object.
I have an ArrayList<Obj>
and I wish to know how much memory it is using.
The Obj
is variant so, it is not as easy as multiply the number of elements in the array per the size of an object.
You can brute force calculate it if you know the content distribution of the objects.
However, the most precise method I can think of is to look at it in a profiler. There are free tools out there, some that come with the JDK. However, the best tool I've used is Yourkit. That doesn't mean it's the only solution, just my favorite.
The way an ArrayList works is that it simply contains an array of a certain size (which can be specified in the constructor, I believe 10 is the default?). Whenever the number of elements becomes too large for the internal array, the size of the internal array is doubled. So you need to multiply the size of the object by the size of the internal array.