Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

Explain the Arena types in the FFM API: global, auto, confined and shared. What safety guarantees does the API give compared with Unsafe?

An Arena controls how long native memory lives. Arena.global() memory is never freed; Arena.ofAuto() memory is freed by the garbage collector once nothing refers to it; Arena.ofConfined() memory is used by one thread and freed deterministically when we close the arena; and Arena.ofShared() memory can be used by many threads and is freed on close through a safe handshake. Every MemorySegment access is bounds-checked and fails after its arena is closed, so the API prevents out-of-bounds access and use-after-free, which Unsafe never checked.

Why this question is asked

Working with memory outside the Java heap used to mean either ByteBuffer, with its 2 GB limit and no deterministic free, or sun.misc.Unsafe, with no safety at all. The FFM API, final in Java 22, gives us a third option that is both fast and safe, and the Arena is the idea at its centre. Interviewers ask this to see whether we understand lifetimes and thread confinement, which decide whether our code leaks, crashes or works. In this answer, we will recap memory segments, go through each arena type with code, see exactly what the checks catch, and compare that with Unsafe.

1A Quick Recap of Segments and Arenas

A MemorySegment is a view of a contiguous region of memory: an address, a size, and rules about who may access it and until when. It can describe on-heap memory, such as a Java array, or off-heap native memory that we allocated or received from a C library.

An Arena is what allocates native segments and decides how long they live. Every segment allocated by an arena shares the arena's lifetime and its thread rules. That is the key design choice: instead of freeing each piece of memory separately, as in C with malloc and free, we free a whole group at once by closing the arena, and the API can then check, on every access, whether the group is still alive.

The four arena types
ArenaFreed whenThreadsclose()
Arena.global()Never (until JVM exits)Any threadThrows UnsupportedOperationException
Arena.ofAuto()By the GC when unreachableAny threadThrows UnsupportedOperationException
Arena.ofConfined()On close, deterministicallyOnly the owner threadFrees immediately
Arena.ofShared()On close, deterministicallyAny threadFrees after a safe handshake

2Global and Auto Arenas: No Close

The global arena is for memory that should live as long as the application, such as a lookup table we pass to a native library once at startup. It is never freed, so allocating from it repeatedly is a leak by design. The automatic arena hands memory management to the garbage collector: when no segment from it is reachable any more, the GC eventually frees the native memory. That is convenient, but we do not control when it happens, so under heavy allocation native memory can grow before the GC catches up, the same weakness as direct ByteBuffers.

Java
MemorySegment table = Arena.global().allocate(4096); // lives until the JVM exits
MemorySegment tmp = Arena.ofAuto().allocate(1024); // freed some time after tmp is unreachable
tmp.set(ValueLayout.JAVA_INT, 0, 7);
tmp.get(ValueLayout.JAVA_INT, 0); // 7
Arena.ofAuto().close();
// UnsupportedOperationException: neither global nor auto arenas can be closed

3Confined and Shared Arenas: Deterministic Free

For most work we want to decide exactly when memory is freed. A confined arena belongs to the thread that created it. Only that thread can access its segments or close it, and closing frees the memory immediately. We use it with try-with-resources, so the memory is freed even if an exception is thrown.

Java
MemorySegment leaked;
try (Arena arena = Arena.ofConfined()) {
MemorySegment ints = arena.allocate(ValueLayout.JAVA_INT, 10); // 40 bytes, zeroed
ints.setAtIndex(ValueLayout.JAVA_INT, 0, 42);
ints.getAtIndex(ValueLayout.JAVA_INT, 0); // 42
ints.byteSize(); // 40
leaked = ints;
Thread.ofVirtual().start(() -> ints.getAtIndex(ValueLayout.JAVA_INT, 0)).join();
// in the other thread: WrongThreadException, the segment is confined to its owner
} // memory is freed here
leaked.getAtIndex(ValueLayout.JAVA_INT, 0);
// IllegalStateException: Already closed

Notice the last line: we kept a reference to the segment after the arena closed, which in C would be a dangling pointer. Here the access fails with an exception instead of reading freed memory.

A shared arena lifts the thread restriction: any thread can access its segments and any thread can close it. The difficulty is closing safely while other threads might be in the middle of an access. The JVM solves this with a handshake: on close it briefly stops each thread at a safe point, checks whether any of them is currently accessing a segment of that arena, and only frees the memory when none is. So closing a shared arena is more expensive than closing a confined one, but it never frees memory under a running access. We use it when several threads work on the same native buffer, for example a parallel computation over a large off-heap array.

4What the Checks Guarantee

