Would it take up far more space than simply writing a CSV file adapter that converts our List object to a CSV file?

It depends on the type of Row, and also on the size and other aspects of the data you are saving1.

On the one hand, the Java serialization protocol includes metadata for every class mentioned in the serialization. This takes significant space.

On the other hand:

  • Java serialization only includes the metadata once per serialization. So if you serialize lots of instances of the same class, the metadata cost becomes insignificant.
  • In a CSV file, all non textual data has to be converted to text. In some cases (e.g. large numbers, floating point numbers, booleans) the textual representation will be larger than the binary representation that is used in Java serialization.

1 - For example, an array of random numbers versus an array of zeros and ones. Java serialization will be better in the first case, and CSV will be better in the second.


But I think that you are probably focusing on the wrong thing here:

  • Unless the files you are generating are enormous, the size probably doesn't matter. Disk space is cheap.
  • The files are likely to be compressible in either case, with the less dense form probably being more compressible.
  • What matters more is whether the representation is suitable for purpose; e.g.
    • Do you want it to be human readable?
    • Do you want it to be readable by non-Java programs, including shell scripts?
    • Do you need to worry about changes to your Java code introducing class versus serialization version problems?
    • Do you want to be able to stream the data? (When writing or reading.)
Answer from Stephen C on Stack Overflow
🌐
Medium
iammadhankumar.medium.com › java-serialization-best-practices-fe7139023ea8
Java Serialization Best Practices | by Madhan Kumar | Medium
October 11, 2024 - Explore External Libraries: For more efficient and flexible serialization, consider using libraries like Google’s Protocol Buffers or Apache Avro. These tools can improve performance, reduce data size, and provide better compatibility across different systems, making them great alternatives to Java...
Top answer
1 of 2
4

Would it take up far more space than simply writing a CSV file adapter that converts our List object to a CSV file?

It depends on the type of Row, and also on the size and other aspects of the data you are saving1.

On the one hand, the Java serialization protocol includes metadata for every class mentioned in the serialization. This takes significant space.

On the other hand:

  • Java serialization only includes the metadata once per serialization. So if you serialize lots of instances of the same class, the metadata cost becomes insignificant.
  • In a CSV file, all non textual data has to be converted to text. In some cases (e.g. large numbers, floating point numbers, booleans) the textual representation will be larger than the binary representation that is used in Java serialization.

1 - For example, an array of random numbers versus an array of zeros and ones. Java serialization will be better in the first case, and CSV will be better in the second.


But I think that you are probably focusing on the wrong thing here:

  • Unless the files you are generating are enormous, the size probably doesn't matter. Disk space is cheap.
  • The files are likely to be compressible in either case, with the less dense form probably being more compressible.
  • What matters more is whether the representation is suitable for purpose; e.g.
    • Do you want it to be human readable?
    • Do you want it to be readable by non-Java programs, including shell scripts?
    • Do you need to worry about changes to your Java code introducing class versus serialization version problems?
    • Do you want to be able to stream the data? (When writing or reading.)
2 of 2
3

Java serialization will be less space efficient in certain cases than simply writing to a CSV file because it stores extra metadata to identify class-types.

I verified such a scenario with two simple test programs. The first one writes an array of ints to a .csv file.

import java.io.*;

public class CSVDemo {
  public static void main(String [] args) {
    try {
       PrintWriter pw = new PrintWriter(new File("dummy.csv"));
       StringBuilder sb = new StringBuilder();
       for(int i = 0; i < 1000; i++){
         sb.append(1);
         sb.append(",");
       }

       pw.write(sb.toString());
       pw.close();
       System.out.printf("Data is saved in dummy.csv");
    } catch(FileNotFoundException e) {
        e.printStackTrace();
    }
  }
}

The second one serializes an object containing an array of ints to a .ser file.

import java.io.*;

public class SerializeDemo
{
   public static void main(String [] args)
   {
      DummyData dummy = new DummyData();

      try {
         FileOutputStream fileOut = new FileOutputStream("dummy.ser");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.writeObject(dummy);
         out.close();
         fileOut.close();
         System.out.printf("Serialized data is saved in dummy.ser");
      } catch(IOException i) {
          i.printStackTrace();
      }
   }

   public static class DummyData implements java.io.Serializable{
     int[] data = new int[1000];
     public DummyData(){
       for(int i = 0; i < 1000; i++){
         data[i] = 1;
       }
     }
   }
}

