Top Description Inners Fields Methods
java.lang.foreign

public sealed Interface MemorySegment

Known Direct Implementers
jdk.internal.foreign.AbstractMemorySegmentImpl
Imports
java.io.UncheckedIOException, java.lang.foreign.ValueLayout.OfInt, java.nio.Buffer, .ByteBuffer, .ByteOrder, .CharBuffer, java.nio.channels.FileChannel, .FileChannel.MapMode, java.nio.charset.Charset, .StandardCharsets, java.util.Arrays, .Objects, .Optional, .Spliterator, java.util.function.Consumer, java.util.stream.Stream, jdk.internal.foreign.AbstractMemorySegmentImpl, .MemorySessionImpl, .SegmentFactories, jdk.internal.javac.Restricted, jdk.internal.reflect.CallerSensitive, jdk.internal.vm.annotation.ForceInline

A memory segment provides access to a contiguous region of memory.

There are two kinds of memory segments:

Heap segments can be obtained by calling one of the MemorySegment#ofArray(int[]) factory methods. These methods return a memory segment backed by the on-heap region that holds the specified Java array.

Native segments can be obtained by calling one of the Arena#allocate(long, long) factory methods, which return a memory segment backed by a newly allocated off-heap region with the given size and aligned to the given alignment constraint. Alternatively, native segments can be obtained by mapping a file into a new off-heap region (in some systems, this operation is sometimes referred to as mmap). Segments obtained in this way are called mapped segments, and their contents can be persisted and loaded to and from the underlying memory-mapped file.

Both kinds of segments are read and written using the same methods, known as access operations. An access operation on a memory segment always and only provides access to the region for which the segment was obtained.

Characteristics of memory segments

Every memory segment has an address, expressed as a long value. The nature of a segment's address depends on the kind of the segment:

Every memory segment has a maximum byte alignment, expressed as a long value. The maximum alignment is always a power of two, derived from the segment address, and the segment type, as explained in more detail below.

