0

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.)

5
  • what does profiler say when you profile your application? where's the performance bottleneck ? Commented Jun 25, 2014 at 10:35
  • I don't use an exact profiler. but when I calculate the time gape from top and bottom per one conversion(long beginig=System.currentTimeMillis(), long endTime=System.currentTimeMillis(), gap=endTime-beginig), and print the gap to console, the time gap is getting increased. Commented Jun 25, 2014 at 10:49
  • 1
    You are calling toByteArray() a bit early. Remove the last two lines, the o.flush(). And you could do new ByteArrayOutputStream(SIZE). Commented Jun 25, 2014 at 10:51
  • Could you show us your class definition? Commented Jun 25, 2014 at 11:04
  • There is no any specific class.. there are many.. How ever, classes have ArrayList, String, int .. Map.. Commented Jun 25, 2014 at 11:38

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.