Search code examples
javajvmfinalizeazul-zulu

Prevent Java VM from finalizing an object


Is there any way to tell the Java VM (for this case, HotSpot or Zing) to no longer treat an objects as if there is a finalize method? Really, the whole class, I'm guessing, if at all possible.

I'm having problems with objects that have already had their resources freed but still put pressure on the VM that thinks it needs to call finalize on them.

This is in a library, so there is no way to change the class.

The use case is a library was written to clean up off-heap resources if you forgot or decided to let the GC do it for you. The problem is I get a lot of them hanging around.


Solution

  • The use case is a library was written to clean up off-heap resources if you forgot or decided to let the GC do it for you.

    For that we use a Cleaner as ByteBuffer does. This is more light weight way of cleaning up resources.


    EDIT Another option is you could

    • obtain the Finalizer.queue and remove elements yourself while holding the Finalizer.lock You could add a thread which periodically cleans up the queue of elements which don't need to be there.
    • you could replace this queue with your own implementation so it behaves differently. e.g. not add objects of a chosen class in the first place.