Penalty to implement Serializable in Java?
java, performance, serializable
Solution
The cost is close to zero, not worth being concerned about.
Some further details:
- There is no increase in the size of each object instance
- There is a small increase in the size of the class itself, but as this is a one-off cost it is trivial when amortised over a large number of instances
- There may be a slight additional runtime cost for anything that needs to do interface checks at runtime (reflection, instancof lookups, extra pressure on inline caches etc.). Again, this is likely to be negligible for most purposes.
- Serializable is a marker interface, there are no methods that require to be implemented. Other marker interface examples are: Clonable, SingleThreadModel, Event listener.
Problem
Is there a penalty to add ``` implements Serializable ``` to a Java class? Impact on size of instantiated object or performance?