当Java对象的所有成员都为null时,它会使用多少内存?

编程入门 行业动态 更新时间:2024-10-23 05:48:10
本文介绍了当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时,它会使用多少内存?

本文发布于:2023-11-11 07:49:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577797.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:它会   都为   对象   内存   成员

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!