为什么Point比Integer占用更少的内存

发布于 2024-11-05 21:08:55 字数 632 浏览 1 评论 0原文

我通过使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月静好 2024-11-12 21:08:56

当对象被序列化到流中时,您正在测量对象的大小。这与内存中对象的大小不同。

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.

瞄了个咪的 2024-11-12 21:08:55

首先,字符串 java.lang.Integerjava.lang.Numbervalue 出现在 Integer< 的序列化结果中/代码>。这应该提示为什么序列化大小与对象成员的相关性不是很好。

以下是列出的结果字节数组的一些内容: http://ideone.com/ragKZ

import java.awt.Point;
import java.io.*;

class Test {

    public static byte[] getBytes(Object object) throws IOException {
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
        objectStream.writeObject(object);
        objectStream.close();
        return byteStream.toByteArray(); 
    }

    public static void main(String[] args) throws IOException {

        byte[] iBytes = getBytes(new Integer(123123));
        System.out.println(new String(iBytes,  8, 17)); // "java.lang.Integer"
        System.out.println(new String(iBytes, 39,  5)); // "value"
        System.out.println(new String(iBytes, 48, 16)); // "java.lang.Number"
        // ...

        byte[] pBytes = getBytes(new Point(123, 123));
        System.out.println(new String(pBytes, 8, 14));  // "java.awt.Point"
        // ...
    }
}

To start with the strings java.lang.Integer, java.lang.Number and value appear in the serialization result for Integer. 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

import java.awt.Point;
import java.io.*;

class Test {

    public static byte[] getBytes(Object object) throws IOException {
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
        objectStream.writeObject(object);
        objectStream.close();
        return byteStream.toByteArray(); 
    }

    public static void main(String[] args) throws IOException {

        byte[] iBytes = getBytes(new Integer(123123));
        System.out.println(new String(iBytes,  8, 17)); // "java.lang.Integer"
        System.out.println(new String(iBytes, 39,  5)); // "value"
        System.out.println(new String(iBytes, 48, 16)); // "java.lang.Number"
        // ...

        byte[] pBytes = getBytes(new Point(123, 123));
        System.out.println(new String(pBytes, 8, 14));  // "java.awt.Point"
        // ...
    }
}
拥醉 2024-11-12 21:08:55

您查找对象大小的方法实际上根本没有测量它们在内存中占用了多少空间。

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 than Integer (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.

一杯敬自由 2024-11-12 21:08:55

Integer 较大,难以序列化,因为其父 Number 是可序列化的。如果打印对象序列化中的所有文本字节,您会得到

....sr..java.lang.Integer.......8...I..valuexr..java.lang.Number...........xp....

然而,Point 的父级不可序列化并且未序列化。

....sr..java.awt.Point...r4~.&...I..xI..yxp...{...{

此外,Integer 的字段名称较长(稍长)。

byte[] bytes = byteStream.toByteArray();
for(int i=0;i<bytes.length;i++) {
    char ch = (char) bytes[i];
    if (ch >= ' ' && ch < 127)
        System.out.print(ch);
    else
        System.out.print('.');
}
System.out.println();

您使用的方法查看对象序列化为 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 get

....sr..java.lang.Integer.......8...I..valuexr..java.lang.Number...........xp....

However, Point's parent is not Serializable and is not serialized.

....sr..java.awt.Point...r4~.&...I..xI..yxp...{...{

Also Integer's field name is longer (slightly)

byte[] bytes = byteStream.toByteArray();
for(int i=0;i<bytes.length;i++) {
    char ch = (char) bytes[i];
    if (ch >= ' ' && ch < 127)
        System.out.print(ch);
    else
        System.out.print('.');
}
System.out.println();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文