为什么Java序列化要占用这么多空间?

Mic*_*tte 3 java serialization

我尝试序列化 Byte 和 Integer 的实例,并惊讶于它们在另一端收到时占用了多少空间。为什么制作一个Integer只需要4个字节,而序列化时却占用了10倍以上的字节?我的意思是在 C++ 中,final 类有一个 64 位的类标识符,加上它的内容。按照这种逻辑,我希望整数在序列化时占用 64 + 32 或 96 位。

import java.io.*;

public class Test {
    public static void main (String[] ar) throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(bos);   
        out.writeObject(new Integer(32));
        byte[] yourBytes = bos.toByteArray();
        System.out.println("length: " + yourBytes.length + " bytes");
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

长度:81 字节

更新:

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

    {
    ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
    ObjectOutput out1 = new ObjectOutputStream(bos1);
    out1.writeObject(new Boolean(false));
    byte[] yourBytes = bos1.toByteArray();
    System.out.println("1 Boolean length: " + yourBytes.length);
    }

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = new ObjectOutputStream(bos);
    for (int i = 0; i < 1000; ++i) {
        out.writeObject(new Boolean(true)); // 47 bytes
    }
    byte[] yourBytes = bos.toByteArray();
    System.out.println("1000 Booleans length: " + yourBytes.length); // 7040 bytes

    final int count = 1000;

    ArrayList<Boolean> listBoolean = new ArrayList<>(count);
    listBoolean.addAll(Collections.nCopies(count, Boolean.TRUE));
    System.out.printf("ArrayList: %d%n", sizeOf(listBoolean)); // 5096 bytes

    Boolean[] arrayBoolean = new Boolean[count];
    Arrays.fill(arrayBoolean, true);
    System.out.printf("Boolean[]: %d%n", sizeOf(arrayBoolean)); // 5083 bytes

    boolean[] array = new boolean[count];
    Arrays.fill(array, true);
    System.out.printf("boolean[]: %d%n", sizeOf(array)); // 1027 bytes

    BitSet bits = new BitSet(count);
    bits.set(0, count);
    System.out.printf("BitSet: %d%n", sizeOf(bits)); // 201 bytes
}

static int sizeOf(Serializable obj) throws IOException {
    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    ObjectOutputStream objsOut = new ObjectOutputStream(bytesOut);
    objsOut.writeObject(obj);
    return bytesOut.toByteArray().length;
}
Run Code Online (Sandbox Code Playgroud)

输出:

1 布尔长度:47(每个布尔 47 个字节)

1000 个布尔值长度:7040(每个布尔值 7 个字节)

ArrayList:5096(每个布尔值 5 个字节)

Boolean[]: 5083(每个布尔值 5 个字节)

boolean[]: 1027(每个布尔值 1 个字节)

BitSet:201(每个布尔值 1 字节的 1/5)

Sat*_*ish 5

虽然 Radiodef 已经阐明了为什么序列化对象的大小很大,但我想在这里提出另一点,所以我们不要忘记底层 java 的序列化算法(几乎在所有算法中)中存在的优化。

在这种情况下,当您编写另一个 Integer 对象(或任何已写入的对象)时,您将看不到类似的大小(我的意思是大小不会是 81 * 2 = 162 字节),

ObjectOutput out = new ObjectOutputStream(bos);   
out.writeObject(new Integer(32));
out.writeObject(new Integer(65));
byte[] yourBytes = bos.toByteArray();
System.out.println("length: " + yourBytes.length + " bytes");
Run Code Online (Sandbox Code Playgroud)

它的工作方式是,当第一次请求类的实例(对象)进行序列化时,它会写入有关整个类的信息即包括类名,它写下类中存在的每个字段的名称。这就是为什么字节数更多的原因。这基本上是为了妥善处理班级评估案例。

当它第一次发送类的元数据时,它也会将相同的信息缓存到称为值缓存或间接表的本地缓存中。因此,下次当请求相同类的另一个实例进行序列化时(记住缓存仅适用于流级别,或在调用 reset() 之前),它只写入一个标记(仅 4 个字节的信息),以便大小会更少。