2

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

4

2 回答 2

2

Dalvikvm 的内存开销与包括 HotSpot 在内的其他主流 32 位 VM 一致。

dalvikvm 中每个对象的基本开销是两个 32 位字。为此,您为每个长字段或双字段添加两个单词,并且每个字段添加一个单词。静态字段不计入此总数。

如果您通过调用未覆盖的Object.hashCode()System.identityHashCode()来使用身份 hashCode,则可能会产生额外的开销。

如果您在对象上进行同步,则会产生额外的内存开销。

于 2010-08-10T17:03:04.003 回答
0

正如 Jesse 指出的那样,布局与 HotSpot VM 非常相似。32 位热点的规则可以在这里找到:http: //kohlerm.blogspot.com/2008/12/how-much-memory-is-used-by-my-java.html

于 2010-08-19T08:00:37.153 回答