Class TrackedArena
java.lang.Object
express.mvp.myra.transport.memory.TrackedArena
- All Implemented Interfaces:
AutoCloseable
Arena wrapper that integrates with ResourceTracker and NativeMemoryCleaner.
This class wraps a standard Arena with tracking and cleanup support, ensuring memory is properly managed throughout its lifecycle.
Features
- Allocation tracking: All allocations registered with ResourceTracker
- Automatic cleanup: Cleaner registration for GC-triggered release
- Statistics: Tracks total allocations and releases
- Thread safety: Safe for use from multiple threads (if underlying arena allows)
Usage Example
try (TrackedArena arena = TrackedArena.ofShared("BufferPool")) {
MemorySegment buffer = arena.allocate(65536);
// Use buffer...
} // Automatically releases all allocations
Arena Types
ofConfined(String)- Single-thread only, fastestofShared(String)- Multi-thread safewrap(Arena, String)- Wrap an existing arena
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionallocate(long byteSize) Allocates memory from this arena with tracking.allocate(long byteSize, long byteAlignment) Allocates aligned memory from this arena with tracking.voidclose()Closes this arena and releases all memory.Returns the source identifier.longReturns total bytes allocated from this arena.booleanisOpen()Checks if this arena is still open.static TrackedArenaofConfined(String source) Creates a tracked confined arena.static TrackedArenaCreates a tracked shared arena.scope()Returns the underlying arena's scope.toString()static TrackedArenaWraps an existing arena with tracking.
-
Method Details
-
ofConfined
Creates a tracked confined arena.Confined arenas can only be accessed from the thread that created them, but provide the best performance.
- Parameters:
source- identifier for tracking- Returns:
- a new tracked confined arena
-
wrap
Wraps an existing arena with tracking.Note: The wrapped arena's lifecycle is now managed by this wrapper. The original arena should not be closed directly.
- Parameters:
arena- the arena to wrapsource- identifier for tracking- Returns:
- a tracked wrapper
-
allocate
Allocates memory from this arena with tracking.- Parameters:
byteSize- the size in bytes- Returns:
- the allocated memory segment
- Throws:
IllegalStateException- if the arena is closed
-
allocate
Allocates aligned memory from this arena with tracking.- Parameters:
byteSize- the size in bytesbyteAlignment- the required alignment- Returns:
- the allocated memory segment
- Throws:
IllegalStateException- if the arena is closed
-
scope
-
getSource
-
getTotalAllocated
public long getTotalAllocated()Returns total bytes allocated from this arena.- Returns:
- total allocated bytes
-
isOpen
public boolean isOpen()Checks if this arena is still open.- Returns:
- true if open
-
close
public void close()Closes this arena and releases all memory.- Specified by:
closein interfaceAutoCloseable
-
toString
-