views:

147

answers:

4

Likely a dumb question but..

Are there any good guidelines for how much memory basic Java data structures will consume?

Ie: How much memory will be consumed by a simple POJO with an int member and a String member (say that holds a 10 char String)? How much memory would be consumed by a List of 10 of said objects?

etc

A: 

I would say 144 bits for the string (32 for the pointer, 80 for the 10 characters, 32 for the length int), 32 bits for the int, and 32 for the pointer to the object, making 208 bits. A list of 10 would need 2080 bits for the objects and pointers to them, an additional 32 * 2 * 10 for the next and previous pointers, 32 for the length of the list, and 32 for the pointer. This makes 2784 bits total. And in my experience with Java vs what I think it will require (aka C), you apply a times two multiplier, making it 5568 bits.

At the end of the day if you are worried about memory requirements, test it and see! But it's quite hard to test for such small numbers... try a few thousand of them instead.

ZoFreX
On a 64bit VM pointers can be 64bit - and 10 characters are 160 bits, since a Java char is a 16bit type (that probably accounts for your experience compared to C).
Michael Borgwardt
Good points both of them :) The main areas I've benched Java are in data structures, where it was using Integer instead of int which I guessed was where the x2 came from in the case.
ZoFreX
Michael: It is however not mandated by the VM specification, that Strings are kept UTF16-encoded in memory. Optimizing for memory footprint instead of runtime efficiency, a String implementation may just as well keep the character data UTF8-encoded internally or use any other encoding.
jarnbjo
Well the API doc does say "A String represents a string in the UTF-16 format". And while would be possibly to satisfy the API while using UTF-8 internally, the effects on runtime efficiency would be pretty horrible (lots of O(1) methods become O(n) suddenly).
Michael Borgwardt
A: 

Gary Sevitsky of IBM Research has written several papers on analyzing and controlling memory bloat in Java programs. The more recent half of his publication list looks relevant to your inquiry.

I attended a tutorial of his at OOPSLA 2008 on this subject and found it a good introduction to this frightening, mostly avoided aspect of Java.

seh
+1  A: 

Strings are notoriously tricky as they hold on to a character array underneath which is shared with other String objects which are substrings of an original String object.

Thorbjørn Ravn Andersen