开发者

What is the memory size of an ArrayList in Java

I h开发者_运维问答ave 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.


It's the capacity of the java.util.ArrayList multiplied by the reference size (4 bytes on 32bit, 8bytes on 64bit) + [Object header + one int and one references].

The capacity is different (always >=) than the size but you want to make 'em equal, call trimToSize()

Technically speaking the ArrayList has an Object[] where it stores the data.


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.


This is what a memory profiler is for. It will tell you for your platform. The minimum size for an empty ArrayList is 64-bytes. It is highly likely you don't need to know this unless you have 100K elements or more.


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.


You may have to use a profiler like the one available in Netbeans that will show you the memory consumption of our program and can give you some details about each object.


Source: http://uttesh.blogspot.com/2015/04/get-byte-or-memory-size-of.html

public static long getBytesFromList(List list) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baos);
    out.writeObject(list);
    out.close();
    return baos.toByteArray().length;
}


The answer is, it depends on how you measure. If you asked me the size of an ArrayList I would give you the shallow size. That is to say, an ArrayList consists of an array of references and an integer indicating the number of contained elements. If you wanted to know the size it is quite simply 4 + *array.length.

If you want to know the size of the ArrayList and all contained elements then there are some heap analyzers that can figure this out. I believe YourKit is one of them.


The memory usage of java depends of the JVM implementation. The only real method to determine the memory usage of an instance I know is to use an Java 5 instrumentation agent. There is a little tutorial to do so.


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.


Take heap dump, after adding and deleting elements from your ArrayList. Use any heap analyzer. (Eclipse MAT I prefer) You might see null values placed for deleted items. this makes ArrayList to hold memory in a heap even when there are only a few elements or no element. You can see the exact consumed memory size in the "Retained Heap" column.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