Skip to content
Advertisement

When you serialized and deserialized an Object where is saved the status of it? [closed]

I know that the objects in Java live on the Heap, but when I start to serialize the object and after to deserialize the status of it is keeps on heap? Or in different place?

Edit:
I mean, how the JVM can recreate a new object on the heap? Just reading the file?

I wanted to know what’s happens on the heap, before and after, the JVM destroys an object and after it comes back? To do this action, there is something in memory stored or all the information are on the file serialized and deserialized?

Answer

When you serialise an object (I suppose you are using ObjectIn/OutputStream), you are turning a Java object into binary data in a stream. This is stream can go to anywhere. It can go to a web server or a file or something else. Serializing an object to a file is the most common, in my experience.

After the object is serialised, it is stored on your hard disc/SSD. However, there is also an independent copy of your object in memory, until your program finishes. It can’t just disappear from memory because you might want to use it again.

Advertisement