Package org.apache.flink.core.memory
Class MemorySegmentFactory
- java.lang.Object
-
- org.apache.flink.core.memory.MemorySegmentFactory
-
@Internal public final class MemorySegmentFactory extends Object
A factory for memory segments (MemorySegment).
-
-
Constructor Summary
Constructors Constructor Description MemorySegmentFactory()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static MemorySegmentallocateOffHeapUnsafeMemory(int size)static MemorySegmentallocateOffHeapUnsafeMemory(int size, Object owner, Runnable customCleanupAction)Allocates an off-heap unsafe memory and creates a new memory segment to represent that memory.static MemorySegmentallocateUnpooledOffHeapMemory(int size)Allocates some unpooled off-heap memory and creates a new memory segment that represents that memory.static MemorySegmentallocateUnpooledOffHeapMemory(int size, Object owner)Allocates some unpooled off-heap memory and creates a new memory segment that represents that memory.static MemorySegmentallocateUnpooledSegment(int size)Allocates some unpooled memory and creates a new memory segment that represents that memory.static MemorySegmentallocateUnpooledSegment(int size, Object owner)Allocates some unpooled memory and creates a new memory segment that represents that memory.static MemorySegmentwrap(byte[] buffer)Creates a new memory segment that targets the given heap memory region.static MemorySegmentwrapCopy(byte[] bytes, int start, int end)Copies the given heap memory region and creates a new memory segment wrapping it.static MemorySegmentwrapInt(int value)Wraps the four bytes representing the given number with aMemorySegment.static MemorySegmentwrapOffHeapMemory(ByteBuffer memory)Creates a memory segment that wraps the off-heap memory backing the given ByteBuffer.
-
-
-
Method Detail
-
wrap
public static MemorySegment wrap(byte[] buffer)
Creates a new memory segment that targets the given heap memory region.This method should be used to turn short lived byte arrays into memory segments.
- Parameters:
buffer- The heap memory region.- Returns:
- A new memory segment that targets the given heap memory region.
-
wrapCopy
public static MemorySegment wrapCopy(byte[] bytes, int start, int end) throws IllegalArgumentException
Copies the given heap memory region and creates a new memory segment wrapping it.- Parameters:
bytes- The heap memory region.start- starting position, inclusiveend- end position, exclusive- Returns:
- A new memory segment that targets a copy of the given heap memory region.
- Throws:
IllegalArgumentException- if start > end or end > bytes.length
-
wrapInt
public static MemorySegment wrapInt(int value)
Wraps the four bytes representing the given number with aMemorySegment.- See Also:
ByteBuffer.putInt(int)
-
allocateUnpooledSegment
public static MemorySegment allocateUnpooledSegment(int size)
Allocates some unpooled memory and creates a new memory segment that represents that memory.This method is similar to
allocateUnpooledSegment(int, Object), but the memory segment will have null as the owner.- Parameters:
size- The size of the memory segment to allocate.- Returns:
- A new memory segment, backed by unpooled heap memory.
-
allocateUnpooledSegment
public static MemorySegment allocateUnpooledSegment(int size, Object owner)
Allocates some unpooled memory and creates a new memory segment that represents that memory.This method is similar to
allocateUnpooledSegment(int), but additionally sets the owner of the memory segment.- Parameters:
size- The size of the memory segment to allocate.owner- The owner to associate with the memory segment.- Returns:
- A new memory segment, backed by unpooled heap memory.
-
allocateUnpooledOffHeapMemory
public static MemorySegment allocateUnpooledOffHeapMemory(int size)
Allocates some unpooled off-heap memory and creates a new memory segment that represents that memory.- Parameters:
size- The size of the off-heap memory segment to allocate.- Returns:
- A new memory segment, backed by unpooled off-heap memory.
-
allocateUnpooledOffHeapMemory
public static MemorySegment allocateUnpooledOffHeapMemory(int size, Object owner)
Allocates some unpooled off-heap memory and creates a new memory segment that represents that memory.- Parameters:
size- The size of the off-heap memory segment to allocate.owner- The owner to associate with the off-heap memory segment.- Returns:
- A new memory segment, backed by unpooled off-heap memory.
-
allocateOffHeapUnsafeMemory
@VisibleForTesting public static MemorySegment allocateOffHeapUnsafeMemory(int size)
-
allocateOffHeapUnsafeMemory
public static MemorySegment allocateOffHeapUnsafeMemory(int size, Object owner, Runnable customCleanupAction)
Allocates an off-heap unsafe memory and creates a new memory segment to represent that memory.Creation of this segment schedules its memory freeing operation when its java wrapping object is about to be garbage collected, similar to
DirectByteBuffer(int). The difference is that this memory allocation is out of option -XX:MaxDirectMemorySize limitation.- Parameters:
size- The size of the off-heap unsafe memory segment to allocate.owner- The owner to associate with the off-heap unsafe memory segment.customCleanupAction- A custom action to run upon calling GC cleaner.- Returns:
- A new memory segment, backed by off-heap unsafe memory.
-
wrapOffHeapMemory
public static MemorySegment wrapOffHeapMemory(ByteBuffer memory)
Creates a memory segment that wraps the off-heap memory backing the given ByteBuffer. Note that the ByteBuffer needs to be a direct ByteBuffer.This method is intended to be used for components which pool memory and create memory segments around long-lived memory regions.
- Parameters:
memory- The byte buffer with the off-heap memory to be represented by the memory segment.- Returns:
- A new memory segment representing the given off-heap memory.
-
-