Why does ArrayList use transient storage?
Why does this need to be transient?
It does this because it provides custom readObject
and writeObject
methods that do a better job of serialization than the default. Specifically, the writeObject method writes just the size and the sequence of elements. This avoids serializing the private array object which 1) has its own header and overheads, and 2) is typically padded with null
s. The space saving can be significant.
Why can't this class be serialized?
The ArrayList
class as a whole can be serialized1. The Object[]
could be serialized directly, but they chose to mark it as transient
implement the serialization another way.
1 - Actually, this depends on the elements' runtime types. For example, if you attempted to serialize an ArrayList
containing Thread
references, then you would get a runtime exception for the first non-null reference.
It can be serialized; the ArrayList
class just takes care of things itself, rather than using the default mechanism. Look at the writeObject()
and readObject()
methods in that class, which are part of the standard serialization mechanism.
If you look at the source, you see that writeObject()
does not save the backing array. Instead, it serializes the elements (including null values) one at a time up to the size()
limit. This avoids the overheads of serializing the array, and especially any unused slots at the end of the array. On deserialization, a new backing array of the minimum required size is created by readObject()
.
ArrayList
implements Serializable
, so it can be serialized, that's exactly why the private backing array is transient
, so it is not serialized along with other data in the class, since all is handled by ArrayList
's writeObject
and readObject
methods.