前面一文已经提到动态库中分配的内存JNA中需要手工的释放,具体方法如下:
Java Code:
public interface Libc extends Library{
Libc INSTANCE = (Libc) Native.loadLibrary("c", Libc.class);
public void free(Pointer p);
}
public static void testJNAMemLeak1()
{
PointerByReference head = new PointerByReference();
IntByReference length = new IntByReference();
while(true)
{
libben.checkJNAMemLeak1(head, length);
System.out.println(length.getValue());
sleep(1);
libc.free(head.getValue());
}
}