magallanes magallanes - 1 month ago 5
Java Question

What is the memory size of an ArrayList in Java

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.

Answer

You can use something like Runtime.getRuntime().totalMemory() and its counterpart Runtime.getRuntime().freeMemory() to get an educated guess, but that doesn't account for objects that are GC'ed between calls.