Search code examples
javagarbage-collectionhook

Is there any hook provided to register a thread with garbage collection in java?


JVM provides you a hook to register a thread with the shutdown initiation sequence. Once a thread is registered, on every shutdown that thread is run.

Now, is there any such a hook java provide to register a thread with JVM's Garbage collector?


Solution

  • Not exactly a hook, but you can use a WeakReference to be notified that an object has become eligible to be garbage collected.