2

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?

Entea
  • 949
  • 14
  • 25
Prashant Shilimkar
  • 7,816
  • 12
  • 49
  • 85

3 Answers3

1

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

Stuart Marks
  • 120,620
  • 35
  • 192
  • 252
1

There are WeakReferences as already mentioned but there are also Phantom References

All of these techniques just allow you to monitor garbage collection on specific objects though.

There is a good description of phantom references here: http://java.dzone.com/articles/finalization-and-phantom

Tim B
  • 39,784
  • 16
  • 75
  • 127
0

You do have finalizers, but they're not guaranteed to run.

Further discussion about why finalizers may not run in this thread: When is the finalize() method called in Java?

Community
  • 1
  • 1
Kayaman
  • 70,361
  • 5
  • 75
  • 119