Java中ArrayList的内存大小是多少

问题描述:

我有 ArrayList< Obj> ,我想知道它使用了多少内存。

I have an ArrayList<Obj> and I wish to know how much memory it is using.

Obj 是变体,所以它并不像根据对象的大小乘以数组中的元素数一样容易。

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.

您可以使用 Runtime.getRuntime()。totalMemory() 及其对应 Runtime.getRuntime()。freeMemory() 获得有根据的猜测,但这并不能解释在通话之间使用GC的对象。

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.