[concurrency-interest] DirectByteBuffers and reachabilityFence

Alexandre De Champeaux adc at quartetfs.com
Mon Dec 7 07:40:18 EST 2015

Hi all,

I recently had a look at the discussion started by Peter Levart on October
21 (

It was a very insightful discussion, and made me aware that the "this"
object could be garbage collected while being inside a call of one of its

However, this got me concerned about the java.nio.DirectByteBuffer read and
write methods:
If the "this" object is garbage collected when making a call like
ByteBuffer.allocateDirect(int).someGetOrPutMethod(), the native pointer
that is passed to sun.misc.Unsafe might be freed, and accessing it will
cause the read/write to occur in an invalid memory area, which might lead
to a segfault, or other major issues.
This would be quite unlikely: JIT compilation needs to occur while keeping
a safepoint, then a GC must happen, and finally the ReferenceHandler thread
needs to perform its cleanup fast enough.

I am particularly concerned by the get(byte[] dst, int offset, int length)
method, that in turns calls Bits.copyToArray, which purposely splits its
calls to Unsafe.copyMemory to allow for safepoints to sneak in.

Am I correct, or does the JVM performs specific protection for instances of


Alexandre de Champeaux
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20151207/c0af94be/attachment.html>

More information about the Concurrency-interest mailing list