Reputation: 315
I am using direct buffers (java.nio) to store vertex information for JOGL. These buffers are large, and they are replaced several times during the application life. The memory is not deallocated in time and I am running out of memory after a few replacements.
It seems that there is not good way to deallocate using java.nio's buffer classes. My question is this:
Is there some method in JOGL to delete Direct Buffers? I am looking into glDeleteBuffer(), but it seems like this only deletes the buffer from the video card memory.
Thanks
Upvotes: 16
Views: 11608
Reputation: 4105
The direct NIO buffers use unmanaged memory. It means that they are allocated on the native heap, not on the Java heap. As a consequence, they are freed only when the JVM runs out of memory on the Java heap, not on the native heap. In other terms, it's unmanaged = it's up to you to manage them. Forcing the garbage collection is discouraged and won't solve this problem most of the time.
When you know that a direct NIO buffer has become useless for you, you have to release its native memory by using its sun.misc.Cleaner (StaxMan is right) and call clean() (except with Apache Harmony), call free() (with Apache Harmony) or use a better public API to do that (maybe in Java > 12, AutoCleaning that extends AutoCloseable?).
It's not JOGL job to do that, you can use plain Java code to do it yourself. My example is under GPL v2 and this example is under a more permissive license.
Edit.: My latest example works even with Java 1.9 and supports OpenJDK, Oracle Java, Sun Java, Apache Harmony, GNU Classpath and Android. You might have to remove some syntactical sugar to make it work with Java < 1.7 (the multi catches, the diamonds and the generics).
Reference: http://www.ibm.com/developerworks/library/j-nativememory-linux/
Direct ByteBuffer objects clean up their native buffers automatically but can only do so as part of Java heap GC — so they do not automatically respond to pressure on the native heap. GC occurs only when the Java heap becomes so full it can't service a heap-allocation request or if the Java application explicitly requests it (not recommended because it causes performance problems).
Reference: http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html#direct
The contents of direct buffers may reside outside of the normal garbage-collected heap
This solution is integrated in Java 14:
try (MemorySegment segment = MemorySegment.allocateNative(100)) {
...
}
You can wrap a byte buffer into a memory segment by calling MemorySegment.ofByteBuffer(ByteBuffer), get its memory address and free it (this is a restricted method) in Java 16:
CLinker.getInstance().freeMemoryRestricted(MemorySegment.ofByteBuffer(myByteBuffer).address());
Note that you still need to use reflection in many non trivial cases in order to find the buffer that can be deallocated, typically when your direct NIO buffer isn't a ByteBuffer.
N.B: sun.misc.Cleaner has been moved into jdk.internal.ref.Cleaner in Java 1.9 in the module "java.base", the latter implemented java.lang.Runnable (thanks to Alan Bateman for reminding me that difference) for a short time but it's no longer the case. You have to call sun.misc.Unsafe.invokeCleaner(), it's done in JogAmp's Gluegen. I preferred using the Cleaner as a Runnable as it avoided to rely on sun.misc.Unsafe but it doesn't work now.
My last suggestion works with Java 9, 10, 11 and 12.
My very latest example requires the use of an incubated feature (requires Java >= 14) but is very very simple.
N.B: Using a memory segment to free a preexisting direct NIO buffer is no longer possible since Java 18. Instead, create a native memory segment by calling MemorySegment.allocateNative(), call asByteBuffer() on it and close its memory session when you no longer need it. It's a lot more invasive but there are probably excellent API design decisions behind this change. Maybe MemorySegment.ofBuffer() in Java >= 21 helps but I fear that it uses a global arena that you can't safely close.
There is a good example in Lucene under a more permissive license.
Upvotes: 21
Reputation: 533
Using information in gouessej's answer, I was able to put this utility class together for freeing the direct memory allocation of a given ByteBuffer. This, of course, should only be used as a last resort, and shouldn't really be used in production code.
Tested and working in Java SE version 10.0.2.
public final class BufferUtil {
//various buffer creation utility functions etc. etc.
protected static final sun.misc.Unsafe unsafe = AccessController.doPrivileged(new PrivilegedAction<sun.misc.Unsafe>() {
@Override
public Unsafe run() {
try {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
return (Unsafe) f.get(null);
} catch(NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
throw new RuntimeException(ex);
}
}
});
/** Frees the specified buffer's direct memory allocation.<br>
* The buffer should not be used after calling this method; you should
* instead allow it to be garbage-collected by removing all references of it
* from your program.
*
* @param directBuffer The direct buffer whose memory allocation will be
* freed
* @return Whether or not the memory allocation was freed */
public static final boolean freeDirectBufferMemory(ByteBuffer directBuffer) {
if(!directBuffer.isDirect()) {
return false;
}
try {
unsafe.invokeCleaner(directBuffer);
return true;
} catch(IllegalArgumentException ex) {
ex.printStackTrace();
return false;
}
}
}
Upvotes: 0
Reputation: 1
Rather than abuse reflection of non-public apis you can do this trivially, entirely within the supported public ones.
Write some JNI which wraps malloc with NewDirectByteBuffer (remember to set the order), and an analogous function to free it.
Upvotes: -1
Reputation: 106401
Direct buffers are tricky and don't have the usual garbage collection guarantees - see for more detail: http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html#direct
If you are having issues, I'd suggest allocating once and re-using the buffer rather than allocating and deallocating repeatedly.
Upvotes: 3
Reputation: 116600
The way in which deallocation is done is awful -- a soft reference is basically inserted into a Cleaner object, which then does deallocation when owning ByteBuffer is garbage collected. But this is not really guaranteed to be called in timely manner.
Upvotes: 2
Reputation: 114817
Deallocating a Direct Buffer is a job done by the garbage collector some time after the ByteBuffer object is marked.
You could try calling the gc immediatly after deleting the last reference to your buffer. At least there's a chance that the memory will be free'd a bit faster.
Upvotes: 0