Top Description Inners Fields Constructors Methods
org.glassfish.grizzly.memory

public Class HeapMemoryManager

extends AbstractMemoryManager<HeapBuffer>
implements WrapperAware
Class Inheritance
All Implemented Interfaces
org.glassfish.grizzly.memory.WrapperAware
Imports
java.nio.ByteBuffer, .ByteOrder, java.nio.charset.Charset, java.util.Arrays, org.glassfish.grizzly.Buffer, .Cacheable, .ThreadCache, org.glassfish.grizzly.monitoring.MonitoringConfig, .MonitoringUtils

A WrapperAware MemoryManager implementation for managing HeapBuffer instances.
Since
2.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
HeapMemoryManager.HeapBufferThreadLocalPool

Information about thread associated memory pool.

private static class
private static class
HeapMemoryManager.TrimmableHeapBuffer

HeapBuffer implementation, which supports trimming.

Field Summary

Modifier and TypeField and Description
private static final ThreadCache.CachedTypeIndex<HeapMemoryManager.RecyclableByteBufferWrapper>
private static final ThreadCache.CachedTypeIndex<HeapMemoryManager.TrimmableHeapBuffer>
Inherited from org.glassfish.grizzly.memory.AbstractMemoryManager:
DEFAULT_MAX_BUFFER_SIZEmaxBufferSizemonitoringConfig

Constructor Summary

AccessConstructor and Description
public
public
HeapMemoryManager(final int maxBufferSize)

Method Summary

Modifier and TypeMethod and Description
public HeapBuffer
allocate(final int size)

Implements org.glassfish.grizzly.memory.MemoryManager.allocate.

Allocated Buffer of the required size.

public HeapBuffer
allocateAtLeast(final int size)

Implements org.glassfish.grizzly.memory.MemoryManager.allocateAtLeast.

Allocated Buffer at least of the provided size.

protected HeapBuffer
allocateHeapBuffer(final int
size of buffer created
size
)

Creates a new HeapBuffer with a a specified size.

protected HeapBuffer
allocateHeapBufferAtLeast(final int size)

private ByteBufferWrapper
createByteBufferWrapper(final ByteBuffer underlyingByteBuffer)

protected Object
public ThreadLocalPool<E>
pack-priv HeapMemoryManager.TrimmableHeapBuffer
createTrimAwareBuffer(final int length)

pack-priv HeapMemoryManager.TrimmableHeapBuffer
createTrimAwareBuffer(final byte[] heap, final int offset, final int length)

private static HeapMemoryManager.HeapBufferThreadLocalPool
getHeapBufferThreadLocalPool()

Gets the thread local buffer pool If the pool is not an instance of HeapBufferThreadLocalPool then null is returned

public MonitoringConfig<MemoryProbe>
public HeapBuffer
reallocate(final HeapBuffer oldBuffer, final int newSize)

Implements org.glassfish.grizzly.memory.MemoryManager.reallocate.

Reallocate Buffer to a required size.

protected HeapBuffer
reallocateHeapBuffer(HeapBuffer oldHeapBuffer, int newSize)

private void
public void
protected final void
releaseHeapBuffer(final HeapBuffer heapBuffer)

public boolean
willAllocateDirect(int size)

Implements org.glassfish.grizzly.memory.MemoryManager.willAllocateDirect.

Return true if next allocate(int) or allocateAtLeast(int) call, made in the current thread for the given memory size, going to return a Buffer based on direct java.nio.ByteBuffer, or false otherwise.

public HeapBuffer
wrap(final byte[] data)

Implements org.glassfish.grizzly.memory.WrapperAware.wrap.

Returns Buffer, which wraps the byte array.

public HeapBuffer
wrap(final byte[] data, final int offset, final int length)

Implements org.glassfish.grizzly.memory.WrapperAware.wrap.

Returns Buffer, which wraps the part of byte array with specific offset and length.

public HeapBuffer
wrap(final String s)

Implements org.glassfish.grizzly.memory.WrapperAware.wrap.

Returns Buffer, which wraps the String.

public HeapBuffer
wrap(final String s, final Charset charset)

Implements org.glassfish.grizzly.memory.WrapperAware.wrap.

