Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java / JavaSE

OpenNxSerialization

5.00/5 (1 vote)
16 Jun 2009GPL32 min read 20.6K   177  
Speed up object serialization in Java.

Introduction

This is the Java flavor of NxSerialization for CLR (.NET and Mono) that is an easy to use object serialization framework that replaces the functionality of the default serialization providers. For background information, read this article http://www.codeproject.com/KB/dotnet/OpenNxSerialization.aspx.

Quick Facts

The figure below contains the benchmark results using the sample application shipped with NxSerialization for Java. The time measured was for 100 iterations of 100 runs each. In each run, an object of the specified type was serialized and then deserialized. These results may vary depending upon the system configuration; however, the important thing to consider is the relative difference or the performance factors between the native and the NxSerializer.

Size based comparison of .NET and NxSerialization formatters

Performance comparison of native and NxSerialization formatters

Why a Java version?

There were no plans to release a Java version of NxSerializatio, because unlike .NET and Mono, the native serializer for Java performs quite well. NxSerialization for Java was written primarily for two purposes. Firstly, as a proof of concept that even Java native serialization can be improved upon. Secondly, and more importantly, it serves as a launch pad for portable binary serialization; a project that is next in the pipeline.

However, since that project is still far from realization, and based upon popular demand, NxSerialization for Java has been released for review. Expectations are not very high from this release, but it would still be good to hear some suggestions and know that someone actually found it useful.

Using the Framework

Application objects can be integrated with the framework in two ways. By writing a surrogate for the object type and registering the surrogate with the framework, or by implementing INxSerializable. The framework provides a built-in surrogate for types that implement INxSerializable. For unknown types, native .NET serialization is used.

The following sample of code demonstrates a type that implements INxSerializable. Note the line at the bottom that registers the type with the framework.

Java
// Registering a type with the framework
NxFormatterServices services = NxFormatterServices.getDefault();
services.registerKnownType(SampleClass.class);

// Registering a custom surrogate with the framework
services.getSurrogateSelector().register(new SampleSurrogate());

// Using NxObjectOutputStream for serialization
ObjectOutput writer = NxObjectOutputStream(new ByteArrayOutputStream());
writer.writeObject(sampleGraph);
writer.flush();

// Using NxObjectInputStream for deserialization
byte[] contents = writer.getBaseStream().toByteArray();
ObjectInput reader = NxObjectInputStream(new ByteArrayInputStream(contents));
Object read = reader.readObject();

Everything else is pretty much self-explanatory. For more information, look at the sample benchmark application provided with the source code.

Comments

Please note that for objects where the actual data size to type-info size ratio is very large, not much memory reduction will occur. Try a byte array of size 100K. It is also possible to come up with a case where the native serializer is actually more efficient in terms of CPU.

History

OpenNxSerialization 1.0 (June 15, 2009)

  • Released the initial version of the framework.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)