Reputation: 6844
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.
Upvotes: 14
Views: 43986
Reputation: 109
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.
Upvotes: 0
Reputation: 23787
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;
}
Upvotes: 3
Reputation: 56390
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.
Upvotes: 8
Reputation: 12056
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.
Upvotes: 29
Reputation: 20594
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.
Upvotes: 1
Reputation: 20442
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.
Upvotes: 1
Reputation: 103135
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.
Upvotes: 3
Reputation: 2001
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.
Upvotes: 0
Reputation: 533432
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.
Upvotes: 4
Reputation: 8204
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.
Upvotes: 3