Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - When does direct buffer released?

Since it's out of jvm heap & gc, when does it released? Or, it remain until process termination?

I already checked:

  • how to garbage collect a direct buffer java
  • Deallocating Direct Buffer Native Memory in Java for JOGL
  • ByteBuffer.allocate() vs. ByteBuffer.allocateDirect()

But all the answers are blur, none answered it explicitly, does there have a clear answer? At least for Java 8 on 64-bit Linux.

like image 487
user218867 Avatar asked Mar 18 '16 06:03

user218867


People also ask

What is direct buffer memory error in Java?

As we said, the Direct Buffers are allocated to native memory space outside of the JVM’s established heap/perm gens. If this memory space outside of heap/perm is exhausted, the java.lang.OutOfMemoryError: Direct buffer memory Error will be throw.

Does directbytebuffer use old Java finalizers?

DirectByteBuffer does not use old Java finalizers. Instead, it uses internal sun.misc.Cleaner API. It creates new thread and stores a PhantomReference to every DirectByteBuffer created (except duplicates and slices which refer to the primary buffer).

How do I close or release a buffer in Java?

After all, there's no method to explicitly close or release them. The answer is that they get garbage collected like any other object, but with one twist: if you don't have enough virtual memory space or commit charge to allocate a direct buffer, that will trigger a full collection even if there's plenty of heap memory available.

What happens when a directbytebuffer becomes phantom-reachable in Java?

When the DirectByteBuffer becomes phantom-reachable (that is, no strong, soft or weak references to the byte buffer exist anymore) and garbage collector sees this, it adds this buffer to the ReferenceQueue which is processed by Cleaner thread. So three events should occur: DirectByteBuffer becomes phantom-reachable.


Video Answer


1 Answers

DirectByteBuffer does not use old Java finalizers. Instead, it uses internal sun.misc.Cleaner API. It creates new thread and stores a PhantomReference to every DirectByteBuffer created (except duplicates and slices which refer to the primary buffer). When the DirectByteBuffer becomes phantom-reachable (that is, no strong, soft or weak references to the byte buffer exist anymore) and garbage collector sees this, it adds this buffer to the ReferenceQueue which is processed by Cleaner thread. So three events should occur:

  • 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.

So in general you have no guarantee when it's freed. If you have enough memory in the Java heap, garbage collector may not be activated for a long time. Also even when it's phantom-reachable, Cleaner thread may need some time to reach this entry. It might be busy processing previous objects which also used the Cleaner API. Note however that partial work-around is implemented in JDK: if you create new DirectByteBuffer and allocated too much direct memory before, garbage collector might be called explicitly to enforce deallocation of previously abandoned buffers. See Bits.reserveMemory() (called from DirectByteBuffer constructor) for details.

Note that in Java-9 the internal Cleaner API was rectified and published for general use: now it's java.lang.ref.Cleaner. Reading the JavaDoc you may get more details how it works.

like image 72
Tagir Valeev Avatar answered Nov 03 '22 02:11

Tagir Valeev