Penalty to implement Serializable in Java?
Asked Answered
R

5

39

Is there a penalty to add

implements Serializable

to a Java class? Impact on size of instantiated object or performance?

Rancor answered 3/5, 2012 at 9:30 Comment(2)
javalobby.org/java/forums/t88856.htmlVyner
@Ajj You're cool dude! You're a genious!Pussy
F
27

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.
Fluellen answered 3/5, 2012 at 9:37 Comment(0)
M
30

There is no performance impact unless you perform serialization/deserialization but there are trade offs in terms of api design.

From Effective java by Joshua Bloch

  • A major cost of implementing Serializable is that it decreases the flexibility to change a class’s implementation once it has been released
  • A second cost of implementing Serializable is that it increases the likelihood of bugs and security holes
  • A third cost of implementing Serializable is that it increases the testing burden associated with releasing a new version of a class

Upto what extent these are applicable to you depend of your usecase.

Manned answered 3/5, 2012 at 9:39 Comment(3)
There is no performance impact even if you do perform serialization/deserialization, because if you don't implement Serializable you can't do that at all so there is nothing to compare it with.Porche
Thanks for Joshua-quote. I agree.Rancor
I would honestly not implement Serializable without first reading that Effective Java book.Puca
F
27

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.
Fluellen answered 3/5, 2012 at 9:37 Comment(0)
E
24

Unless you / someone else actually serializes / deserializes, there won't be any impact. Serializable is just a marker interface.

It probably will only increase the size of the generated bytecode by a few bytes.

Egotism answered 3/5, 2012 at 9:32 Comment(0)
B
2

You should always consider the maintenance overhead first. The cost of a deployed application over its live can be several times the cost of developing it.

In this case, the cost of making a class Serializable, but not actually used for Serialization could cause bugs or confusion far grater than the performance cost. e.g. say it takes 1 minute or someone's time to determine the Serializable is not needed, this can cost far more than the few nano-second extra startup time the application incurs.

If you actually need it to be Serializable, you can't compare it to the non-Serializable version because only the first case will actually do the job required.

Bochum answered 3/5, 2012 at 10:45 Comment(0)
A
0

Serializable is just a "marker" interface. It does not require you to implement any methods.

The only thing is that it is recommended to have a static final long serialVersionUID to help the serialization API. That costs a single long value per serialized class.

You might want to check out this article about Java Serialization API. It describes most of the caveats of serialization / deserialization in detail.

Ashleaashlee answered 3/5, 2012 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.