Returns Buffer, which wraps the String with the specific Charset.

public Buffer
wrap(final ByteBuffer byteBuffer)

Implements org.glassfish.grizzly.memory.WrapperAware.wrap.

Returns Buffer, which wraps the ByteBuffer.

Inherited from org.glassfish.grizzly.memory.AbstractMemoryManager:
allocateFromPoolgetMaxBufferSizegetReadyThreadBufferSizegetThreadLocalPool

Field Detail

BBW_CACHE_IDXback to summary
private static final ThreadCache.CachedTypeIndex<HeapMemoryManager.RecyclableByteBufferWrapper> BBW_CACHE_IDX
CACHE_IDXback to summary
private static final ThreadCache.CachedTypeIndex<HeapMemoryManager.TrimmableHeapBuffer> CACHE_IDX

Constructor Detail

HeapMemoryManagerback to summary
public HeapMemoryManager()
HeapMemoryManagerback to summary
public HeapMemoryManager(final int maxBufferSize)

Method Detail

allocateback to summary
public HeapBuffer allocate(final int size)

Implements org.glassfish.grizzly.memory.MemoryManager.allocate.

Doc from org.glassfish.grizzly.memory.MemoryManager.allocate.

Allocated Buffer of the required size.

Annotations
@Override
allocateAtLeastback to summary
public HeapBuffer allocateAtLeast(final int size)

Implements org.glassfish.grizzly.memory.MemoryManager.allocateAtLeast.

Doc from org.glassfish.grizzly.memory.MemoryManager.allocateAtLeast.

Allocated Buffer at least of the provided size. This could be useful for usecases like Socket.read(...), where we're not sure how many bytes are available, but want to read as much as possible.

Annotations
@Override
allocateHeapBufferback to summary
protected HeapBuffer allocateHeapBuffer(final int size)

Creates a new HeapBuffer with a a specified size.

Parameters
size:int

size of buffer created

allocateHeapBufferAtLeastback to summary
protected HeapBuffer allocateHeapBufferAtLeast(final int size)
createByteBufferWrapperback to summary
private ByteBufferWrapper createByteBufferWrapper(final ByteBuffer underlyingByteBuffer)
createJmxManagementObjectback to summary
protected Object createJmxManagementObject()

Implements abstract org.glassfish.grizzly.memory.AbstractMemoryManager.createJmxManagementObject.

Annotations
@Override
createThreadLocalPoolback to summary
public ThreadLocalPool<E> createThreadLocalPool()

Implements org.glassfish.grizzly.memory.ThreadLocalPoolProvider.createThreadLocalPool.

Annotations
@Override
createTrimAwareBufferback to summary
pack-priv HeapMemoryManager.TrimmableHeapBuffer createTrimAwareBuffer(final int length)
createTrimAwareBufferback to summary
pack-priv HeapMemoryManager.TrimmableHeapBuffer createTrimAwareBuffer(final byte[] heap, final int offset, final int length)
getHeapBufferThreadLocalPoolback to summary
private static HeapMemoryManager.HeapBufferThreadLocalPool getHeapBufferThreadLocalPool()

Gets the thread local buffer pool If the pool is not an instance of HeapBufferThreadLocalPool then null is returned

Annotations
@SuppressWarnings:unchecked
getMonitoringConfigback to summary
public MonitoringConfig<MemoryProbe> getMonitoringConfig()

Implements org.glassfish.grizzly.monitoring.MonitoringAware.getMonitoringConfig.

Doc from org.glassfish.grizzly.monitoring.MonitoringAware.getMonitoringConfig.

Return the object associated MonitoringConfig.

Annotations
@Override
reallocateback to summary
public HeapBuffer reallocate(final HeapBuffer oldBuffer, final int newSize)

Implements org.glassfish.grizzly.memory.MemoryManager.reallocate.

Doc from org.glassfish.grizzly.memory.MemoryManager.reallocate.

Reallocate Buffer to a required size. Implementation may choose the way, how reallocation could be done, either by allocating new Buffer of required size and copying old Buffer content there, or perform more complex logic related to memory pooling etc.

