Débora
Débora

Reputation: 5952

Java Efficient way to convert objects into byte array

I want to convert my pojo objects into byte arrays. All of my objects are serializable. I use

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        ObjectOutputStream o = new ObjectOutputStream(byteArrayOutputStream);
        o.writeObject(myobject);

        byte [] byteArray=byteArrayOutputStream.toByteArray();
        o.flush();
        o.close();
        byteArrayOutputStream.flush();
        byteArrayOutputStream.close();  

When objects are converting into byte array with many threads, this conversion takes time more and the time consumption per conversion(same objects into byte array) gets increased gradually. (For example, first thread conversion take 200ms, second thread gets 300ms for the same object conversion) . Can any one let me know why this object conversion time gradually increase and a better way to convert. ( I found a way using Externalization. But then my all objects have to be rewritten with Externalizable.)

Upvotes: 0

Views: 1160

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533930

Can any one let me know why this object conversion time gradually increase and a better way to convert.

ObjectOutputStream creates a lot of garbage and this put a strain on your L3 cache, main memory bus and GC. These are shared resources i.e. the work of one thread impacts all the other.

Note: this will be a problem for you whether you have 1 core or 12 cores on a socket.

I found a way using Externalization.

This may be the simplest solution, if you want to avoid using another serialization liibrary such as Kryo, thrift, protobuf etc. I have one which I claim to be the fastest, but it is definitively not the simplest. e.g. it converts straight from/to native memory and skips the byte[] entirely ;)

But then my all objects have to be rewritten with Externalizable.

You can start with just one class, i.e. the one which adds the highest load, there is no requirement to do them all.

Upvotes: 1

Related Questions