Object Resurrection in Java

Please go through Garbage Collector(GC) before diving into the below discussion.

An object becomes garbage when it is no longer reachable from the program, and may be collected (destroyed and deallocated). Then, during object destruction, before the garbage collector deallocates the object, the finalize method may be run, which may in turn make that the garbage object reachable again by creating references to it.

If a resurrected object is later de-referenced, it is again eligible for Garbage Collection. However, this time the finalize() method will not be invoked again, since Java only invokes the finalizer at most one time.

In the below code, the Garbage object is resurrected in the finalize method.

Screen Shot 2020-02-17 at 9.37.40 PM


Screen Shot 2020-02-17 at 9.38.33 PM

Leave a Reply

Your email address will not be published. Required fields are marked *