Annotations
@Override
reallocateHeapBufferback to summary
protected HeapBuffer reallocateHeapBuffer(HeapBuffer oldHeapBuffer, int newSize)
reallocatePoolBufferback to summary
private void reallocatePoolBuffer()
releaseback to summary
public void release(final HeapBuffer buffer)

Implements org.glassfish.grizzly.memory.MemoryManager.release.

Doc from org.glassfish.grizzly.memory.MemoryManager.release.

Release Buffer. Implementation may ignore releasing and let JVM Garbage collector to take care about the Buffer, or return Buffer to pool, in case of more complex MemoryManager implementation.

Annotations
@Override
releaseHeapBufferback to summary
protected final void releaseHeapBuffer(final HeapBuffer heapBuffer)
willAllocateDirectback to summary
public boolean willAllocateDirect(int size)

Implements org.glassfish.grizzly.memory.MemoryManager.willAllocateDirect.

Doc from org.glassfish.grizzly.memory.MemoryManager.willAllocateDirect.

Return true if next allocate(int) or allocateAtLeast(int) call, made in the current thread for the given memory size, going to return a Buffer based on direct java.nio.ByteBuffer, or false otherwise.

Annotations
@Override
wrapback to summary
public HeapBuffer wrap(final byte[] data)

Implements org.glassfish.grizzly.memory.WrapperAware.wrap.

Doc from org.glassfish.grizzly.memory.WrapperAware.wrap.

Returns Buffer, which wraps the byte array.

Annotations
@Override
wrapback to summary
public HeapBuffer wrap(final byte[] data, final int offset, final int length)

Implements org.glassfish.grizzly.memory.WrapperAware.wrap.

Doc from org.glassfish.grizzly.memory.WrapperAware.wrap.

Returns Buffer, which wraps the part of byte array with specific offset and length.

Annotations
@Override
wrapback to summary
public HeapBuffer wrap(final String s)

Implements org.glassfish.grizzly.memory.WrapperAware.wrap.

Doc from org.glassfish.grizzly.memory.WrapperAware.wrap.

Returns Buffer, which wraps the String.

Annotations
@Override
wrapback to summary
public HeapBuffer wrap(final String s, final Charset charset)

Implements org.glassfish.grizzly.memory.WrapperAware.wrap.

Doc from org.glassfish.grizzly.memory.WrapperAware.wrap.

Returns Buffer, which wraps the String with the specific Charset.

Annotations
@Override
wrapback to summary
public Buffer wrap(final ByteBuffer byteBuffer)

Implements org.glassfish.grizzly.memory.WrapperAware.wrap.

Doc from org.glassfish.grizzly.memory.WrapperAware.wrap.

Returns Buffer, which wraps the ByteBuffer.

Annotations
@Override
org.glassfish.grizzly.memory back to summary

private final Class HeapMemoryManager.HeapBufferThreadLocalPool

extends Object
implements ThreadLocalPool<HeapBuffer>
Class Inheritance
  • java.lang.Object
  • org.glassfish.grizzly.memory.HeapMemoryManager.HeapBufferThreadLocalPool
All Implemented Interfaces
org.glassfish.grizzly.memory.ThreadLocalPool

Information about thread associated memory pool.

Field Summary

Modifier and TypeField and Description
private final ByteBuffer[]
private int
private int
private int
private final HeapMemoryManager
private byte[]
pool

Memory pool

private int
private int

Constructor Summary

AccessConstructor and Description
public
public
HeapBufferThreadLocalPool(final HeapMemoryManager mm, final int maxByteBufferCacheSize)

Method Summary

Modifier and TypeMethod and Description
public HeapBuffer
allocate(final int size)

Implements org.glassfish.grizzly.memory.ThreadLocalPool.allocate.

Creates a buffer with a given capacity and limit

private void
public boolean
hasRemaining()

Implements org.glassfish.grizzly.memory.ThreadLocalPool.hasRemaining.

Whether there are elements between the current position and the end

public boolean
isLastAllocated(final HeapBuffer oldHeapBuffer)

Implements org.glassfish.grizzly.memory.ThreadLocalPool.isLastAllocated.

Whether the last element in the buffer has been set

