- All Superinterfaces:
AutoCloseable
,SegmentAllocatorPREVIEW
Arena
is a preview API of the Java platform.
An arena has a scope, called the arena scope. When the arena is closed, the arena scope is no longer alivePREVIEW. As a result, all the segments associated with the arena scope are invalidated, safely and atomically, their backing memory regions are deallocated (where applicable) and can no longer be accessed after the arena is closed:
try (Arena arena = Arena.openConfined()) {
MemorySegment segment = MemorySegment.allocateNative(100, arena.scope());
...
} // memory released here
SegmentAllocator
PREVIEW. All the segments allocated by the
arena are associated with the arena scope. This makes arenas extremely useful when interacting with foreign code, as shown below:
try (Arena arena = Arena.openConfined()) {
MemorySegment nativeArray = arena.allocateArray(ValueLayout.JAVA_INT, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
MemorySegment nativeString = arena.allocateUtf8String("Hello!");
MemorySegment upcallStub = linker.upcallStub(handle, desc, arena.scope());
...
} // memory released here
Safety and thread-confinement
Arenas provide strong temporal safety guarantees: a memory segment allocated by an arena cannot be accessed after the arena has been closed. The cost of providing this guarantee varies based on the number of threads that have access to the memory segments allocated by the arena. For instance, if an arena is always created and closed by one thread, and the memory segments associated with the arena's scope are always accessed by that same thread, then ensuring correctness is trivial.Conversely, if an arena allocates segments that can be accessed by multiple threads, or if the arena can be closed by a thread other than the accessing thread, then ensuring correctness is much more complex. For example, a segment allocated with the arena might be accessed while another thread attempts, concurrently, to close the arena. To provide the strong temporal safety guarantee without forcing every client, even simple ones, to incur a performance impact, arenas are divided into thread-confined arenas, and shared arenas.
Confined arenas, support strong thread-confinement guarantees. Upon creation, they are assigned an
owner thread, typically the thread which initiated the creation operation.
The segments created by a confined arena can only be accessedPREVIEW
by the owner thread. Moreover, any attempt to close the confined arena from a thread other than the owner thread will
fail with WrongThreadException
.
Shared arenas, on the other hand, have no owner thread. The segments created by a shared arena can be accessedPREVIEW by any thread. This might be useful when multiple threads need to access the same memory segment concurrently (e.g. in the case of parallel processing). Moreover, a shared arena can be closed by any thread.
- Since:
- 20
-
Method Summary
Modifier and TypeMethodDescriptiondefault MemorySegmentPREVIEW
allocate
(long byteSize, long byteAlignment) Returns a native memory segment with the given size (in bytes) and alignment constraint (in bytes).void
close()
Closes this arena.boolean
isCloseableBy
(Thread thread) Returnstrue
if the provided thread can close this arena.Returns a new confined arena, owned by the current thread.Returns a new shared arena.scope()
Returns the arena scope.Methods declared in interface java.lang.foreign.SegmentAllocatorPREVIEW
allocate, allocate, allocate, allocate, allocate, allocate, allocate, allocate, allocate, allocate, allocateArray, allocateArray, allocateArray, allocateArray, allocateArray, allocateArray, allocateArray, allocateArray, allocateUtf8String
-
Method Details
-
allocate
Returns a native memory segment with the given size (in bytes) and alignment constraint (in bytes). The returned segment is associated with the arena scope. The segment'saddress
PREVIEW is the starting address of the allocated off-heap memory region backing the segment, and the address is aligned according the provided alignment constraint.- Specified by:
allocate
in interfaceSegmentAllocatorPREVIEW
- Implementation Requirements:
- The default implementation of this method is equivalent to the following code:
MemorySegment.allocateNative(bytesSize, byteAlignment, scope());
S1, S2
returned by this method, the following invariant must hold:S1.overlappingSlice(S2).isEmpty() == true
- Parameters:
byteSize
- the size (in bytes) of the off-heap memory block backing the native memory segment.byteAlignment
- the alignment constraint (in bytes) of the off-heap region of memory backing the native memory segment.- Returns:
- a new native memory segment.
- Throws:
IllegalArgumentException
- ifbytesSize < 0
,alignmentBytes <= 0
, or ifalignmentBytes
is not a power of 2.IllegalStateException
- if the arena has already been closed.WrongThreadException
- if this method is called from a threadT
, such thatscope().isAccessibleBy(T) == false
.- See Also:
-
scope
SegmentScopePREVIEW scope()Returns the arena scope.- Returns:
- the arena scope
-
close
void close()Closes this arena. If this method completes normally, the arena scope is no longer alivePREVIEW, and all the memory segments associated with it can no longer be accessed. Furthermore, any off-heap region of memory backing the segments associated with that scope are also released.- Specified by:
close
in interfaceAutoCloseable
- API Note:
- This operation is not idempotent; that is, closing an already closed arena always results in an exception being thrown. This reflects a deliberate design choice: failure to close an arena might reveal a bug in the underlying application logic.
- Throws:
IllegalStateException
- if the arena has already been closed.IllegalStateException
- if the arena scope is kept alivePREVIEW.WrongThreadException
- if this method is called from a threadT
, such thatisCloseableBy(T) == false
.- See Also:
-
isCloseableBy
Returnstrue
if the provided thread can close this arena.- Parameters:
thread
- the thread to be tested.- Returns:
true
if the provided thread can close this arena
-
openConfined
Returns a new confined arena, owned by the current thread.- Returns:
- a new confined arena, owned by the current thread
-
Arena
when preview features are enabled.