Skip to main content

Posts

Showing posts with the label java serialization

Comparing Java Default Serialization with Apache Avro serialization

I did a comparison of Java default serialization and Apache Avro serialization of data and results were very astonishing. You can read my older posts for Java serialization process and Apache Avro Serialization . Apache Avro consumed 15-20 times less memory to store the serialized data. I created a class with three fields (two String and one enum and serialized them with Avro and Java. The memory used by Avro is 14 bytes and Java used 231 bytes (length of byte[] ) Reason for generating less bytes by Avro Java Serialization The default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields. References to other objects (except in transient or static fields) cause those objects to be written also. Multiple references to a single object are encoded using a reference sharing mechanism so that graphs of objects can be restored to the same shape as when the original was written. Apac

Serialization in Java with example

What is Serialization? Serialization is the process of encoding an object to byte stream and reverse of it is called deserialization.It is platform independent process which means you can serialize the object in one JVM and can transport the object over network and / or store it in filesystem and then deserialize it in other or on same JVM. Class needs to implements marker interface Serializable in order to make their object to be eligible for Serialization. fields with transient and/or static modifiers are not serialized by regular serialization process. ObjectInputStream and ObjectOutputStream are high level stream classes which have methods to serialize and de-serialize Java objects. ObjectOutputStream class has many write methods but the method that is usually used method is: public final void writeObject ( Object obj ) throws IOException ObjectInputStream class has many read methods but the method that is usually used method is: public final Object read