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?
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With