Class TrackedArena

java.lang.Object
express.mvp.myra.transport.memory.TrackedArena
All Implemented Interfaces:
AutoCloseable

public final class TrackedArena extends Object implements 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

See Also:
  • Method Details

    • ofConfined

      public static TrackedArena ofConfined(String source)
      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
    • ofShared

      public static TrackedArena ofShared(String source)
      Creates a tracked shared arena.

      Shared arenas can be accessed from any thread, suitable for concurrent use.

      Parameters:
      source - identifier for tracking
      Returns:
      a new tracked shared arena
    • wrap

      public static TrackedArena wrap(Arena arena, String source)
      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 wrap
      source - identifier for tracking
      Returns:
      a tracked wrapper
    • allocate

      public MemorySegment allocate(long byteSize)
      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

      public MemorySegment allocate(long byteSize, long byteAlignment)
      Allocates aligned memory from this arena with tracking.
      Parameters:
      byteSize - the size in bytes
      byteAlignment - the required alignment
      Returns:
      the allocated memory segment
      Throws:
      IllegalStateException - if the arena is closed
    • scope

      public MemorySegment.Scope scope()
      Returns the underlying arena's scope.
      Returns:
      the scope
    • getSource

      public String getSource()
      Returns the source identifier.
      Returns:
      the source
    • 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:
      close in interface AutoCloseable
    • toString

      public String toString()
      Overrides:
      toString in class Object