Garbage collector and finalize method in Java Baeldung


From one article

The collector will clean up the finalized object in two steps: in the first it will execute finalize, and in the second it will collect it.

And so questions

  1. When the collector encounters a finalized object, it first dispatches it to the queue. And the code in the finalize method is executed already in the queue. When the method is executed, it will become available to the collector and destroyed during the next build. Did I understand correctly?
  2. Stop the world affects the Finalizer stream?


Yes, you're right – after executing the finalize() method, the object must be re-collected by the garbage collector (and this is considered a serious problem with the finalize() method – it prevents the garbage collector from freeing memory).

By the way, the object will not necessarily be available for assembly immediately – the finalize() method can store a reference to the object somewhere. Such a situation is called "revival" of an object and, generally speaking, is considered an anti-pattern. The main problem with this trick is that you can only "revive" an object once.

Stop the world certainly affects the Finalizer stream, since it is the same stream as everyone else.

Scroll to Top