we have the following piece of code:
long buffer = ((DirectBuffer) ByteBuffer.allocateDirect(256)).address();
It seems that there is no reference to direct buffer (as an object) on the thread's stack. So, it means that that object is phantom-reachable.
- DirectByteBuffer becomes phantom-reachable.
- Garbage collection is performed (in separate thread), DirectByteBuffer Java object is collected and an entry is added to the ReferenceQueue.
- Cleaner thread reaches this entry and runs the registered clean-up action (in this case, it's java.nio.DirectByteBuffer.Deallocator object), this action finally frees the native memory.
The citation comes from: Java - When does direct buffer released?
So, it is possible that allocated memory that can be freed. However, we have a pointer to that, buffer
of type long
. Therefore it is possible that we've got SIGSEGV or something like that.
My question is:
Does it mean that we can hurt ourself using DirectBuffer in that manner?