Answer»
- When an object becomes eligible for garbage collection, the GC has to run the finalize method on it. The finalize method is guaranteed to run only once, thus the GC flags the object as finalize and gives it a rest until the NEXT cycle.
- In the finalize method you can technically “resurrect” an object, for example, by assigning it to a static field. The object would become alive again and non-eligible for garbage collection, so the GC would not collect it during the next cycle.
- The object, however, would be marked as finalized, so when it would become eligible again, the finalize method would not be called. In essence, you can turn this “RESURRECTION” TRICK only once for the LIFETIME of the object. Beware that this ugly hack should be used only if you really know what you’re doing — however, understanding this trick gives some insight into how the GC works.
|