Search code examples
javagarbage-collectionfinalizer

How to add a safe guarded to release the resource that should have been released explicitly by the client code


The user is supposed to call Player.close() to release some critical resources managed by the native code and kill several internal java threads created for the player object. However,the user failed to call it ,resulting in some resource leak. Adding a Finalize method won't work because the finalize won't be called as the internal threads have not been killed...

How should we handle this situation?

EDIT:

I wiil look into the phantom reference. Besides, Shouldn't I terminate those aggregared thread so that player object become eligible for gc?


Solution

  • Make the internal threads hold only weak references to these things.

    Perhaps consider learning about phantom references.