I assumed, I understood how Bytebuffer and DirectByteBuffer differs until i read a artical on IBM documentation, metioning :
"Direct ByteBuffer objects clean up their native buffers automatically but can only do so as part of Java heap GC"
https://www.ibm.com/developerworks/library/j-nativememory-linux/
Now I am not able to understand this line, as it says DirectByteBuffer does cleaning as part of Java heap GC.
IFAIK, Java Heap GC only do clean up in java heap(where DirectByteBuffer is not allocated). It(GC) is no aware of native memory(where DirectByteBuffer is allocated).
Please help me understand this line, or if there is gap in my understanding
A direct buffer is a chunk of native memory shared with Java from which you can perform a direct read. An instance of DirectByteBuffer can be created using the ByteBuffer. allocateDirect() factory method.
The clear() method of java. nio. ByteBuffer Class is used to clear this buffer. The position is set to zero, the limit is set to the capacity, and the mark is discarded.
A GC cycle is a repeatable process that involves a set of GC operations. These operations process all or parts of the Java heap. When operating on the whole of the Java heap, the cycle is referred to as a global GC cycle; When operating on part of the heap, the cycle is referred to as a partial GC cycle.
When you create an instance of java.nio.DirectByteBuffer
you, essentially, have 2 parts:
java.nio.DirectByteBuffer
, which is allocated on the heapIn addition, the constructor of java.nio.DirectByteBuffer
registers a runnable of type java.nio.DirectByteBuffer.Deallocator
, which is a private static class. This runnable is executed when this instance of java.nio.DirectByteBuffer
is cleaned up by the GC. And it is the task of this Deallocator
to release the native byte buffer. RTFS! :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With