Every memory segment has a size. The size of a heap segment is derived from the Java array from which it is obtained. This size is predictable across Java runtimes. The size of a native segment is either passed explicitly (as in Arena#allocate(long, long)) or derived from a MemoryLayout (as in Arena#allocate(MemoryLayout)). The size of a memory segment is typically a positive number but may be zero, but never negative.

The address and size of a memory segment jointly ensure that access operations on the segment cannot fall outside the boundaries of the region of memory that backs the segment. That is, a memory segment has spatial bounds.

Every memory segment is associated with a scope. This ensures that access operations on a memory segment cannot occur when the region of memory that backs the memory segment is no longer available (e.g., after the scope associated with the accessed memory segment is no longer alive). That is, a memory segment has temporal bounds.

Finally, access operations on a memory segment can be subject to additional thread-confinement checks. Heap segments can be accessed from any thread. Conversely, native segments can only be accessed compatibly with the confinement characteristics of the arena used to obtain them.

Accessing memory segments

A memory segment can be read or written using various access operations provided in this class (e.g. get(ValueLayout.OfInt, long)). Each access operation takes a value layout, which specifies the size and shape of the value, and an offset, expressed in bytes. For instance, to read an int from a segment, using default endianness, the following code can be used:
MemorySegment segment = ... int value = segment.get(ValueLayout.JAVA_INT, 0);
MemorySegment segment = ...
int value = segment.get(ValueLayout.JAVA_INT, 0);
If the value to be read is stored in memory using big-endian encoding, the access operation can be expressed as follows:
int value = segment.get(ValueLayout.JAVA_INT.withOrder(BIG_ENDIAN), 0);
int value = segment.get(ValueLayout.JAVA_INT.withOrder(BIG_ENDIAN), 0);
Access operations on memory segments are implemented using var handles. The ValueLayout#varHandle() method can be used to obtain a var handle that can be used to get/set values represented by the given value layout on a memory segment at the given offset:
VarHandle intAtOffsetHandle = ValueLayout.JAVA_INT.varHandle(); // (MemorySegment, long) int value = (int) intAtOffsetHandle.get(segment, 10L); // segment.get(ValueLayout.JAVA_INT, 10L)
VarHandle intAtOffsetHandle = ValueLayout.JAVA_INT.varHandle(); // (MemorySegment, long)
int value = (int) intAtOffsetHandle.get(segment, 10L);          // segment.get(ValueLayout.JAVA_INT, 10L)
Alternatively, a var handle that can be used to access an element of an int array at a given logical index can be created as follows:
VarHandle intAtOffsetAndIndexHandle = ValueLayout.JAVA_INT.arrayElementVarHandle(); // (MemorySegment, long, long) int value = (int) intAtOffsetAndIndexHandle.get(segment, 2L, 3L); // segment.get(ValueLayout.JAVA_INT, 2L + (3L * 4L))
VarHandle intAtOffsetAndIndexHandle =
        ValueLayout.JAVA_INT.arrayElementVarHandle();             // (MemorySegment, long, long)
int value = (int) intAtOffsetAndIndexHandle.get(segment, 2L, 3L); // segment.get(ValueLayout.JAVA_INT, 2L + (3L * 4L))

Clients can also drop the base offset parameter, in order to make the access expression simpler. This can be used to implement access operations such as getAtIndex(OfInt, long):

VarHandle intAtIndexHandle = MethodHandles.insertCoordinates(intAtOffsetAndIndexHandle, 1, 0L); // (MemorySegment, long) int value = (int) intAtIndexHandle.get(segment, 3L); // segment.getAtIndex(ValueLayout.JAVA_INT, 3L);
VarHandle intAtIndexHandle =
        MethodHandles.insertCoordinates(intAtOffsetAndIndexHandle, 1, 0L); // (MemorySegment, long)
int value = (int) intAtIndexHandle.get(segment, 3L);                       // segment.getAtIndex(ValueLayout.JAVA_INT, 3L);
Var handles for more complex access expressions (e.g. struct field access, pointer dereference) can be created directly from memory layouts, using layout paths.

Slicing memory segments

Memory segments support slicing. Slicing a memory segment returns a new memory segment that is backed by the same region of memory as the original. The address of the sliced segment is derived from the address of the original segment, by adding an offset (expressed in bytes). The size of the sliced segment is either derived implicitly (by subtracting the specified offset from the size of the original segment), or provided explicitly. In other words, a sliced segment has stricter spatial bounds than those of the original segment:
Arena arena = ... MemorySegment segment = arena.allocate(100); MemorySegment slice = segment.asSlice(50, 10); slice.get(ValueLayout.JAVA_INT, 20); // Out of bounds! arena.close(); slice.get(ValueLayout.JAVA_INT, 0); // Already closed!
Arena arena = ...
MemorySegment segment = arena.allocate(100);
MemorySegment slice = segment.asSlice(50, 10);
slice.get(ValueLayout.JAVA_INT, 20); // Out of bounds!
arena.close();
slice.get(ValueLayout.JAVA_INT, 0); // Already closed!
The above code creates a native segment that is 100 bytes long; then, it creates a slice that starts at offset 50 of segment, and is 10 bytes long. That is, the address of the slice is segment.address() + 50, and its size is 10. As a result, attempting to read an int value at offset 20 of the slice segment will result in an exception. The temporal bounds of the original segment is inherited by its slices; that is, when the scope associated with segment is no longer alive, slice will also become inaccessible.

A client might obtain a Stream from a segment, which can then be used to slice the segment (according to a given element layout) and even allow multiple threads to work in parallel on disjoint segment slices (to do this, the segment has to be accessible from multiple threads). The following code can be used to sum all int values in a memory segment in parallel:

try (Arena arena = Arena.ofShared()) { SequenceLayout SEQUENCE_LAYOUT = MemoryLayout.sequenceLayout(1024, ValueLayout.JAVA_INT); MemorySegment segment = arena.allocate(SEQUENCE_LAYOUT); int sum = segment.elements(ValueLayout.JAVA_INT).parallel() .mapToInt(s -> s.get(ValueLayout.JAVA_INT, 0)) .sum(); }
try (Arena arena = Arena.ofShared()) {
    SequenceLayout SEQUENCE_LAYOUT = MemoryLayout.sequenceLayout(1024, ValueLayout.JAVA_INT);
    MemorySegment segment = arena.allocate(SEQUENCE_LAYOUT);
    int sum = segment.elements(ValueLayout.JAVA_INT).parallel()
                     .mapToInt(s -> s.get(ValueLayout.JAVA_INT, 0))
                     .sum();
}

Alignment

Access operations on a memory segment are constrained not only by the spatial and temporal bounds of the segment, but also by the alignment constraint of the value layout specified to the operation. An access operation can access only those offsets in the segment that denote addresses in physical memory that are aligned according to the layout. An address in physical memory is aligned according to a layout if the address is an integer multiple of the layout's alignment constraint. For example, the address 1000 is aligned according to an 8-byte alignment constraint (because 1000 is an integer multiple of 8), and to a 4-byte alignment constraint, and to a 2-byte alignment constraint; in contrast, the address 1004 is aligned according to a 4-byte alignment constraint, and to a 2-byte alignment constraint, but not to an 8-byte alignment constraint. Access operations are required to respect alignment because it can impact the performance of access operations, and can also determine which access operations are available at a given physical address. For instance, atomic access operations operations using java.lang.invoke.VarHandle are only permitted at aligned addresses. In addition, alignment applies to an access operation whether the segment being accessed is a native segment or a heap segment.

If the segment being accessed is a native segment, then its address in physical memory can be combined with the offset to obtain the target address in physical memory. The pseudo-function below demonstrates this:

boolean isAligned(MemorySegment segment, long offset, MemoryLayout layout) { return ((segment.address() + offset) % layout.byteAlignment()) == 0; }
boolean isAligned(MemorySegment segment, long offset, MemoryLayout layout) {
  return ((segment.address() + offset) % layout.byteAlignment()) == 0;
}
For example:

The alignment constraint used to access a segment is typically dictated by the shape of the data structure stored in the segment. For example, if the programmer wishes to store a sequence of 8-byte values in a native segment, then the segment should be allocated by specifying an 8-byte alignment constraint, either via Arena#allocate(long, long) or Arena#allocate(MemoryLayout). These factories ensure that the off-heap region of memory backing the returned segment has a starting address that is 8-byte aligned. Subsequently, the programmer can access the segment at the offsets of interest -- 0, 8, 16, 24, etc -- in the knowledge that every such access is aligned.

If the segment being accessed is a heap segment, then determining whether access is aligned is more complex. The address of the segment in physical memory is not known and is not even fixed (it may change when the segment is relocated during garbage collection). This means that the address cannot be combined with the specified offset to determine a target address in physical memory. Since the alignment constraint always refers to alignment of addresses in physical memory, it is not possible in principle to determine if any offset in a heap segment is aligned. For example, suppose the programmer chooses an 8-byte alignment constraint and tries to access offset 16 in a heap segment. If the heap segment's address 0 corresponds to physical address 1000, then the target address (1016) would be aligned, but if address 0 corresponds to physical address 1004, then the target address (1020) would not be aligned. It is undesirable to allow access to target addresses that are aligned according to the programmer's chosen alignment constraint, but might not be predictably aligned in physical memory (e.g. because of platform considerations and/or garbage collection behavior).

In practice, the Java runtime lays out arrays in memory so that each n-byte element occurs at an n-byte aligned physical address. The runtime preserves this invariant even if the array is relocated during garbage collection. Access operations rely on this invariant to determine if the specified offset in a heap segment refers to an aligned address in physical memory. For example:

In other words, heap segments feature a maximum alignment which is derived from the size of the elements of the Java array backing the segment, as shown in the following table:

Maximum alignment of heap segments
Array type (of backing region) Maximum supported alignment (in bytes)
boolean[] ValueLayout.JAVA_BOOLEAN.byteAlignment()
byte[] ValueLayout.JAVA_BYTE.byteAlignment()
char[] ValueLayout.JAVA_CHAR.byteAlignment()
short[] ValueLayout.JAVA_SHORT.byteAlignment()
int[] ValueLayout.JAVA_INT.byteAlignment()
float[] ValueLayout.JAVA_FLOAT.byteAlignment()
long[] ValueLayout.JAVA_LONG.byteAlignment()
double[] ValueLayout.JAVA_DOUBLE.byteAlignment()
Heap segments can only be accessed using a layout whose alignment is smaller or equal to the maximum alignment associated with the heap segment. Attempting to access a heap segment using a layout whose alignment is greater than the maximum alignment associated with the heap segment will fail, as demonstrated in the following example:
MemorySegment byteSegment = MemorySegment.ofArray(new byte[10]); byteSegment.get(ValueLayout.JAVA_INT, 0); // fails: ValueLayout.JAVA_INT.byteAlignment() > ValueLayout.JAVA_BYTE.byteAlignment()
MemorySegment byteSegment = MemorySegment.ofArray(new byte[10]);
byteSegment.get(ValueLayout.JAVA_INT, 0); // fails: ValueLayout.JAVA_INT.byteAlignment() > ValueLayout.JAVA_BYTE.byteAlignment()
In such circumstances, clients have two options. They can use a heap segment backed by a different array type (e.g. long[]), capable of supporting greater maximum alignment. More specifically, the maximum alignment associated with long[] is set to ValueLayout.JAVA_LONG.byteAlignment(), which is 8 bytes:
MemorySegment longSegment = MemorySegment.ofArray(new long[10]); longSegment.get(ValueLayout.JAVA_INT, 0); // ok: ValueLayout.JAVA_INT.byteAlignment() <= ValueLayout.JAVA_LONG.byteAlignment()
MemorySegment longSegment = MemorySegment.ofArray(new long[10]);
longSegment.get(ValueLayout.JAVA_INT, 0); // ok: ValueLayout.JAVA_INT.byteAlignment() <= ValueLayout.JAVA_LONG.byteAlignment()
Alternatively, they can invoke the access operation with an unaligned layout. All unaligned layout constants (e.g. ValueLayout#JAVA_INT_UNALIGNED) have their alignment constraint set to 1:
MemorySegment byteSegment = MemorySegment.ofArray(new byte[10]); byteSegment.get(ValueLayout.JAVA_INT_UNALIGNED, 0); // ok: ValueLayout.JAVA_INT_UNALIGNED.byteAlignment() == ValueLayout.JAVA_BYTE.byteAlignment()
MemorySegment byteSegment = MemorySegment.ofArray(new byte[10]);
byteSegment.get(ValueLayout.JAVA_INT_UNALIGNED, 0); // ok: ValueLayout.JAVA_INT_UNALIGNED.byteAlignment() == ValueLayout.JAVA_BYTE.byteAlignment()
Clients can use the MemorySegment#maxByteAlignment() method to check if a memory segment supports the alignment constraint of a memory layout, as follows:
MemoryLayout layout = ... MemorySegment segment = ... boolean isAligned = segment.maxByteAlignment() >= layout.byteAlignment();
MemoryLayout layout = ...
MemorySegment segment = ...
boolean isAligned = segment.maxByteAlignment() >= layout.byteAlignment();

Zero-length memory segments

When interacting with foreign functions, it is common for those functions to allocate a region of memory and return a pointer to that region. Modeling the region of memory with a memory segment is challenging because the Java runtime has no insight into the size of the region. Only the address of the start of the region, stored in the pointer, is available. For example, a C function with return type char* might return a pointer to a region containing a single char value, or to a region containing an array of char values, where the size of the array might be provided in a separate parameter. The size of the array is not readily apparent to the code calling the foreign function and hoping to use its result. In addition to having no insight into the size of the region of memory backing a pointer returned from a foreign function, it also has no insight into the lifetime intended for said region of memory by the foreign function that allocated it.

The MemorySegment API uses zero-length memory segments to represent:

The address of the zero-length segment is the address stored in the pointer. The spatial and temporal bounds of the zero-length segment are as follows:

To demonstrate how clients can work with zero-length memory segments, consider the case of a client that wants to read a pointer from some memory segment. This can be done via the MemorySegment#get(AddressLayout, long) access method. This method accepts an address layout (e.g. ValueLayout#ADDRESS), the layout of the pointer to be read. For instance, on a 64-bit platform, the size of an address layout is 8 bytes. The access operation also accepts an offset, expressed in bytes, which indicates the position (relative to the start of the memory segment) at which the pointer is stored. The access operation returns a zero-length native memory segment, backed by a region of memory whose starting address is the 64-bit value read at the specified offset.

The returned zero-length memory segment cannot be accessed directly by the client: since the size of the segment is zero, any access operation would result in out-of-bounds access. Instead, the client must, unsafely, assign new spatial bounds to the zero-length memory segment. This can be done via the reinterpret(long) method, as follows:

MemorySegment z = segment.get(ValueLayout.ADDRESS, ...); // size = 0 MemorySegment ptr = z.reinterpret(16); // size = 16 int x = ptr.getAtIndex(ValueLayout.JAVA_INT, 3); // ok
MemorySegment z = segment.get(ValueLayout.ADDRESS, ...);   // size = 0
MemorySegment ptr = z.reinterpret(16);                     // size = 16
int x = ptr.getAtIndex(ValueLayout.JAVA_INT, 3);           // ok

In some cases, the client might additionally want to assign new temporal bounds to a zero-length memory segment. This can be done via the reinterpret(long, Arena, Consumer) method, which returns a new native segment with the desired size and the same temporal bounds as those of the provided arena:

MemorySegment ptr = null; try (Arena arena = Arena.ofConfined()) { MemorySegment z = segment.get(ValueLayout.ADDRESS, ...); // size = 0, scope = always alive ptr = z.reinterpret(16, arena, null); // size = 4, scope = arena.scope() int x = ptr.getAtIndex(ValueLayout.JAVA_INT, 3); // ok } int x = ptr.getAtIndex(ValueLayout.JAVA_INT, 3); // throws IllegalStateException
MemorySegment ptr = null;
try (Arena arena = Arena.ofConfined()) {
      MemorySegment z = segment.get(ValueLayout.ADDRESS, ...);    // size = 0, scope = always alive
      ptr = z.reinterpret(16, arena, null);                       // size = 4, scope = arena.scope()
      int x = ptr.getAtIndex(ValueLayout.JAVA_INT, 3);            // ok
}
int x = ptr.getAtIndex(ValueLayout.JAVA_INT, 3);                  // throws IllegalStateException
Alternatively, if the size of the region of memory backing the zero-length memory segment is known statically, the client can overlay a target layout on the address layout used when reading a pointer. The target layout is then used to dynamically expand the size of the native memory segment returned by the access operation so that the size of the segment is the same as the size of the target layout . In other words, the returned segment is no longer a zero-length memory segment, and the pointer it represents can be dereferenced directly:
AddressLayout intArrPtrLayout = ValueLayout.ADDRESS.withTargetLayout( MemoryLayout.sequenceLayout(4, ValueLayout.JAVA_INT)); // layout for int (*ptr)[4] MemorySegment ptr = segment.get(intArrPtrLayout, ...); // size = 16 int x = ptr.getAtIndex(ValueLayout.JAVA_INT, 3); // ok
AddressLayout intArrPtrLayout = ValueLayout.ADDRESS.withTargetLayout(
        MemoryLayout.sequenceLayout(4, ValueLayout.JAVA_INT)); // layout for int (*ptr)[4]
MemorySegment ptr = segment.get(intArrPtrLayout, ...);         // size = 16
int x = ptr.getAtIndex(ValueLayout.JAVA_INT, 3);               // ok

All the methods that can be used to manipulate zero-length memory segments (reinterpret(long), reinterpret(Arena, Consumer), reinterpret(long, Arena, Consumer) and AddressLayout#withTargetLayout(MemoryLayout)) are restricted methods, and should be used with caution: assigning a segment incorrect spatial and/or temporal bounds could result in a VM crash when attempting to access the memory segment.

Implementation Specification

Implementations of this interface are immutable, thread-safe and value-based.

Since
22

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
MemorySegment.Scope

A scope models the lifetime of all the memory segments associated with it.

Field Summary

Modifier and TypeField and Description
public static final MemorySegment
NULL

A zero-length native segment modelling the NULL address.

Method Summary

Modifier and TypeMethod and Description
public long

Returns:

the address of this memory segment
address
()

Returns the address of this memory segment.

public ByteBuffer

Returns:

a ByteBuffer view of this memory segment
asByteBuffer
()

Wraps this segment in a ByteBuffer.

public Optional<MemorySegment>

Returns:

a slice of this segment (where overlapping occurs)
asOverlappingSlice
(MemorySegment
the segment to test for an overlap with this segment
other
)

Returns a slice of this segment that is the overlap between this and the provided segment.

public MemorySegment

Returns:

a read-only view of this segment
asReadOnly
()

Returns a read-only view of this segment.

public MemorySegment

Returns:

a slice of this memory segment
asSlice
(long
The new segment base offset (relative to the address of this segment), specified in bytes
offset
,
long
The new segment size, specified in bytes
newSize
)

Returns a slice of this memory segment, at the given offset.

public MemorySegment

Returns:

a slice of this memory segment
asSlice
(long
The new segment base offset (relative to the address of this segment), specified in bytes
offset
,
long
The new segment size, specified in bytes
newSize
,
long
The alignment constraint (in bytes) of the returned slice
byteAlignment
)

Returns a slice of this memory segment, at the given offset, with the provided alignment constraint.

public MemorySegment

Returns:

a slice of this memory segment
asSlice
(long
The new segment base offset (relative to the address of this segment), specified in bytes
offset
,
MemoryLayout
The layout of the segment slice
layout
)

Returns a slice of this memory segment with the given layout, at the given offset.

public MemorySegment

Returns:

a slice of this memory segment
asSlice
(long
The new segment base offset (relative to the address of this segment), specified in bytes
offset
)

Returns a slice of this memory segment, at the given offset.

public long

Returns:

the size (in bytes) of this memory segment
byteSize
()

Returns the size (in bytes) of this memory segment.

public static void
copy(MemorySegment
the source segment
srcSegment
,
long
the starting offset, in bytes, of the source segment
srcOffset
,
MemorySegment
the destination segment
dstSegment
,
long
the starting offset, in bytes, of the destination segment
dstOffset
,
long
the number of bytes to be copied
bytes
)

Performs a bulk copy from source segment to destination segment.

public static void
copy(MemorySegment
the source segment
srcSegment
,
ValueLayout
the element layout associated with the source segment
srcElementLayout
,
long
the starting offset, in bytes, of the source segment
srcOffset
,
MemorySegment
the destination segment
dstSegment
,
ValueLayout
the element layout associated with the destination segment
dstElementLayout
,
long
the starting offset, in bytes, of the destination segment
dstOffset
,
long
the number of elements to be copied
elementCount
)

Performs a bulk copy from source segment to destination segment.

public static void
copy(MemorySegment
the source segment
srcSegment
,
ValueLayout
the source element layout. If the byte order associated with the layout is different from the native order, a byte swap operation will be performed on each array element
srcLayout
,
long
the starting offset, in bytes, of the source segment
srcOffset
,
Object
the destination array
dstArray
,
int
the starting index of the destination array
dstIndex
,
int
the number of array elements to be copied
elementCount
)

Copies a number of elements from a source memory segment to a destination array.

public static void
copy(Object
the source array
srcArray
,
int
the starting index of the source array
srcIndex
,
MemorySegment
the destination segment
dstSegment
,
ValueLayout
the destination element layout. If the byte order associated with the layout is different from the native order, a byte swap operation will be performed on each array element.
dstLayout
,
long
the starting offset, in bytes, of the destination segment
dstOffset
,
int
the number of array elements to be copied
elementCount
)

Copies a number of elements from a source array to a destination memory segment.

public MemorySegment

Returns:

this segment
copyFrom
(MemorySegment
the source segment
src
)

Performs a bulk copy from the given source segment to this segment.

public Stream<MemorySegment>

Returns:

a sequential Stream over disjoint slices in this segment
elements
(MemoryLayout
the layout to be used for splitting
elementLayout
)

Returns a sequential Stream over disjoint slices (whose size matches that of the specified layout) in this segment.

public boolean

Returns:

true if the specified object is equal to this memory segment
equals
(Object
the object to be compared for equality with this memory segment
that
)

Compares the specified object with this memory segment for equality.

public MemorySegment

Returns:

this memory segment
fill
(byte
the value to write into this segment
value
)

Fills the contents of this memory segment with the given value.

public void
force()

Forces any changes made to the contents of this mapped segment to be written to the storage device described by the mapped segment's file descriptor.

public byte

Returns:

a byte value read from this segment
get
(ValueLayout.OfByte
the layout of the region of memory to be read
layout
,
long
the offset in bytes (relative to this segment address) at which this access operation will occur.
offset
)

Reads a byte from this segment at the given offset, with the given layout.

public boolean

Returns:

a boolean value read from this segment
get
(ValueLayout.OfBoolean
the layout of the region of memory to be read
layout
,
long
the offset in bytes (relative to this segment address) at which this access operation will occur
offset
)

Reads a boolean from this segment at the given offset, with the given layout.

public char

Returns:

a char value read from this segment
get
(ValueLayout.OfChar
the layout of the region of memory to be read
layout
,
long
the offset in bytes (relative to this segment address) at which this access operation will occur
offset
)

Reads a char from this segment at the given offset, with the given layout.

public short

Returns:

a short value read from this segment
get
(ValueLayout.OfShort
the layout of the region of memory to be read
layout
,
long
the offset in bytes (relative to this segment address) at which this access operation will occur
offset
)

Reads a short from this segment at the given offset, with the given layout.

public int

Returns:

an int value read from this segment
get
(ValueLayout.OfInt
the layout of the region of memory to be read
layout
,
long
the offset in bytes (relative to this segment address) at which this access operation will occur.
offset
)

Reads an int from this segment at the given offset, with the given layout.

public float

Returns:

a float value read from this segment
get
(ValueLayout.OfFloat
the layout of the region of memory to be read
layout
,
long
the offset in bytes (relative to this segment address) at which this access operation will occur
offset
)

Reads a float from this segment at the given offset, with the given layout.

public long

Returns:

a long value read from this segment
get
(ValueLayout.OfLong
the layout of the region of memory to be read
layout
,
long
the offset in bytes (relative to this segment address) at which this access operation will occur.
offset
)

Reads a long from this segment at the given offset, with the given layout.

public double

Returns:

a double value read from this segment
get
(ValueLayout.OfDouble
the layout of the region of memory to be read
layout
,
long
the offset in bytes (relative to this segment address) at which this access operation will occur
offset
)

Reads a double from this segment at the given offset, with the given layout.

public MemorySegment

Returns:

a native segment wrapping an address read from this segment
get
(AddressLayout
the layout of the region of memory to be read
layout
,
long
the offset in bytes (relative to this segment address) at which this access operation will occur
offset
)

Reads an address from this segment at the given offset, with the given layout.

public byte

Returns:

a byte value read from this segment
getAtIndex
(ValueLayout.OfByte
the layout of the region of memory to be read
layout
,
long
a logical index. The offset in bytes (relative to this segment address) at which the access operation will occur can be expressed as (index * layout.byteSize()).
index
)

Reads a byte from this segment at the given index, scaled by the given layout size.

public boolean

Returns:

a boolean value read from this segment
getAtIndex
(ValueLayout.OfBoolean
the layout of the region of memory to be read
layout
,
long
a logical index. The offset in bytes (relative to this segment address) at which the access operation will occur can be expressed as (index * layout.byteSize()).
index
)

Reads a boolean from this segment at the given index, scaled by the given layout size.

public char

Returns:

a char value read from this segment
getAtIndex
(ValueLayout.OfChar
the layout of the region of memory to be read
layout
,
long
a logical index. The offset in bytes (relative to this segment address) at which the access operation will occur can be expressed as (index * layout.byteSize()).
index
)

Reads a char from this segment at the given index, scaled by the given layout size.

public short

Returns:

a short value read from this segment
getAtIndex
(ValueLayout.OfShort
the layout of the region of memory to be read
layout
,
long
a logical index. The offset in bytes (relative to this segment address) at which the access operation will occur can be expressed as (index * layout.byteSize()).
index
)

Reads a short from this segment at the given index, scaled by the given layout size.

public int

Returns:

an int value read from this segment
getAtIndex
(ValueLayout.OfInt
the layout of the region of memory to be read.
layout
,
long
a logical index. The offset in bytes (relative to this segment address) at which the access operation will occur can be expressed as (index * layout.byteSize()).
index
)

Reads an int from this segment at the given index, scaled by the given layout size.

public float

Returns:

a float value read from this segment
getAtIndex
(ValueLayout.OfFloat
the layout of the region of memory to be read
layout
,
long
a logical index. The offset in bytes (relative to this segment address) at which the access operation will occur can be expressed as (index * layout.byteSize()).
index
)

Reads a float from this segment at the given index, scaled by the given layout size.

public long

Returns:

a long value read from this segment
getAtIndex
(ValueLayout.OfLong
the layout of the region of memory to be read
layout
,
long
a logical index. The offset in bytes (relative to this segment address) at which the access operation will occur can be expressed as (index * layout.byteSize()).
index
)

Reads a long from this segment at the given index, scaled by the given layout size.

public double

Returns:

a double value read from this segment
getAtIndex
(ValueLayout.OfDouble
the layout of the region of memory to be read
layout
,
long
a logical index. The offset in bytes (relative to this segment address) at which the access operation will occur can be expressed as (index * layout.byteSize()).
index
)

Reads a double from this segment at the given index, scaled by the given layout size.

public MemorySegment

Returns:

a native segment wrapping an address read from this segment
getAtIndex
(AddressLayout
the layout of the region of memory to be read
layout
,
long
a logical index. The offset in bytes (relative to this segment address) at which the access operation will occur can be expressed as (index * layout.byteSize()).
index
)

Reads an address from this segment at the given at the given index, scaled by the given layout size.

public String

Returns:

a Java string constructed from the bytes read from the given starting address up to (but not including) the first '\0' terminator character (assuming one is found)
getString
(long
the offset in bytes (relative to this segment address) at which this access operation will occur
offset
)

Reads a null-terminated string from this segment at the given offset, using the UTF-8 charset.

public String

Returns:

a Java string constructed from the bytes read from the given starting address up to (but not including) the first '\0' terminator character (assuming one is found)
getString
(long
offset in bytes (relative to this segment address) at which this access operation will occur
offset
,
Charset
the charset used to decode the string bytes
charset
)

Reads a null-terminated string from this segment at the given offset, using the provided charset.

public int

Returns:

the hash code value for this memory segment
hashCode
()

Returns the hash code value for this memory segment.

public Optional<Object>

Returns:

the Java object associated with this memory segment, if any
heapBase
()

Returns the Java object stored in the on-heap region of memory backing this memory segment, if any.

public boolean

Returns:

true if this segment can be accessed from the provided thread
isAccessibleBy
(Thread
the thread to be tested
thread
)

Returns true if this segment can be accessed from the provided thread.

public boolean

Returns:

true if it is likely that the contents of this segment are resident in physical memory
isLoaded
()

Determines whether all the contents of this mapped segment are resident in physical memory.

public boolean

Returns:

true if this segment is a mapped segment
isMapped
()

Returns true if this segment is a mapped segment.

public boolean

Returns:

true if this segment is a native segment
isNative
()

Returns true if this segment is a native segment.

public boolean

Returns:

true, if this segment is read-only
isReadOnly
()

Returns true, if this segment is read-only.

public void
load()

Loads the contents of this mapped segment into physical memory.

public long

Returns:

the maximum byte alignment associated with this memory segment
maxByteAlignment
()

Returns the maximum byte alignment associated with this memory segment.

public long

Returns:

the relative offset, in bytes, of the first mismatch between this and the given other segment, otherwise -1 if no mismatch
mismatch
(MemorySegment
the segment to be tested for a mismatch with this segment
other
)

Finds and returns the offset, in bytes, of the first mismatch between this segment and the given other segment.

public static long

Returns:

the relative offset, in bytes, of the first mismatch between the source and destination segments, otherwise -1 if no mismatch
mismatch
(MemorySegment
the source segment.
srcSegment
,
long
the offset (inclusive) of the first byte in the source segment to be tested
srcFromOffset
,
long
the offset (exclusive) of the last byte in the source segment to be tested
srcToOffset
,
MemorySegment
the destination segment
dstSegment
,
long
the offset (inclusive) of the first byte in the destination segment to be tested
dstFromOffset
,
long
the offset (exclusive) of the last byte in the destination segment to be tested
dstToOffset
)

Finds and returns the relative offset, in bytes, of the first mismatch between the source and the destination segments.

public static MemorySegment

Returns:

a zero-length native segment with the given address
ofAddress
(long
the address of the returned native segment
address
)

Creates a zero-length native segment from the given address value.

public static MemorySegment

Returns:

a heap memory segment backed by a byte array
ofArray
(byte[]
the primitive array backing the heap memory segment
byteArray
)

Creates a heap segment backed by the on-heap region of memory that holds the given byte array.

public static MemorySegment

Returns:

a heap memory segment backed by a char array
ofArray
(char[]
the primitive array backing the heap segment
charArray
)

Creates a heap segment backed by the on-heap region of memory that holds the given char array.

public static MemorySegment

Returns:

a heap memory segment backed by a short array
ofArray
(short[]
the primitive array backing the heap segment
shortArray
)

Creates a heap segment backed by the on-heap region of memory that holds the given short array.

public static MemorySegment

Returns:

a heap memory segment backed by an int array
ofArray
(int[]
the primitive array backing the heap segment
intArray
)

Creates a heap segment backed by the on-heap region of memory that holds the given int array.

public static MemorySegment

Returns:

a heap memory segment backed by a float array
ofArray
(float[]
the primitive array backing the heap segment
floatArray
)

Creates a heap segment backed by the on-heap region of memory that holds the given float array.

public static MemorySegment

Returns:

a heap memory segment backed by a long array
ofArray
(long[]
the primitive array backing the heap segment
longArray
)

Creates a heap segment backed by the on-heap region of memory that holds the given long array.

public static MemorySegment

Returns:

a heap memory segment backed by a double array
ofArray
(double[]
the primitive array backing the heap segment
doubleArray
)

Creates a heap segment backed by the on-heap region of memory that holds the given double array.

public static MemorySegment

Returns:

a memory segment, derived from the given buffer instance
ofBuffer
(Buffer
the buffer instance to be turned into a new memory segment
buffer
)

Creates a memory segment that is backed by the same region of memory that backs the given Buffer instance.

public MemorySegment

Returns:

a new memory segment that has the same address and scope as this segment, but the new provided size
reinterpret
(long
the size of the returned segment
newSize
)

Returns a new memory segment that has the same address and scope as this segment, but with the provided size.

public MemorySegment

Returns:

a new memory segment with unbounded size
reinterpret
(Arena
the arena to be associated with the returned segment
arena
,
Consumer<MemorySegment>
the cleanup action that should be executed when the provided arena is closed (can be null)
cleanup
)

Returns a new memory segment with the same address and size as this segment, but with the provided scope.

public MemorySegment

Returns:

a new segment that has the same address as this segment, but with the new size and its scope set to that of the provided arena.
reinterpret
(long
the size of the returned segment
newSize
,
Arena
the arena to be associated with the returned segment
arena
,
Consumer<MemorySegment>
the cleanup action that should be executed when the provided arena is closed (can be null).
cleanup
)

Returns a new segment with the same address as this segment, but with the provided size and scope.

public MemorySegment.Scope

Returns:

the scope associated with this memory segment
scope
()

Returns the scope associated with this memory segment.

public void
set(ValueLayout.OfByte
the layout of the region of memory to be written
layout
,
long
the offset in bytes (relative to this segment address) at which this access operation will occur.
offset
,
byte
the byte value to be written.
value
)

Writes a byte into this segment at the given offset, with the given layout.

public void
set(ValueLayout.OfBoolean
the layout of the region of memory to be written
layout
,
long
the offset in bytes (relative to this segment address) at which this access operation will occur
offset
,
boolean
the boolean value to be written
value
)

Writes a boolean into this segment at the given offset, with the given layout.

public void
set(ValueLayout.OfChar
the layout of the region of memory to be written
layout
,
long
the offset in bytes (relative to this segment address) at which this access operation will occur.
offset
,
char
the char value to be written
value
)

Writes a char into this segment at the given offset, with the given layout.

public void
set(ValueLayout.OfShort
the layout of the region of memory to be written
layout
,
long
the offset in bytes (relative to this segment address) at which this access operation will occur.
offset
,
short
the short value to be written
value
)

Writes a short into this segment at the given offset, with the given layout.

public void
set(ValueLayout.OfInt
the layout of the region of memory to be written
layout
,
long
the offset in bytes (relative to this segment address) at which this access operation will occur
offset
,
int
the int value to be written
value
)

Writes an int into this segment at the given offset, with the given layout.

public void
set(ValueLayout.OfFloat
the layout of the region of memory to be written
layout
,
long
the offset in bytes (relative to this segment address) at which this access operation will occur
offset
,
float
the float value to be written
value
)

Writes a float into this segment at the given offset, with the given layout.

public void
set(ValueLayout.OfLong
the layout of the region of memory to be written
layout
,
long
the offset in bytes (relative to this segment address) at which this access operation will occur.
offset
,
long
the long value to be written.
value
)

Writes a long into this segment at the given offset, with the given layout.

public void
set(ValueLayout.OfDouble
the layout of the region of memory to be written
layout
,
long
the offset in bytes (relative to this segment address) at which this access operation will occur
offset
,
double
the double value to be written
value
)

Writes a double into this segment at the given offset, with the given layout.

public void
set(AddressLayout
the layout of the region of memory to be written
layout
,
long
the offset in bytes (relative to this segment address) at which this access operation will occur.
offset
,
MemorySegment
the address value to be written.
value
)

Writes an address into this segment at the given offset, with the given layout.

public void
setAtIndex(ValueLayout.OfChar
the layout of the region of memory to be written
layout
,
long
a logical index. The offset in bytes (relative to this segment address) at which the access operation will occur can be expressed as (index * layout.byteSize()).
index
,
char
the char value to be written
value
)

Writes a char into this segment at the given index, scaled by the given layout size.

public void
setAtIndex(ValueLayout.OfByte
the layout of the region of memory to be written
layout
,
long
a logical index. The offset in bytes (relative to this segment address) at which the access operation will occur can be expressed as (index * layout.byteSize()).
index
,
byte
the short value to be written
value
)

Writes a byte into this segment at the given index, scaled by the given layout size.

public void
setAtIndex(ValueLayout.OfBoolean
the layout of the region of memory to be written
layout
,
long
a logical index. The offset in bytes (relative to this segment address) at which the access operation will occur can be expressed as (index * layout.byteSize()).
index
,
boolean
the short value to be written
value
)

Writes a boolean into this segment at the given index, scaled by the given layout size.

public void
setAtIndex(ValueLayout.OfShort
the layout of the region of memory to be written
layout
,
long
a logical index. The offset in bytes (relative to this segment address) at which the access operation will occur can be expressed as (index * layout.byteSize()).
index
,
short
the short value to be written
value
)

Writes a short into this segment at the given index, scaled by the given layout size.

public void
setAtIndex(ValueLayout.OfInt
the layout of the region of memory to be written
layout
,
long
a logical index. The offset in bytes (relative to this segment address) at which the access operation will occur can be expressed as (index * layout.byteSize()).
index
,
int
the int value to be written
value
)

Writes an int into this segment at the given index, scaled by the given layout size.

public void
setAtIndex(ValueLayout.OfFloat
the layout of the region of memory to be written
layout
,
long
a logical index. The offset in bytes (relative to this segment address) at which the access operation will occur can be expressed as (index * layout.byteSize()).
index
,
float
the float value to be written
value
)

Writes a float into this segment at the given index, scaled by the given layout size.

public void
setAtIndex(ValueLayout.OfLong
the layout of the region of memory to be written
layout
,
long
a logical index. The offset in bytes (relative to this segment address) at which the access operation will occur can be expressed as (index * layout.byteSize()).
index
,
long
the long value to be written
value
)

Writes a long into this segment at the given index, scaled by the given layout size.

public void
setAtIndex(ValueLayout.OfDouble
the layout of the region of memory to be written
layout
,
long
a logical index. The offset in bytes (relative to this segment address) at which the access operation will occur can be expressed as (index * layout.byteSize()).
index
,
double
the double value to be written
value
)

Writes a double into this segment at the given index, scaled by the given layout size.

public void
setAtIndex(AddressLayout
the layout of the region of memory to be written
layout
,
long
a logical index. The offset in bytes (relative to this segment address) at which the access operation will occur can be expressed as (index * layout.byteSize()).
index
,
MemorySegment
the address value to be written
value
)

Writes an address into this segment at the given index, scaled by the given layout size.

public void
setString(long
the offset in bytes (relative to this segment address) at which this access operation will occur, the final address of this write operation can be expressed as address() + offset.
offset
,
String
the Java string to be written into this segment
str
)

Writes the given string into this segment at the given offset, converting it to a null-terminated byte sequence using the UTF-8 charset.

public void
setString(long
offset in bytes (relative to this segment address) at which this access operation will occur, the final address of this write operation can be expressed as address() + offset
offset
,
String
the Java string to be written into this segment
str
,
Charset
the charset used to encode the string bytes
charset
)

Writes the given string into this segment at the given offset, converting it to a null-terminated byte sequence using the provided charset.

public Spliterator<MemorySegment>

Returns:

the element spliterator for this segment
spliterator
(MemoryLayout
the layout to be used for splitting
elementLayout
)

Returns a spliterator for this memory segment.

public byte[]

Returns:

a new byte array whose contents are copied from this memory segment
toArray
(ValueLayout.OfByte
the source element layout. If the byte order associated with the layout is different from the native order, a byte swap operation will be performed on each array element
elementLayout
)

Copy the contents of this memory segment into a new byte array.

public short[]

Returns:

a new short array whose contents are copied from this memory segment
toArray
(ValueLayout.OfShort
the source element layout. If the byte order associated with the layout is different from the native order, a byte swap operation will be performed on each array element
elementLayout
)

Copy the contents of this memory segment into a new short array.

public char[]

Returns:

a new char array whose contents are copied from this memory segment
toArray
(ValueLayout.OfChar
the source element layout. If the byte order associated with the layout is different from the native order, a byte swap operation will be performed on each array element
elementLayout
)

Copy the contents of this memory segment into a new char array.

public int[]

Returns:

a new int array whose contents are copied from this memory segment
toArray
(ValueLayout.OfInt
the source element layout. If the byte order associated with the layout is different from the native order, a byte swap operation will be performed on each array element.
elementLayout
)

Copy the contents of this memory segment into a new int array.

public float[]

Returns:

a new float array whose contents are copied from this memory segment
toArray
(ValueLayout.OfFloat
the source element layout. If the byte order associated with the layout is different from the native order, a byte swap operation will be performed on each array element
elementLayout
)

Copy the contents of this memory segment into a new float array.

public long[]

Returns:

a new long array whose contents are copied from this memory segment
toArray
(ValueLayout.OfLong
the source element layout. If the byte order associated with the layout is different from the native order, a byte swap operation will be performed on each array element
elementLayout
)

Copy the contents of this memory segment into a new long array.

public double[]

Returns:

a new double array whose contents are copied from this memory segment
toArray
(ValueLayout.OfDouble
the source element layout. If the byte order associated with the layout is different from the native order, a byte swap operation will be performed on each array element
elementLayout
)

Copy the contents of this memory segment into a new double array.

public void
unload()

Unloads the contents of this mapped segment from physical memory.