Every access through a MemorySegment is checked in two ways. Spatial safety: the offset and size are compared with the segment's bounds, so reading past the end throws IndexOutOfBoundsException instead of reading a neighbour's memory. Temporal safety: the segment's arena must still be alive, and for a confined arena the caller must be the owner thread, so use-after-free becomes IllegalStateException and a cross-thread access becomes WrongThreadException.

Java
try (Arena arena = Arena.ofConfined()) {
MemorySegment ints = arena.allocate(ValueLayout.JAVA_INT, 10);
ints.getAtIndex(ValueLayout.JAVA_INT, 10);
// IndexOutOfBoundsException: index 10 is past the end of a 10-element segment
}
Step throughWhat happens on a segment access
  1. Is the offset plus size inside the segment?
    YesContinueNoIndexOutOfBoundsException
  2. Is the arena still alive?
    YesContinueNoIllegalStateException: Already closed
  3. Confined and called from another thread?
    YesWrongThreadExceptionNoRead or write the memory

3 steps in order. Play them, or step through them one at a time.

These checks are cheap in practice. The JIT recognises them in loops and hoists or removes them where it can prove the access is safe, so code over segments runs close to the speed of raw memory access.

5Compared With Unsafe

sun.misc.Unsafe offered allocateMemory, freeMemory and getInt or putInt at raw addresses. It was fast and widely used by libraries for off-heap memory, but it checked nothing. Reading past an allocation silently returned whatever was there; writing past it corrupted other data; using an address after freeMemory might work, return garbage or crash the JVM with a SIGSEGV, depending on luck. Bugs showed up far from their cause, if at all.

The FFM API gives the same capability with the bounds and lifetime checks built in, and the JDK is moving libraries toward it: Unsafe's memory access methods were deprecated for removal in Java 23 and print warnings when used in recent JDKs; check your version for the current default.

Java
// Unsafe: no checks at all
long addr = UNSAFE.allocateMemory(40);
UNSAFE.putInt(addr + 40, 1); // writes past the end: silent memory corruption
UNSAFE.freeMemory(addr);
UNSAFE.getInt(addr); // use after free: garbage, or a JVM crash

6Follow-up questions

Which arena should be our default?

Arena.ofConfined() with try-with-resources. It frees memory deterministically, has the cheapest close, and catches cross-thread mistakes. We move to a shared arena only when several threads really need the same memory, and to an automatic arena only when lifetimes are too complex to manage with a scope.

Can the checks catch a C library using memory after we close the arena?

No. The checks apply to accesses made through MemorySegment in Java. If we pass an address to a native function and it keeps the pointer and uses it later, that happens in C, outside the API's control. We must make sure the arena outlives any native use of its memory.

How do we allocate a struct rather than an array?

We describe it with a StructLayout, for example MemoryLayout.structLayout(ValueLayout.JAVA_INT.withName("x"), ValueLayout.JAVA_INT.withName("y")), allocate it with arena.allocate(layout), and use VarHandles derived from the layout to read and write named fields.

7Conclusion

In this answer, we saw that arenas decide how long native memory lives: the global arena never frees, the automatic arena leaves it to the GC, the confined arena frees deterministically on close for one thread, and the shared arena frees on close for many threads using a safe handshake. Every segment access is bounds-checked and fails after its arena closes or from the wrong thread, which prevents the out-of-bounds and use-after-free bugs that Unsafe allowed silently.

What to say in the interviewAbout 80 seconds aloud

Open with

An Arena in the FFM API owns the lifetime of the native memory it allocates, and every segment from it shares that lifetime. There are four kinds. Arena.global() memory is never freed, so it is for things that live as long as the app.

Then, if they want more

Arena.ofAuto() memory is freed by the garbage collector some time after its segments become unreachable, convenient but not deterministic. Arena.ofConfined() belongs to one thread: only that thread can use or close it, and close frees the memory immediately, so I use it with try-with-resources as my default.

Arena.ofShared() can be used and closed from any thread; on close the JVM does a handshake with all threads to make sure none is in the middle of an access before freeing. Global and auto arenas cannot be closed at all.

The safety comes from checks on every access: offsets are bounds-checked, so reading past the end throws IndexOutOfBoundsException; accessing a segment after its arena is closed throws IllegalStateException; and touching a confined segment from another thread throws WrongThreadException.

Unsafe had none of this, so an overflow or use-after-free silently corrupted memory or crashed the JVM, which is why its memory methods are now deprecated for removal.

About this answer

Last updated
12 November 2026
Versions it names
Java 22, Java 23
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain the Arena types in the FFM API: global, auto, confined and shared. What safety...": https://scrivio-interview-library.netlify.app/java/foreign-memory-and-native/7267a5084e/