How to get amount of serialized bytes representing a Java object?

前端 未结 6 439
执笔经年
执笔经年 2021-02-01 20:45

What syntax would I use to get the number of bytes representing a string and compare them to the number of bytes representing an ArrayList holding that string, for

6条回答
  •  情深已故
    2021-02-01 21:17

    You can check the size of object after serialization process using Apache Commons as follows:

        // Create serialize objects.
        final List src = new ArrayList();
        src.add("awsome");
        src.add("stack");
        src.add("overflow");
    
        System.out.println(
                "Size after serialization:" + SerializationUtils.serialize((Serializable) src).length);
    

    Output :

    Size after serialization:86
    

提交回复
热议问题