views:

45

answers:

2

Hi guys,

I'm currently implementing some image processing code for Android. I'm aware of the memory limits and am happy to code within them. However, I cannot find any documentation that lets me work out how many bytes are used for each instance of a given class that I might want to instantiate (on the heap).

I'm an experienced C++ programmer and so am relatively competent at working out such struct/class sizing issues for my C++ code (taking into account processor data path width, platform alignment issues etc). I know that Java in general is at a higher level of abstraction and so that I may not be able to guarantee particular memory usage for a general Java VM. However given that android is running on a different VM, and given that developers are strongly memory constrained: I'm assuming that there may be a relatively deterministic set of rules for working out how big a given object instance will be, given knowledge of the members.

Anyone know these rules?

Thanks!

Alex

+1  A: 

Dalvikvm's memory overhead is consistent with other mainstream 32-bit VMs including HotSpot.

The base overhead for every object in the dalvikvm is two 32-bit words. To this you add two words for every long or double field, and one word each other field. Static fields don't count against this total.

There may be additional overhead if you exercise the identity hashCode by calling either an unoverridden Object.hashCode() or System.identityHashCode().

And there is additional memory overhead if you synchronize on the object.

Jesse Wilson
That is just what I need to know. Thank you very much.
Alex
A: 

As Jesse pointed out the layout is very similiar to the HotSpot VM. The rules for 32 bit Hotspot can be found here: http://kohlerm.blogspot.com/2008/12/how-much-memory-is-used-by-my-java.html

kohlerm