The .ser file took 4079 bytes. The .csv file took 2000 bytes. Granted, this is a slight simplification of your use case (I'm equating an int to your Row type), but the general trend should be the same.

Trying with larger numbers yields the same result. Using 100000 ints results in ~400KB for .ser and 200KB for .csv

However, as the below comment pointed out, if choosing random values for ints, the .csv actually grows larger.

🌐
DZone
dzone.com › coding › languages › did you know the fastest way of serializing a java field is not serializing it at all?
Fastest Way of Serializing Java Field
January 20, 2022 - Chronicle Queue can work with Serializable objects but also provides a similar, but faster and more space-efficient way to serialize data via the abstract class SelfDescribingMarshallable.
🌐
Baeldung
baeldung.com › home › java › core java › different serialization approaches for java
Different Serialization Approaches for Java | Baeldung
1 month ago - These protocols provide flexibility to use any one of several serialization formats for data exchange. Some examples of supported protocols include: TBinaryProtocol uses a binary format and hence faster to process than the text protocol · TCompactProtocol is a more compact binary format and, therefore, more efficient to process as well
🌐
IEEE Xplore
ieeexplore.ieee.org › document › 776421
Efficient object serialization in Java | IEEE Conference Publication | IEEE Xplore
Object serialization is the ability of an object to write a complete state of itself and of any objects that it references to an output stream, so that it can be recreated from the serialized representation at a later time.
🌐
Ahmed DAMMAK
ahdak.github.io › blog › effective-java-part-11
Effective Java - 3rd Edition - Serialization – Ahmed DAMMAK
The default serialized form of an object is a reasonably efficient encoding, describing data contained in the object and in every reached object, and the topology by which all of this objects are interlinked.
🌐
DEV Community
dev.to › activej › activeserializer-ultra-fast-java-serialization-library-582g
ActiveSerializer. Ultra-fast Java Serialization Library - DEV Community
November 5, 2020 - ActiveSerializer is a modern bytecode generator of extremely fast and space-efficient serializers for data transferring. It's core features are ultimate performance and streamlined programming model.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java Best Practices – High performance Serialization - Java Code Geeks
October 21, 2012 - You show a some useful techniques to use the ObjectOutput and ObjectInput APIs to get the best out of the JDK serialization algorithm. Having tried fast-serialization myself I can confirm it is indeed very fast. Great piece of code. Application servers and other JavaEE technologies do not always allow the use of an alternative serialization mechanism.
Find elsewhere
Author: RuedigerMoeller
🌐
OpenJDK
openjdk.org › projects › amber › design-notes › towards-better-serialization
Towards Better Serialization
This means it is easy to sneak bad data into a serializable class (unless you’ve (painfully) duplicated your argument checking between constructors and readObject(), in which case you have lost the biggest benefit of Java’s serialization mechanism: that it supposedly comes for free).
🌐
ADHDecode
adhdecode.com › home › performance engineering › java/jvm performance › java serialization performance
Java Serialization Performance — How It Works | ADHDecode
March 19, 2026 - Then there are the optimized serialization libraries. These are the pros. Protobuf, Avro, Kryo. They all tackle the problem differently, but the core idea is to reduce metadata and use more efficient binary encoding.
🌐
Medium
medium.com › @AlexanderObregon › a-deep-dive-into-java-serialization-e514346ac2b2
Java Serialization Full Deep Dive | Medium
November 6, 2023 - Kryo: Kryo is a fast and efficient object graph serialization framework for Java.
🌐
DEV Community
dev.to › adaumircosta › java-serialization-best-practices-49m1
Java Serialization Best Practices - DEV Community
August 7, 2024 - Serialization in Java is the process of converting an object's state into a byte stream, which can then be reverted back into a copy of the object. While Java provides built-in serialization mechanisms, it's important to follow best practices to ensure efficiency, security, and compatibility.
🌐
Foojay
foojay.io › home › devops › why the fastest way of serializing a java field is not serializing it at all
Why the Fastest Way of Serializing a Java Field is not Serializing it at All | Foojay.io
October 25, 2022 - Chronicle Queue can work with Serializable objects but also provides a similar but faster and more space-efficient way to serialize data via the abstract class SelfDescribingMarshallable .
🌐
Java Code Geeks
javacodegeeks.com › home › enterprise java
Optimizing Java Serialization - Java vs XML vs JSON vs Kryo vs POF
September 11, 2013 - It is possible to optimize Java serialization through implementing the Externalizable interface. Implementing this interface avoids writing out the entire class definition, just the class name is written.
🌐
Foojay
foojay.io › home › high-performance java serialisation to different formats
High-Performance Java Serialisation to Different Formats
December 7, 2022 - It has reduced complexity and uses tree-like structures which makes it very efficient. Moreover, it can be done in a lot of different formats with no need to change the coding. This article covers the basics of serialization and discusses some ...
🌐
DZone
dzone.com › coding › java › avoiding java serialization to increase performance
Avoiding Java Serialization to increase performance
August 20, 2011 - The main problem with Java Serialization is performance and efficiency. Java serialization is much slower than using in memory stores and tends to significantly expand the size of the object.
🌐
O'Reilly
oreilly.com › library › view › java-performance-tuning › 0596000154 › ch08s04.html
Serialization - Java Performance Tuning [Book]
September 1, 2000 - You can consider implementing readObject() and writeObject() (see java.io.Serializable documentation) to override the default serialization routine; it may be that you can produce a faster serialization routine for your specific objects.
Author: Jack Shirazi
Published: 2000
Pages: 442