TXT

JAVA Serialization and performance

By Elaine Henry,2014-07-07 15:15
59 views 0
We all have done serialization in some way or another, mostly it's done without that we ever really 'know' that it happens. How can we control the java serialization and how can we improve its performance?

    We all have done serialization in some way or another, mostly it's done without that we ever really 'know' that it happens. A good example is in this case

    all sorts of software that use RMI (Remote Method Invocation).

    Most people realize the serialization process by giving the class the java.io.Serializable interface like this:

import java.io.*;

public class Pojo implements Serializable {

private static final long serialVersionUID = L;

....

}

    This is normally more then adequate for serialization, however the performance

    of doing serialization this way is poor. It has to use reflection not only

    to find out what the fields are to serialize but also to find out the types

    of the various fields. Reflection is as we know a very time consuming process.

    So the question is, what can we do to eliminate as much of this overhead as possible. There are a few ways, possible.

1. Using the ObjectStreamField class.

    2. Using the readObject / writeObject methods.

    3. Using the Externalizable interface.

    In the end the best performance is gained by using the Externalizable interface. But just to get the whole idea i??ll give an example of all the four

    different ways.

    We've already shown the serialization process by just using the Serializable

    interface. The only part I didn't mention is how you can exclude fields.

To exclude

    fields from the serialization process on can use the transient keyword.

    The ObjectStreamField class is used to tell the serialization mechanism more about the field and their types. This should save some time because it is

    not necessary anymore to retrieve the types of the different field using reflection. The transient keyword still works with this solution, so it is possible to create and ObjectStreamField class for all the fields and

    then exclude them using the transient keyword.

public class Pojo implements Serializable {

private static final long serialVersionUID = 1L;

private String valueA = "SomeTextA";

    private int valueB = 10;

    private float valueC = 100f;

    private double valueD = 100.100d;

    private short valueE = 10;

// Getters and setters go here.

    private static final ObjectStreamField[] serialPersistFields = { new ObjectStreamField("valueA", String.class),

    new ObjectStreamField("valueB", Integer.class),

    new ObjectStreamField("valueC", Float.class),

    new ObjectStreamField("valueD", Double.class),

    new ObjectStreamField("valueE", Short.class)

    };

    }

    As you can see it is not much that has to be done to achieve a bit more performance.

    The only 'problem' with this method is that you must specify all the fields you want to serialize, while with just using the Serializable interface all of the fields are included except for the

    fields that are marked as transient .

    The second way was by using the readObject / writeObject methods.

Report this document

For any questions or suggestions please email
cust-service@docsford.com