假设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时,它会使用多少内存?
发布评论