public static synchronized void cleanDirectBuffer(ByteBuffer buffer) {
    Preconditions.checkNotNull(buffer, "buffer");
    Preconditions.checkArgument(buffer.isDirect(), "buffer isn't a DirectByteBuffer");
    try {
      if (sByteBufferCleanerMethod == null) {
        sByteBufferCleanerMethod = buffer.getClass().getMethod("cleaner");
        sByteBufferCleanerMethod.setAccessible(true);
      }
      final Object cleaner = sByteBufferCleanerMethod.invoke(buffer);
      if (cleaner == null) {
        if (buffer.capacity() > 0) {
          LOG.warn("Failed to get cleaner for ByteBuffer: {}", buffer.getClass().getName());
        }
        // The cleaner could be null when the buffer is initialized as zero capacity.
        return;
      }
      if (sCleanerCleanMethod == null) {
        sCleanerCleanMethod = cleaner.getClass().getMethod("clean");
      }
      sCleanerCleanMethod.invoke(cleaner);
    } catch (Exception e) {
      LOG.warn("Failed to unmap direct ByteBuffer: {}, error message: {}",
                buffer.getClass().getName(), e.getMessage());
    } finally {
      // Force to drop reference to the buffer to clean
      buffer = null;
    }
  }
public static synchronized void cleanDirectBuffer ( ByteBuffer buffer ) { Preconditions . checkNotNull ( buffer , "buffer" ) ; Preconditions . checkArgument ( buffer . isDirect ( ) , "buffer isn't a DirectByteBuffer" ) ; try { if ( sByteBufferCleanerMethod == null ) { sByteBufferCleanerMethod = buffer . getClass ( ) . getMethod ( "cleaner" ) ; sByteBufferCleanerMethod . setAccessible ( true ) ; } final Object cleaner = sByteBufferCleanerMethod . invoke ( buffer ) ; if ( cleaner == null ) { if ( buffer . capacity ( ) > 0 ) { LOG . warn ( "Failed to get cleaner for ByteBuffer: {}" , buffer . getClass ( ) . getName ( ) ) ; } return ; } if ( sCleanerCleanMethod == null ) { sCleanerCleanMethod = cleaner . getClass ( ) . getMethod ( "clean" ) ; } sCleanerCleanMethod . invoke ( cleaner ) ; } catch ( Exception e ) { LOG . warn ( "Failed to unmap direct ByteBuffer: {}, error message: {}" , buffer . getClass ( ) . getName ( ) , e . getMessage ( ) ) ; } finally { buffer = null ; } }
Forces to unmap a direct buffer if this buffer is no longer used. After calling this method,
this direct buffer should be discarded. This is unsafe operation and currently a work-around to
avoid huge memory occupation caused by memory map.

<p>
NOTE: DirectByteBuffers are not guaranteed to be garbage-collected immediately after their
references are released and may lead to OutOfMemoryError. This function helps by calling the
Cleaner method of a DirectByteBuffer explicitly. See <a
href="http://stackoverflow.com/questions/1854398/how-to-garbage-collect-a-direct-buffer-java"
>more discussion</a>.

@param buffer the byte buffer to be unmapped, this must be a direct buffer