Search code examples
javaserializablestring-interning

Are Interned Strings preserved when serializing?


If I have large object graph that contains many duplicate strings, is there a benefit to intern()ing the strings before serializing them? Will this reduce the amount of data transferred? Will the strings share pointers on the receiving end?

My guess is that the Strings would be de-duped before sending, thus reducing the size of the data and that they would all be represented by the same object on the receiving end, but that they would not actually be interned on the receiving end. (meaning there would be one new instance of the string created on each serialization 'transaction')


Solution

  • ObjectOutputStream keeps track of object graph (until reset), one object is only written once, even if it's reached through multiple references. Reducing objects by interning will definitely reduce bytes.

    On the receiving end, the same object graph is recreated, so one string instance on the sending end becomes one string instance on the receiving end.