Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does adding [Serializable] to the class have any performance implications?

I need to add the [Serializable] attribute to a class that is extremely performance sensitive.

Will this attribute have any performance implications on the operation of the class?

like image 265
AngryHacker Avatar asked Sep 10 '09 01:09

AngryHacker


People also ask

What is the purpose of serializable attribute?

Uses for serialization Serialization allows the developer to save the state of an object and re-create it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions such as: Sending the object to a remote application by using a web service.

Can serializable class inherited?

You must explicitly mark each derived class as [Serializable] . If, however, you mean the ISerializable interface, then yes: interface implementations are inherited, but you need to be careful - for example by using a virtual method so that derived classes can contribute their data to the serialization.

How do you prevent a variable from being serialized in a serializable class?

You can prevent member variables from being serialized by marking them with the NonSerialized attribute as follows. If possible, make an object that could contain security-sensitive data nonserializable. If the object must be serialized, apply the NonSerialized attribute to specific fields that store sensitive data.

Are interfaces serializable?

The Serializable interface is present in java.io package. It is a marker interface. A Marker Interface does not have any methods and fields. Thus classes implementing it do not have to implement any methods.


2 Answers

Instances of attribute classes are only created when they're first accessed. If you don't do any serialization on that particular class, the SerializableAttribute() constructor will never be called, hence it won't cause any performance issues.

Here's an interesting article about attribute constructors: http://www.codingonthetrain.com/2008/10/attribute-constructors.html

like image 94
Tamas Czinege Avatar answered Sep 22 '22 05:09

Tamas Czinege


Attributes are a metadata annotations so they don't add weight to a class at runtime, unless they are interpreted by the runtime in a certain way that makes it treat the class differently.

[Serializable] is simply a marker attribute used as a convention to indicate that the class is serializable, it has no effect and the runtime does not treat the classes in any special way.

like image 29
Pop Catalin Avatar answered Sep 20 '22 05:09

Pop Catalin