List of usage examples for java.nio Buffer getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:Main.java
private static boolean isDirect(Buffer buf) { if (buf instanceof FloatBuffer) { return ((FloatBuffer) buf).isDirect(); }/* w w w .j a v a 2 s . c om*/ if (buf instanceof IntBuffer) { return ((IntBuffer) buf).isDirect(); } if (buf instanceof ShortBuffer) { return ((ShortBuffer) buf).isDirect(); } if (buf instanceof ByteBuffer) { return ((ByteBuffer) buf).isDirect(); } if (buf instanceof DoubleBuffer) { return ((DoubleBuffer) buf).isDirect(); } if (buf instanceof LongBuffer) { return ((LongBuffer) buf).isDirect(); } throw new UnsupportedOperationException(" BufferUtils.isDirect was called on " + buf.getClass().getName()); }
From source file:com.linkedin.pinot.common.utils.MmapUtils.java
/** * Unloads a byte buffer from memory//from w w w .ja va 2s. c o m * * @param buffer The buffer to unload, can be null */ public static void unloadByteBuffer(Buffer buffer) { if (null == buffer) return; // Non direct byte buffers do not require any special handling, since they're on heap if (!buffer.isDirect()) return; // Remove usage from direct byte buffer usage final int bufferSize = buffer.capacity(); final AllocationContext bufferContext = BUFFER_TO_CONTEXT_MAP.get(buffer); // A DirectByteBuffer can be cleaned up by doing buffer.cleaner().clean(), but this is not a public API. This is // probably not portable between JVMs. try { Method cleanerMethod = buffer.getClass().getMethod("cleaner"); Method cleanMethod = Class.forName("sun.misc.Cleaner").getMethod("clean"); cleanerMethod.setAccessible(true); cleanMethod.setAccessible(true); // buffer.cleaner().clean() Object cleaner = cleanerMethod.invoke(buffer); if (cleaner != null) { cleanMethod.invoke(cleaner); if (bufferContext != null) { switch (bufferContext.allocationType) { case DIRECT_BYTE_BUFFER: DIRECT_BYTE_BUFFER_USAGE.addAndGet(-bufferSize); if (LOGGER.isDebugEnabled()) { LOGGER.debug( "Releasing byte buffer of size {} with context {}, allocation after operation {}", bufferSize, bufferContext, getTrackedAllocationStatus()); } break; case MMAP: MMAP_BUFFER_USAGE.addAndGet(-bufferSize); MMAP_BUFFER_COUNT.decrementAndGet(); if (LOGGER.isDebugEnabled()) { LOGGER.debug( "Unmapping byte buffer of size {} with context {}, allocation after operation {}", bufferSize, bufferContext, getTrackedAllocationStatus()); } break; } BUFFER_TO_CONTEXT_MAP.remove(buffer); } else { LOGGER.warn( "Attempted to release byte buffer of size {} with no context, no deallocation performed.", bufferSize); if (LOGGER.isDebugEnabled()) { List<String> matchingAllocationContexts = new ArrayList<>(); synchronized (BUFFER_TO_CONTEXT_MAP) { clearSynchronizedMapEntrySetCache(); for (Map.Entry<ByteBuffer, AllocationContext> byteBufferAllocationContextEntry : BUFFER_TO_CONTEXT_MAP .entrySet()) { if (byteBufferAllocationContextEntry.getKey().capacity() == bufferSize) { matchingAllocationContexts .add(byteBufferAllocationContextEntry.getValue().toString()); } } // Clear the entry set cache afterwards so that we don't hang on to stale entries clearSynchronizedMapEntrySetCache(); } LOGGER.debug("Contexts with a size of {}: {}", bufferSize, matchingAllocationContexts); LOGGER.debug("Called by: {}", Utils.getCallingMethodDetails()); } } } } catch (Exception e) { LOGGER.warn("Caught (ignored) exception while unloading byte buffer", e); } }