当Java对象的所有成员都为null时,它会使用多少内存? [英] How much memory does a Java object use when all its members are null?

查看:699
本文介绍了当Java对象的所有成员都为null时,它会使用多少内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设Java对象只占用对象引用的8个字节是正确的,只要它的所有成员都设置为 null 或成员的定义由于某种原因已经在实例中占用了空间?

Is it correct to assume a Java object only takes up the 8 bytes for the object reference as long as all it's members are set to null or does the definition of members already use up space in the instance for some reason?

换句话说,如果我有大量的对象我想要节省空间,我可以指望将未使用的成员设置为 null 以减少内存占用量?

In other words, if I have a large collection of objects that I want to be space efficient, can I count on leaving unused members set to null for reducing memory footprint?

推荐答案

否,对于存储在字段中的每个空值,您需要4个或8个字节(取决于它是32位还是64位系统)。如果某个地方没有存储的东西可以告诉它,那么该对象如何知道它的字段为空?

No, you need either 4 or 8 bytes ( depending whether it's a 32 or 64 bit system ) for each null you are storing in a field. How would the object know its field was null if there wasn't something stored somewhere to tell it so?

这篇关于当Java对象的所有成员都为null时,它会使用多少内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