Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Huge number of int arrays during java deserialization

I see that there are a huge number of int array instances during Java deserialization using ObjectInputStream. I understand that this is due to creation of dependency tracking objects (java.io.ObjectInputStream$HandleTable$HandleList). As I understand, they are used to propagate ClassNotFoundException to "dependent" objects while reading an object.

I fail to understand the need to track this dependency of ClassNotFoundException over a target object? Why can't the implementation throw the exception right away, if the ClassNotFoundException is found?

There are significant bumps in transient memory usage during deserialization in one of the Java applications I work in. I have been able to attribute the spikes to the int[] instances after having taken a histogram using jmap.

I am trying to determine if terminating the deserialization process upon encountering the ClassNotFoundException should be ok. In this case, I would like to change the ObjectInputStream and use the modified class in bootstrap classpath.

I do realize that HandleTable's "entries" are absolutely required to wire any previously read objects into dependent objects read later from the stream. Just to clarify, the question has been about the significance of java.io.ObjectInputStream$HandleTable$HandleList, which I fail to understand.

Any insight is greatly appreciated.

Thanks,
Raja.

like image 769
Raja Reddy Avatar asked Nov 12 '22 18:11

Raja Reddy


1 Answers

It tracks objects for the purpose of resolving circular references and ensuring that two objects A and B that point to the same object C before serialization do not point to two separate instances of C after deserialization. Without tracking references, when the object hierarchy of A is deserialized it will create an instance of C, then then upon deserializing the B object hierarchy it would create another separate instance of C. It has little to do with ClassNotFoundException.

The best you can do is throw away ObjectInputStreams as soon as you are finished deserializing one "graph" of objects, rather than reusing it for many objects.

like image 122
brettw Avatar answered Nov 15 '22 04:11

brettw