Releasing Memory Allocated by Native Libraries in Java

后端 未结 1 1337
清酒与你
清酒与你 2021-01-03 03:09

If you are running code that makes calls to a native library in Java, what is the usual way of freeing memory allocated by these libraries when the memory allocation should

相关标签:
1条回答
  • 2021-01-03 03:41

    What you can do is use a Cleaner. This is a more official API in Java 9 but is available in Java 1.4+.

    Essentially you give it a Runnable to execute when the resource is cleaned up.

    One advantage of using a Cleaner is you can call it to clean up deterministically, but if you forget or fail to do so, the GC will call it after it runs.

    There isn't a safe way to clean up an object when a thread dies as the Thread object can live for the life of the program even if dead. A simpler approach is to clean up as you know it is not needed or after the GC determines it is not required.

    Another approach is to use a reference queue and a background thread. It's not as elegant but works across Java 8 and later versions.

    0 讨论(0)
提交回复
热议问题