private boolean
isReleasableLeft(final HeapBuffer oldHeapBuffer)

public HeapBuffer
reallocate(final HeapBuffer heapBuffer, final int newSize)

Implements org.glassfish.grizzly.memory.ThreadLocalPool.reallocate.

Creates a new Buffer with a set size and assigns it the data that was held in the old one as long as the given size is not smaller than the data held.

public HeapBuffer
reduceLastAllocated(final HeapBuffer heapBuffer)

Implements org.glassfish.grizzly.memory.ThreadLocalPool.reduceLastAllocated.

Reduces the buffer to the last data allocated

public boolean
release(final HeapBuffer heapBuffer)

Implements org.glassfish.grizzly.memory.ThreadLocalPool.release.

deallocates the data in the buffer

public int
remaining()

Implements org.glassfish.grizzly.memory.ThreadLocalPool.remaining.

Gets the number of elements between the current position and the limit

public void
reset(final HeapBuffer heapBuffer)

Implements org.glassfish.grizzly.memory.ThreadLocalPool.reset.

Resets the Buffer to empty values and empties the pool

public void
reset(final byte[] heap, final int offset, final int capacity)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public boolean
wantReset(final int size)

Implements org.glassfish.grizzly.memory.ThreadLocalPool.wantReset.

Checks if the size of the Buffer should be reset.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

byteBufferCacheback to summary
private final ByteBuffer[] byteBufferCache
byteBufferCacheSizeback to summary
private int byteBufferCacheSize
endback to summary
private int end
leftPosback to summary
private int leftPos
mmback to summary
private final HeapMemoryManager mm
poolback to summary
private byte[] pool

Memory pool

rightPosback to summary
private int rightPos
startback to summary
private int start

Constructor Detail

HeapBufferThreadLocalPoolback to summary
public HeapBufferThreadLocalPool(final HeapMemoryManager mm)
HeapBufferThreadLocalPoolback to summary
public HeapBufferThreadLocalPool(final HeapMemoryManager mm, final int maxByteBufferCacheSize)

Method Detail

allocateback to summary
public HeapBuffer allocate(final int size)

Implements org.glassfish.grizzly.memory.ThreadLocalPool.allocate.

Doc from org.glassfish.grizzly.memory.ThreadLocalPool.allocate.

Creates a buffer with a given capacity and limit

Annotations
@Override
clearByteBufferCacheback to summary
private void clearByteBufferCache()
hasRemainingback to summary
public boolean hasRemaining()

Implements org.glassfish.grizzly.memory.ThreadLocalPool.hasRemaining.

Doc from org.glassfish.grizzly.memory.ThreadLocalPool.hasRemaining.

Whether there are elements between the current position and the end

Annotations
@Override
isLastAllocatedback to summary
public boolean isLastAllocated(final HeapBuffer oldHeapBuffer)

Implements org.glassfish.grizzly.memory.ThreadLocalPool.isLastAllocated.

Doc from org.glassfish.grizzly.memory.ThreadLocalPool.isLastAllocated.

Whether the last element in the buffer has been set

Annotations
@Override
isReleasableLeftback to summary
private boolean isReleasableLeft(final HeapBuffer oldHeapBuffer)
reallocateback to summary
public HeapBuffer reallocate(final HeapBuffer heapBuffer, final int newSize)

Implements org.glassfish.grizzly.memory.ThreadLocalPool.reallocate.

Doc from org.glassfish.grizzly.memory.ThreadLocalPool.reallocate.

Creates a new Buffer with a set size and assigns it the data that was held in the old one as long as the given size is not smaller than the data held.

Annotations
@Override
reduceLastAllocatedback to summary
public HeapBuffer reduceLastAllocated(final HeapBuffer heapBuffer)

Implements org.glassfish.grizzly.memory.ThreadLocalPool.reduceLastAllocated.

Doc from org.glassfish.grizzly.memory.ThreadLocalPool.reduceLastAllocated.

Reduces the buffer to the last data allocated

Annotations
@Override
releaseback to summary
public boolean release(final HeapBuffer heapBuffer)

Implements org.glassfish.grizzly.memory.ThreadLocalPool.release.

Doc from org.glassfish.grizzly.memory.ThreadLocalPool.release.

