Package org.glassfish.grizzly.memory
Class HeapMemoryManager
- java.lang.Object
-
- org.glassfish.grizzly.memory.AbstractMemoryManager<HeapBuffer>
-
- org.glassfish.grizzly.memory.HeapMemoryManager
-
- All Implemented Interfaces:
MemoryManager<HeapBuffer>,ThreadLocalPoolProvider,WrapperAware,MonitoringAware<MemoryProbe>
public class HeapMemoryManager extends AbstractMemoryManager<HeapBuffer> implements WrapperAware
- Since:
- 2.0
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.glassfish.grizzly.memory.AbstractMemoryManager
AbstractMemoryManager.TrimAware
-
-
Field Summary
-
Fields inherited from class org.glassfish.grizzly.memory.AbstractMemoryManager
DEFAULT_MAX_BUFFER_SIZE, maxBufferSize, monitoringConfig
-
Fields inherited from interface org.glassfish.grizzly.memory.MemoryManager
DEFAULT_MEMORY_MANAGER
-
-
Constructor Summary
Constructors Constructor Description HeapMemoryManager()HeapMemoryManager(int maxBufferSize)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description HeapBufferallocate(int size)AllocatedBufferof the required size.HeapBufferallocateAtLeast(int size)AllocatedBufferat least of the provided size.protected HeapBufferallocateHeapBuffer(int size)Creates a new HeapBuffer with a a specified size.protected HeapBufferallocateHeapBufferAtLeast(int size)protected ObjectcreateJmxManagementObject()ThreadLocalPoolcreateThreadLocalPool()MonitoringConfig<MemoryProbe>getMonitoringConfig()Return the object associatedMonitoringConfig.HeapBufferreallocate(HeapBuffer oldBuffer, int newSize)ReallocateBufferto a required size.protected HeapBufferreallocateHeapBuffer(HeapBuffer oldHeapBuffer, int newSize)voidrelease(HeapBuffer buffer)ReleaseBuffer.protected voidreleaseHeapBuffer(HeapBuffer heapBuffer)booleanwillAllocateDirect(int size)Return true if nextMemoryManager.allocate(int)orMemoryManager.allocateAtLeast(int)call, made in the current thread for the given memory size, going to return aBufferbased on directByteBuffer, or false otherwise.HeapBufferwrap(byte[] data)ReturnsBuffer, which wraps the byte array.HeapBufferwrap(byte[] data, int offset, int length)ReturnsBuffer, which wraps the part of byte array with specific offset and length.HeapBufferwrap(String s)HeapBufferwrap(String s, Charset charset)Bufferwrap(ByteBuffer byteBuffer)ReturnsBuffer, which wraps theByteBuffer.-
Methods inherited from class org.glassfish.grizzly.memory.AbstractMemoryManager
allocateFromPool, getMaxBufferSize, getReadyThreadBufferSize, getThreadLocalPool
-
-
-
-
Method Detail
-
allocate
public HeapBuffer allocate(int size)
AllocatedBufferof the required size.- Specified by:
allocatein interfaceMemoryManager<HeapBuffer>- Parameters:
size-Buffersize to be allocated.- Returns:
- allocated
Buffer.
-
allocateAtLeast
public HeapBuffer allocateAtLeast(int size)
AllocatedBufferat 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.- Specified by:
allocateAtLeastin interfaceMemoryManager<HeapBuffer>- Parameters:
size- the minBuffersize to be allocated.- Returns:
- allocated
Buffer.
-
reallocate
public HeapBuffer reallocate(HeapBuffer oldBuffer, int newSize)
ReallocateBufferto a required size. Implementation may choose the way, how reallocation could be done, either by allocating newBufferof required size and copying oldBuffercontent there, or perform more complex logic related to memory pooling etc.- Specified by:
reallocatein interfaceMemoryManager<HeapBuffer>- Parameters:
oldBuffer- oldBufferto be reallocated.newSize- newBufferrequired size.- Returns:
- reallocated
Buffer.
-
release
public void release(HeapBuffer buffer)
ReleaseBuffer. Implementation may ignore releasing and let JVM Garbage collector to take care about theBuffer, or returnBufferto pool, in case of more complex MemoryManager implementation.- Specified by:
releasein interfaceMemoryManager<HeapBuffer>- Parameters:
buffer-Bufferto be released.
-
willAllocateDirect
public boolean willAllocateDirect(int size)
Return true if nextMemoryManager.allocate(int)orMemoryManager.allocateAtLeast(int)call, made in the current thread for the given memory size, going to return aBufferbased on directByteBuffer, or false otherwise.- Specified by:
willAllocateDirectin interfaceMemoryManager<HeapBuffer>- Returns:
-
getMonitoringConfig
public MonitoringConfig<MemoryProbe> getMonitoringConfig()
Return the object associatedMonitoringConfig.- Specified by:
getMonitoringConfigin interfaceMonitoringAware<MemoryProbe>- Returns:
- the object associated
MonitoringConfig.
-
createThreadLocalPool
public ThreadLocalPool createThreadLocalPool()
- Specified by:
createThreadLocalPoolin interfaceThreadLocalPoolProvider- Returns:
- a new
ThreadLocalPoolimplementation. This method must return a newThreadLocalPoolinstance per invocation.
-
createJmxManagementObject
protected Object createJmxManagementObject()
- Specified by:
createJmxManagementObjectin classAbstractMemoryManager<HeapBuffer>- Returns:
- the JMX
Objectused to register/deregister with the JMX runtime.
-
wrap
public HeapBuffer wrap(byte[] data)
ReturnsBuffer, which wraps the byte array.- Specified by:
wrapin interfaceWrapperAware- Parameters:
data- byte array to wrap- Returns:
Bufferwrapper on top of passed byte array.
-
wrap
public HeapBuffer wrap(byte[] data, int offset, int length)
ReturnsBuffer, which wraps the part of byte array with specific offset and length.- Specified by:
wrapin interfaceWrapperAware- Parameters:
data- byte array to wrapoffset- byte buffer offsetlength- byte buffer length- Returns:
Bufferwrapper on top of passed byte array.
-
wrap
public HeapBuffer wrap(String s)
- Specified by:
wrapin interfaceWrapperAware- Parameters:
s-String- Returns:
Bufferwrapper on top of passedString.
-
wrap
public HeapBuffer wrap(String s, Charset charset)
-
wrap
public Buffer wrap(ByteBuffer byteBuffer)
ReturnsBuffer, which wraps theByteBuffer.- Specified by:
wrapin interfaceWrapperAware- Parameters:
byteBuffer-ByteBufferto wrap- Returns:
Bufferwrapper on top of passedByteBuffer.
-
allocateHeapBuffer
protected HeapBuffer allocateHeapBuffer(int size)
Creates a new HeapBuffer with a a specified size.- Parameters:
size- size of buffer created- Returns:
-
allocateHeapBufferAtLeast
protected HeapBuffer allocateHeapBufferAtLeast(int size)
-
reallocateHeapBuffer
protected HeapBuffer reallocateHeapBuffer(HeapBuffer oldHeapBuffer, int newSize)
-
releaseHeapBuffer
protected final void releaseHeapBuffer(HeapBuffer heapBuffer)
-
-