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
  • 947
  • 14
  • 26
Prashant Shilimkar
  • 8,402
  • 13
  • 54
  • 89

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
  • 127,867
  • 37
  • 205
  • 259
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
  • 40,716
  • 16
  • 83
  • 128
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
  • 72,141
  • 5
  • 83
  • 121