Android 中的类实例大小
我目前正在为 Android 实现一些图像处理代码。我知道内存限制,并且很乐意在其中编写代码。但是,我找不到任何文档可以让我计算出我可能想要实例化(在堆上)的给定类的每个实例使用了多少字节。
我是一名经验丰富的 C++ 程序员,因此在解决 C++ 代码的结构/类大小问题方面相对有能力(考虑处理器数据路径宽度、平台对齐问题等)。我知道 Java 通常处于较高的抽象级别,因此我可能无法保证一般 Java VM 的特定内存使用情况。然而,考虑到 android 运行在不同的虚拟机上,并且考虑到开发人员受到强烈的内存限制:我假设可能存在一组相对确定的规则来计算给定对象实例的大小,前提是了解成员。
有人知道这些规则吗?
谢谢!
亚历克斯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Dalvikvm的内存开销与包括HotSpot在内的其他主流32位VM一致。
dalvikvm 中每个对象的基本开销是两个 32 位字。为此,您可以为每个长字段或双字段添加两个单词,并为每个字段添加一个单词。静态字段不计入此总数。
如果您通过调用未覆盖的 Object.hashCode() 或 System.identityHashCode()。
如果在对象上进行同步,则会产生额外的内存开销。
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 指出的,布局与 HotSpot VM 非常相似。
32 位热点的规则可以在这里找到:
http://kohlem。 blogspot.com/2008/12/how-much-memory-is-used-by-my-java.html
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