Backend Development 6 min read

Java Serialization vs Binary Encoding: Size and Performance Comparison

The article examines Java's built‑in Serializable interface, highlights its cross‑language incompatibility, large payload size, and low efficiency, then presents benchmark code comparing it with a custom binary encoding, showing significant reductions in data size and processing time, and recommends modern frameworks such as Protobuf or Thrift.

Top Architect
Top Architect
Top Architect
Java Serialization vs Binary Encoding: Size and Performance Comparison

In this post the author, a senior architect, introduces Java's built‑in serialization mechanism via the Serializable interface and explains that using it only requires implementing the interface and then using object streams for serialization and deserialization.

The author points out three major drawbacks of Java serialization: it cannot be used across different programming languages, the serialized byte stream is relatively large, and the serialization speed is low.

To illustrate the size issue, a simple Message class is serialized using Java's default mechanism and then using a manual binary encoding. The following test code demonstrates the comparison:

@Test
public void testSerializable(){
String str = "哈哈,我是一条消息";
Message msg = new Message((byte)0xAD,35,str);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(msg);
os.flush();
byte[] b = out.toByteArray();
System.out.println("jdk序列化后的长度: " + b.length);
os.close();
out.close();
ByteBuffer buffer = ByteBuffer.allocate(1024);
byte[] bt = msg.getMsgBody().getBytes();
buffer.put(msg.getType());
buffer.putInt(msg.getLength());
buffer.put(bt);
buffer.flip();
byte[] result = new byte[buffer.remaining()];
buffer.get(result);
System.out.println("使用二进制序列化的长度:" + result.length);
} catch (IOException e) {
e.printStackTrace();
}
}

The output shows that the binary encoding produces a much smaller byte array than Java's default serialization.

A second benchmark measures the time cost of performing 100,000 serializations with each method. The test code is:

@Test
public void testSerializable(){
String str = "哈哈,我是一条消息";
Message msg = new Message((byte)0xAD,35,str);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
long startTime = System.currentTimeMillis();
for(int i = 0; i < 100000; i++) {
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(msg);
os.flush();
byte[] b = out.toByteArray();
os.close();
out.close();
}
long endTime = System.currentTimeMillis();
System.out.println("jdk序列化100000次耗时:" + (endTime - startTime));
long startTime1 = System.currentTimeMillis();
for(int i = 0; i < 100000; i++) {
ByteBuffer buffer = ByteBuffer.allocate(1024);
byte[] bt = msg.getMsgBody().getBytes();
buffer.put(msg.getType());
buffer.putInt(msg.getLength());
buffer.put(bt);
buffer.flip();
byte[] result = new byte[buffer.remaining()];
buffer.get(result);
}
long endTime1 = System.currentTimeMillis();
System.out.println("使用二进制序列化100000次耗时:" + (endTime1 - startTime1));
} catch (IOException e) {
e.printStackTrace();
}
}

The timing results demonstrate that binary encoding is considerably faster than Java's built‑in serialization.

Based on these findings, the author concludes that Java's default serialization is unsuitable for high‑performance scenarios and recommends using mature serialization frameworks such as Google Protobuf or Apache Thrift, which provide cross‑language support, compact payloads, and high efficiency.

JavaperformanceSerializationProtobufBinary EncodingThrift
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.