释放Java中本机库分配的内存 [英] Releasing Memory Allocated by Native Libraries in Java

查看:104
本文介绍了释放Java中本机库分配的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果运行的代码使用Java调用本机库,那么当内存分配应在对象的整个生命周期内都可用时,释放这些库分配的内存的常用方法是什么?在C ++中,我将使用析构函数,但Java从未真正拥有这些析构函数,并且现在有更少了.

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 last for the lifetime of the object? In C++, I would use destructors, but Java never really had those and has them even less now.

我最感兴趣的特定案例是 JOCL ,其中有一个包装已编译对象的对象OpenCL内核及其所有自变量始终相同.表示已编译内核和参数的结构都在库侧分配,JOCL提供了一种方法

The specific case I'm most interested in is JOCL, where I have an object that wraps a compiled OpenCL kernel and all of the arguments thereto that are always the same. Structures representing the compiled kernel and the arguments are all allocated on the library side, and JOCL provides a method clReleaseMemObject that you call to decrement a reference counter indicating when the object should be deleted (note that this is a bit different from directly freeing the memory, but I don't think substantially so in this case).

我假设如果程序终止时对象仍然存在,则操作系统将清除所有内容,但是我不确定线程​​中创建的对象.所以:

I presume that if the object is still around when the program terminates, everything is cleaned up by the OS, but I'm not so sure about about objects created in a thread. So:

  1. 如果要在垃圾回收对象时释放本机内存,是否有合适的位置调用释放该内存的方法?

  1. If you want the native memory deallocated when the object is garbage collected, is there a proper place to call the method that releases this memory?

如果该对象是一个可以在线程持续时间内使用的对象,那么是否存在进行此调用的适当位置,或者甚至有必要这样做?

If the object is one that will last for the duration of a thread, is there a proper place to make this call, or is this even necessary?

推荐答案

您可以使用

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

本质上,您给它提供了Runnable,以便在清理资源时执行.

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

使用清理器的一个优点是您可以调用它确定性地进行清理,但是如果您忘记或不这样做,GC将在运行后对其进行调用.

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.

当线程死亡时,没有一种安全的方法来清理对象,因为即使对象已死,线程对象也可以在程序的生命期内生存.一种更简单的方法是根据您的了解进行清理,或者在GC确定不需要之后进行清理.

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.

另一种方法是使用参考队列和后台线程.它不那么优雅,但可以在Java 8和更高版本中使用.

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.

这篇关于释放Java中本机库分配的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