151. Understanding addresses (pointers)
A memory segment has a memory address (pointer) expressed as a long
number. An off-heap memory segment has a physical address that points out the memory region that backs the segment (base address). Each memory layout stored in this segment has its own memory address as well. For instance, here is an example of querying the base address of a memory segment via the address()
method (arena
is an instance of Arena
):
MemorySegment segment = arena
.allocate(ValueLayout.JAVA_INT, 1000);
long addr = segment.address(); // 2620870760384
On the other hand, an on-heap memory segment has a non-physical stable virtualized address typically representing an offset within the memory region of that segment (the client sees a stable address while the garbage collector can reallocate the region of memory inside the heap). For instance, an on-heap segment created via one of the ofArray()
factory methods has an address of 0.
Next, let’s focus...