B
B
brucebanner2016-10-17 05:03:07
Java
brucebanner, 2016-10-17 05:03:07

Garbage collector and finalize method in Java?

First question. From one article.

System.runFinalization() creates a second "SecondaryFinalizer" thread, which also calls finalize() on objects in the same queue, while the thread that called System.runFinalization() waits until the Finalizer queue that is currently available has run out.

I did not understand here, will it work in parallel? Or all the same, it will wait until the end of the first stream and continue, but then what's the point of it.
Second question. The quote below is from another 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 be collected.

  1. The collector goes for a walk in memory
  2. Stops all threads
  3. Makes its own operations with objects depending on the approach. It encounters a finalized object, executes the code in the finalize() body, and submits it to the Finalizer's queue for removal.
  4. Clears garbage from memory without touching the Finalizer queue.
  5. Resumes the application.

Second pass.
The same thing, only at the third point, the Finalizer thread runs in parallel, clearing objects from the queue.
Did I understand correctly? Or, the Finalizer thread runs independently of the collector itself. And already in the first pass at step 4, the Finalizer thread will live its own life. And does stop the world affect the Finalizer thread? And another question on point 3, will the code in the finalize body be executed immediately before being placed in the queue, or is it already in the queue before being deleted?
Thanks a lot!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question