为什么Point比Integer占用更少的内存
我通过使用 ByteArrayOutputStream 检查对象的字节数组的长度来测量对象的大小。 执行时:
System.out.println(Utils.getObjectSize(new Integer(123123)));
System.out.println(Utils.getObjectSize(new Point(123, 123)));
它们返回 81 和 51。
我相信 Point 由两个基元组成,但这似乎不是原因。
我的 Utils.getObjectSize 代码:
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
objectStream.writeObject(object);
objectStream.close();
return byteStream.toByteArray().length;
编辑:
我表达了自己的错误。我实际上想知道为什么他们在流中需要更大的尺寸。
I'm measuring the sizes of objects by checking how long their byte arrays are using ByteArrayOutputStream.
When doing:
System.out.println(Utils.getObjectSize(new Integer(123123)));
System.out.println(Utils.getObjectSize(new Point(123, 123)));
They return 81 and 51.
I belive Point consists of two primitives but that doesn't seem to be the reason.
The code I for Utils.getObjectSize:
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
objectStream.writeObject(object);
objectStream.close();
return byteStream.toByteArray().length;
EDIT:
I expressed myself wrong. I actually wanted to know why they take more size in the stream.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当对象被序列化到流中时,您正在测量对象的大小。这与内存中对象的大小不同。
You're measuring the size of the object when it's serialized to a stream. That won't be the same as the size of the object in memory.
首先,字符串
java.lang.Integer
、java.lang.Number
和value
出现在Integer< 的序列化结果中/代码>。这应该提示为什么序列化大小与对象成员的相关性不是很好。
以下是列出的结果字节数组的一些内容: http://ideone.com/ragKZ
To start with the strings
java.lang.Integer
,java.lang.Number
andvalue
appear in the serialization result forInteger
. That should give a hint about why the serialization size is not very well correlated with the members of the object.Here is some of the content of the resulting byte arrays listed: http://ideone.com/ragKZ
您查找对象大小的方法实际上根本没有测量它们在内存中占用了多少空间。
Point
实际上会比Integer
占用更多内存(对于 4 字节对齐的 JVM;对于 8 字节对齐,它们将是相同的) - 它只是序列化到更小的大小,可能是由于继承层次结构。Your approach for finding the size of objects doesn't actually measure how much space they take up in memory at all.
Point
will actually take more memory thanInteger
(for a 4-byte-aligned JVM; with 8 byte alignment they'll be the same) - it just happens to serialize to a smaller size, probably due to the inheritance hierarchy.Integer 较大,难以序列化,因为其父
Number
是可序列化的。如果打印对象序列化中的所有文本字节,您会得到然而,Point 的父级不可序列化并且未序列化。
此外,Integer 的字段名称较长(稍长)。
您使用的方法查看对象序列化为 byte[] 的大小,而不是内存中的大小。标准的 Java 序列化效率不是很高。
Integer 可能消耗 16 + 4 字节,Point 可能消耗 16 + 2 * 4 字节。然而,由于许多 JVM 在 8 字节边界上分配,您可能会发现它们消耗相同数量的内存。
Integer 序列化较大的原因是 Java 不仅序列化对象,还序列化其父对象,包括列出它们是什么。
Integer is larger to serialise because its parent
Number
is Serializable. If you print all the text bytes in the object serialisation you getHowever, Point's parent is not Serializable and is not serialized.
Also Integer's field name is longer (slightly)
The method you are using looks at how large is the Object serialisation as a byte[], not how large it is in memory. The standard Java serialisation is not very efficient.
Integer is likely to consume 16 + 4 bytes and Point is likely to consume 16 + 2 * 4 bytes. However as many JVMs allocate on a 8 byte boundary, you are likely to find they consume the same amount of memory.
The reason Integer is larger to serialize is that Java serializes not just the Object but its parents as well including listing what they are.