Why is Java deserialization related to CPU?

I have a Java program that prepares data into a rather complex and large data structure in memory (several GB) and serializes it to disk, and another program that reads the serialized data structure in memory. I was surprised to notice that the deserialization step is rather slow and that it is related to the CPU. (100% CPU usage in top, but only 3 to 5 MB / s, reading from iotop, which is very small for what should be sequential reading on the hard drive). The processor is quite recent (Core i7-3820), the structure fits into the memory, the swap space is not configured.

Why is this so? Is there an alternative way to serialize objects in Java that does not have a CPU as a bottleneck?

Here is the deserialization code, if that matters:

FileInputStream f = new FileInputStream(path);
ObjectInputStream of = new ObjectInputStream(f);
Object obj = of.readObject();
+5
source share
2 answers

Desicialization is quite expensive. If you use general deserialization, it will use a lot of reflection and creation of objects.

There are many alternatives that use the generated code faster and more often than the reflection.

http://code.google.com/p/thrift-protobuf-compare/wiki/Benchmarking

You will notice that one of the fastest is to use Externalizable, which may be an option for you. This means adding custom methods to serialize and deserialize objects.

I wrote very fast approaches, but this does not allow creating any objects, processing them or using data in a file in place (i.e. without deserializing them)

+4

, , , " " " " , , .

, Java Reflection. , , , , . , "" , Reflection .

, ( ), - Serializable:

private static final long serialVersionUID = [some number]L;

, Java , , .

:

http://oreilly.com/catalog/javarmi/chapter/ch10.html

+2

All Articles