deallocates the data in the buffer

Annotations
@Override
remainingback to summary
public int remaining()

Implements org.glassfish.grizzly.memory.ThreadLocalPool.remaining.

Doc from org.glassfish.grizzly.memory.ThreadLocalPool.remaining.

Gets the number of elements between the current position and the limit

Annotations
@Override
resetback to summary
public void reset(final HeapBuffer heapBuffer)

Implements org.glassfish.grizzly.memory.ThreadLocalPool.reset.

Doc from org.glassfish.grizzly.memory.ThreadLocalPool.reset.

Resets the Buffer to empty values and empties the pool

Annotations
@Override
resetback to summary
public void reset(final byte[] heap, final int offset, final int capacity)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override
wantResetback to summary
public boolean wantReset(final int size)

Implements org.glassfish.grizzly.memory.ThreadLocalPool.wantReset.

Doc from org.glassfish.grizzly.memory.ThreadLocalPool.wantReset.

Checks if the size of the Buffer should be reset.

Annotations
@Override
org.glassfish.grizzly.memory back to summary

private final Class HeapMemoryManager.RecyclableByteBufferWrapper

extends ByteBufferWrapper
implements Cacheable
Class Inheritance
All Implemented Interfaces
org.glassfish.grizzly.Cacheable

Field Summary

Inherited from org.glassfish.grizzly.memory.ByteBufferWrapper:
allowBufferDisposeDEBUG_MODEdisposeStackTracemarkvisible

Constructor Summary

AccessConstructor and Description
private
RecyclableByteBufferWrapper(final ByteBuffer underlyingByteBuffer)

Method Summary

Modifier and TypeMethod and Description
public void
dispose()

Overrides org.glassfish.grizzly.memory.ByteBufferWrapper.dispose.

Implements org.glassfish.grizzly.Buffer.dispose.

Notify the allocator that the space for this Buffer is no longer needed.

private void
initialize(final ByteBuffer underlyingByteBuffer)

public void
Inherited from org.glassfish.grizzly.memory.ByteBufferWrapper:
allowBufferDisposeallowBufferDisposearrayarrayOffsetasReadOnlyBuffercapacitycheckDisposeclearcompactcompareTodumpHexduplicateequalsflipgetgetgetgetgetgetgetChargetChargetDoublegetDoublegetFloatgetFloatgetIntgetIntgetLonggetLonggetShortgetShorthasArrayhashCodehasRemainingisCompositeisDirectisExternalisReadOnlylimitlimitmarkorderorderpositionpositionprepareDisposeprependputputputputputputputputput8BitStringputCharputCharputDoubleputDoubleputFloatputFloatputIntputIntputLongputLongputShortputShortreleaseremainingresetrewindshrinksliceslicesplittoBufferArraytoBufferArraytoBufferArraytoBufferArraytoByteBuffertoByteBuffertoByteBufferArraytoByteBufferArraytoByteBufferArraytoByteBufferArraytoStringtoStringContenttoStringContenttoStringContenttrimtryDisposeunderlyingwrapByteBuffer

Constructor Detail

RecyclableByteBufferWrapperback to summary
private RecyclableByteBufferWrapper(final ByteBuffer underlyingByteBuffer)

Method Detail

disposeback to summary
public void dispose()

Overrides org.glassfish.grizzly.memory.ByteBufferWrapper.dispose.

Implements org.glassfish.grizzly.Buffer.dispose.

Doc from org.glassfish.grizzly.Buffer.dispose.

Notify the allocator that the space for this Buffer is no longer needed. All calls to methods on a Buffer will fail after a call to dispose().

Annotations
@Override
initializeback to summary
private void initialize(final ByteBuffer underlyingByteBuffer)
recycleback to summary
public void recycle()

Implements org.glassfish.grizzly.Cacheable.recycle.

Annotations
@Override
org.glassfish.grizzly.memory back to summary

private final Class HeapMemoryManager.TrimmableHeapBuffer

extends HeapBuffer
implements TrimAware
Class Inheritance
All Implemented Interfaces
org.glassfish.grizzly.memory.AbstractMemoryManager.TrimAware, org.glassfish.grizzly.Cacheable

