OpenJDK Terminology

Foreign Function & Memory API

July 06, 2026

The Foreign Function & Memory (FFM) API, finalised in Java 22 (JEP 454), provides a safe, efficient, and pure-Java way to interact with native code and off-heap memory — replacing the older, error-prone Java Native Interface (JNI).

Foreign memory access allows Java programs to allocate, read, and write memory outside the Java heap using MemorySegment. Off-heap allocation is useful for large data structures that should not be managed by the GC, for memory-mapped files, and for shared memory between processes. The API enforces lifetime tracking: a MemorySegment is associated with an Arena, and its memory is released deterministically when the arena is closed.

Foreign function calls allow Java programs to call native functions in shared libraries (.so, .dll, .dylib) without writing any C or JNI glue code. A Linker resolves native symbols and produces a MethodHandle that can be called from Java. The API handles ABI conventions, argument marshalling, and return type mapping automatically for all supported platforms.

try (Arena arena = Arena.ofConfined()) {
    MethodHandle strlen = Linker.nativeLinker()
        .downcallHandle(
            Linker.nativeLinker().defaultLookup().find("strlen").orElseThrow(),
            FunctionDescriptor.of(JAVA_LONG, ADDRESS));
    MemorySegment str = arena.allocateFrom("Hello, FFM!");
    long len = (long) strlen.invoke(str); // 11
}

The FFM API supersedes both JNI (for calling native functions) and the older sun.misc.Unsafe (for off-heap memory). JNI remains available for backwards compatibility but is no longer the recommended approach for new code.

See also: Bytecode, Java Module System (JPMS)

Comments (0)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Comments (0)

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

No comments yet. Be the first.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard