Channels ▼

Eric Bruno

Dr. Dobb's Bloggers

Increase Java Serialization Performance

July 30, 2013

Object Serialization Performance

Let's measure the performance of sending Java Objects over a network via Java IO and Object serialization. We'll do this in steps to compare.

—Object Serialization, No Buffered IO:

The first test will be to run this application without buffered IO, so the Listener code is modified slightly:

    ObjectInputStream ois =
        new ObjectInputStream( client.getInputStream() );

    Message msg = (Message)ois.readObject();

For the Sender:

    ObjectOutputStream oos = 
        new ObjectOutputStream( sender.getOutputStream() );

    Message msg = new Message();

The results on my computer, when sending and receiving 100,000 Objects this way is ~43,000 objects / second.

—Object Serialization, With Buffered IO:

Adding buffered IO (as shown in the code snippets earlier) the results improved dramatically to more than 100,000 objects / second.

Further Improvement?

This test shows that buffered IO helps improve Object serialization, but is that the best we can do? The answer is no: Object serialization over a network using this approach is not the most efficient method. There's a better way, which is to use a DataStream. With this approach, most of the code in this example remains the same, except we won't send the actual Java Object over the network. Instead, we'll send its data fields, as shown in the new Sender class run() method:

    public void run() {
        try {
            Socket sender = new Socket("localhost", 8081);
            if ( sender != null && sender.isConnected() ) {
                DataOutputStream dos = 
                    new DataOutputStream( 
                        new BufferedOutputStream( sender.getOutputStream() ));

                Message msg = new Message();
       = true;
                msg.userid = messages;
                msg.username = "User_" + messages;
       = this.toString();
                msg.type = Message.MESSAGE_TYPE_USER;

        catch ( Exception e ) {

The first change is to use a DataOutputStream object in place of ObjectOutputStream. Each field of the Object is written to the stream via calls that match their type; i.e., writeInt() for an Integer, writeUTF() for a String, and so on.

Corresponding changes must be made on the other side, within the Listener class:

    public void run() {
        // Wait for messages from client
        try {
            DataInputStream dis =
                new DataInputStream(
                    new BufferedInputStream(client.getInputStream()));

            Message msg = new Message();

            msg.type = dis.readInt();
   = dis.readBoolean();
            msg.username = dis.readUTF();
            msg.userid = dis.readInt();
   = dis.readUTF();
        catch ( Exception e ) {

Here, a DataInputStream is used, and the fields of the Message object are filled via matching calls to read from the stream. Since the bytes are sent in the order they're written, they must be read in the same order. Therefore, the fields must be read from the stream in the same order they were written to the stream. Let's see how these results affect performance.

Data Stream Performance

When using the DataOutputStream and DataInputStream approach to send Object data over a network, performance improves dramatically. The results on my computer, when sending and receiving 100,000 Objects this way is almost 300,000 objects / second. This is about a 3x improvement over the buffered IO example of Object serialization earlier! Although you lose a little flexibility, the trade-off with this approach is well worth it.


This example serves to illustrate the varying methods of sending data across Java Threads, and even Java applications over a network, using Java IO, along with the performance. It also shows how a little experimentation can lead to big performance gains.

Happy coding!

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.