Module java.base

Interface MemorySegment.Scope

Enclosing interface:
MemorySegment

public static sealed interface MemorySegment.Scope
A scope models the lifetime of all the memory segments associated with it.

That is, a memory segment cannot be accessed if its associated scope is not alive. Scope instances can be compared for equality. That is, two scopes are considered equal if they denote the same lifetime.

The lifetime of a memory segment can be either unbounded or bounded. An unbounded lifetime is modeled with the global scope. The global scope is always alive. As such, a segment associated with the global scope features trivial temporal bounds and is always accessible. Segments associated with the global scope are:

Conversely, a bounded lifetime is modeled with a segment scope that can be invalidated, either explicitly, or automatically, by the garbage collector. A segment scope that is invalidated automatically is an automatic scope. An automatic scope is always alive as long as it is reachable. Segments associated with an automatic scope are:

If two memory segments are obtained from the same buffer or array, the automatic scopes associated with said segments are considered equal, as the two segments have the same lifetime:
byte[] arr = new byte[10];
MemorySegment segment1 = MemorySegment.ofArray(arr);
MemorySegment segment2 = MemorySegment.ofArray(arr);
assert segment1.scope().equals(segment2.scope());