HeapBuffer implementation, which supports trimming. In other words it's possible to return unused org.glassfish.grizzly.Buffer space to pool.

Field Summary

Modifier and TypeField and Description
private HeapMemoryManager
Inherited from org.glassfish.grizzly.memory.HeapBuffer:
allowBufferDisposebigEndianbyteBuffercapDEBUG_MODEdisposeStackTraceheaplimmarkoffsetorderposreadOnly

Constructor Summary

AccessConstructor and Description
private
TrimmableHeapBuffer(final HeapMemoryManager mm, byte[] heap, int offset, int capacity)

Method Summary

Modifier and TypeMethod and Description
protected HeapBuffer
createHeapBuffer(final int offs, final int capacity)

Overrides org.glassfish.grizzly.memory.HeapBuffer.createHeapBuffer.

Create a new HeapBuffer based on the current heap.

public void
dispose()

Overrides org.glassfish.grizzly.memory.HeapBuffer.dispose.

Implements org.glassfish.grizzly.Buffer.dispose.

Notify the allocator that the space for this Buffer is no longer needed.

pack-priv void
initialize(final HeapMemoryManager mm, final byte[] heap, final int offset, final int length)

public void
public void
trim()

Overrides org.glassfish.grizzly.memory.HeapBuffer.trim.

Implements org.glassfish.grizzly.Buffer.trim.

Trim the buffer by reducing capacity to position, if possible.

Inherited from org.glassfish.grizzly.memory.HeapBuffer:
allowBufferDisposeallowBufferDisposearrayarrayOffsetasReadOnlyBuffercapacitycheckDisposeclearcompactcompareTodumpHexduplicateequalsflipgetgetgetgetgetgetgetChargetChargetDoublegetDoublegetFloatgetFloatgetIntgetIntgetLonggetLonggetShortgetShorthasArrayhashCodehasRemainingisCompositeisDirectisExternalisReadOnlylimitlimitmarkonShareHeaporderorderpositionpositionprepareDisposeprependputputputputputputputputput8BitStringputCharputCharputDoubleputDoubleputFloatputFloatputIntputIntputLongputLongputShortputShortreleaseremainingresetrewindshrinksliceslicesplittoBufferArraytoBufferArraytoBufferArraytoBufferArraytoByteBuffertoByteBuffertoByteBuffer0toByteBufferArraytoByteBufferArraytoByteBufferArraytoByteBufferArraytoStringtoStringContenttoStringContenttoStringContenttryDisposeunderlyingwrapwrap

Field Detail

mmback to summary
private HeapMemoryManager mm

Constructor Detail

TrimmableHeapBufferback to summary
private TrimmableHeapBuffer(final HeapMemoryManager mm, byte[] heap, int offset, int capacity)

Method Detail

createHeapBufferback to summary
protected HeapBuffer createHeapBuffer(final int offs, final int capacity)

Overrides org.glassfish.grizzly.memory.HeapBuffer.createHeapBuffer.

Doc from org.glassfish.grizzly.memory.HeapBuffer.createHeapBuffer.

Create a new HeapBuffer based on the current heap.

Annotations
@Override
disposeback to summary
public void dispose()

Overrides org.glassfish.grizzly.memory.HeapBuffer.dispose.

Implements org.glassfish.grizzly.Buffer.dispose.

Doc from org.glassfish.grizzly.Buffer.dispose.

Notify the allocator that the space for this Buffer is no longer needed. All calls to methods on a Buffer will fail after a call to dispose().

Annotations
@Override
initializeback to summary
pack-priv void initialize(final HeapMemoryManager mm, final byte[] heap, final int offset, final int length)
recycleback to summary
public void recycle()

Implements org.glassfish.grizzly.Cacheable.recycle.

Annotations
@Override
trimback to summary
public void trim()

Overrides org.glassfish.grizzly.memory.HeapBuffer.trim.

Implements org.glassfish.grizzly.Buffer.trim.

Doc from org.glassfish.grizzly.Buffer.trim.

Trim the buffer by reducing capacity to position, if possible. May return without changing capacity. Also resets the position to 0, like flip().

Annotations
@Override