Quick revision
The short answer
Moving the JNI wrappers to the Foreign Function and Memory API turns each binding into plain Java: the C glue code, the per-platform compile step and most of the single-maintainer knowledge disappear, and jextract can generate much of the remaining code from the C headers. The plan is a pilot on one wrapper behind its existing Java interface, a benchmark and side-by-side tests, then a switch, with --enable-native-access granted only to the binding module. The main risks are the Java 22 minimum, platform-specific type sizes and performance that must be measured rather than assumed.
What to remember
In this answer, we saw that the strongest argument for moving JNI wrappers to FFM is removing the C glue that only one person understands, which makes the bindings plain Java that the whole team can read, build and review. jextract reduces the hand-written part further. We would migrate one wrapper as a pilot behind its existing interface, compare old and new in tests and benchmarks, switch with a flag, and only then repeat. The risks we manage are the Java 22 minimum, platform type sizes, performance that must be measured, and keeping native access limited to the binding module.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Moving the JNI wrappers to the Foreign Function and Memory API turns each binding into plain Java: the C glue code, the per-platform compile step and most of the single-maintainer knowledge disappear, and jextract can generate much of the remaining code from the C headers. The plan is a pilot on one wrapper behind its existing Java interface, a benchmark and side-by-side tests, then a switch, with --enable-native-access granted only to the binding module. The main risks are the Java 22 minimum, platform-specific type sizes and performance that must be measured rather than assumed.
Why this question is asked
Many organisations have a handful of JNI wrappers that were written years ago by one engineer, around a compression library, a hardware SDK or a pricing engine in C. They work, until that engineer leaves, a new CPU architecture arrives, or a crash in the C glue takes down a production JVM. Interviewers ask this question at senior level because it is not really about an API: it is about reducing a bus-factor risk without breaking working code. In this answer, we will recap what JNI and FFM are, compare one binding written both ways, build the case for migrating, lay out a pilot-first plan, and go through the risks and how we would control each one.
1A Quick Recap of JNI and FFM
JNI, the Java Native Interface, is the original way for Java to call C. We declare a method with the native keyword in Java, then write a C function with a special generated name that receives a JNIEnv pointer, converts Java arrays and strings into C values, calls the real library and converts the result back. That C file must be compiled for every operating system and CPU we deploy to, and packaged so the JVM can load it with System.loadLibrary.
The Foreign Function and Memory API, FFM for short, became a final feature in Java 22 (JEP 454). It lets Java code look up a symbol in a native library, describe its signature with a FunctionDescriptor, and get back a MethodHandle that calls it directly. Memory that C needs is allocated as a MemorySegment inside an Arena, an object that decides how long that memory lives. There is no C code of our own at all; the only native code left is the library we wanted to call in the first place.
2The Same Binding Written Both Ways
Let us take one small function from zlib, crc32, which computes a checksum over a byte buffer. With JNI we need a Java class and a C file that the build compiles per platform.
Notice what disappeared. There is no generated function name to keep in sync, no manual release of the array elements, and no native build. A reviewer who knows Java can read the whole binding, and a mistake such as a wrong argument type fails with an exception in Java instead of corrupting memory inside C glue. That is the heart of the case: the risky, rarely-touched part of each wrapper was the glue, and FFM removes it.
For larger libraries we would not write these handles by hand. jextract, a tool from the OpenJDK project that ships separately from the JDK, reads the C header files and generates the layouts and method handles for every function and struct. We then wrap the generated code in a small hand-written Java class that offers the API our services already use.
3Making the Case to the Organisation
The argument for migrating rests on risk and cost rather than fashion. Today each wrapper has one maintainer, a native toolchain per platform, and a failure mode where a bug in the glue crashes the whole JVM with no Java stack trace. After the migration, the bindings are ordinary Java code that any team member can review, they build with the same Maven or Gradle job as everything else, and moving to ARM servers no longer means finding someone who can cross-compile the glue.
There is also a direction-of-travel argument. Recent JDKs warn when code uses JNI or restricted FFM methods without permission (JEP 472 in Java 24 prepares to restrict JNI), and the stated goal is integrity by default. Both JNI and FFM will need explicit permission, but FFM is where new tooling and documentation are going. Migrating while the original authors are still around is far cheaper than doing it in an emergency later.
4A Pilot-First Migration Plan
We would not migrate everything at once. We pick one wrapper as a pilot, ideally one with clear inputs and outputs and good existing tests, such as the checksum or compression binding. The callers already use a Java interface, or we introduce one, so the JNI version and the FFM version can sit side by side behind it.
- Put the wrapper behind a Java interfacecallers stop depending on JNI details
- Generate bindings with jextractor write a few handles by hand
- Run old and new side by side in testscompare outputs on the same inputs
- Benchmark with JMH on production hardwarex64 and ARM if both are used
- Results equal and speed acceptable?YesSwitch the flag service by serviceNoFix or keep JNI for this one
- Delete the JNI glue after one release
6 steps in order. Play them, or step through them one at a time.
The pilot proves the approach on one wrapper before we spend effort on the rest.
Then we run both implementations against the same inputs in tests, including edge cases such as empty arrays and very large buffers, and compare the results byte for byte. We benchmark with JMH on the hardware we actually run, because downcalls through FFM are competitive with JNI in many cases but not all, and copying a large Java array into native memory has its own cost. Only when the results match and the numbers are acceptable do we switch the flag, first for one service, then for all. The JNI code stays in the repository for one release so the switch back is a configuration change, and then we delete it.
5The Risks and How We Control Them
The biggest constraint is the Java version. FFM is final only from Java 22, so a service still on Java 21 can only use the preview version, which needs --enable-preview and has a different API surface in places. In practice we tie the migration of each wrapper to the services that use it reaching Java 25, the current LTS, and keep JNI for the stragglers until they upgrade.
The second risk is type sizes. In C, a long is 64 bits on Linux and macOS but 32 bits on Windows, so a hand-written FunctionDescriptor that says JAVA_LONG is correct on one platform and wrong on another. jextract generates per-platform bindings for exactly this reason, and Linker.nativeLinker().canonicalLayouts() gives the right layout for the current machine. Our side-by-side tests must run on every platform we deploy to.
The third risk is permissions and blast radius. FFM is still native code: a wrong pointer can still crash the JVM. Calling restricted methods such as downcallHandle prints a warning unless the module is allowed native access, and future releases plan to make it an error. We grant --enable-native-access only to the binding module (or use the Enable-Native-Access manifest attribute for an executable jar), so that no other library in the process gets the same power by accident.
| Risk | How we control it |
|---|---|
| Services still on Java 21 | Migrate those services to Java 25 first; keep JNI until then |
| C type sizes differ per platform | Use jextract per platform; test on every target OS and CPU |
| Slower in a hot path | JMH benchmark before switching; keep JNI behind the flag |
| Native crashes still possible | Same tests as before plus fuzzing of inputs at the boundary |
| Too much native access granted | --enable-native-access only for the binding module |
6Follow-up questions
Is FFM always faster than JNI?
No, and we should not promise that. Downcalls are compiled by the JIT into a direct native call and are competitive with JNI in many benchmarks, sometimes faster, but the total cost also depends on copying data between the heap and native memory. If a hot path copies a large array on every call, keeping the data in a long-lived MemorySegment can matter more than the call itself, so we measure the real workload.
What if a vendor only ships a JNI library?
Then we keep JNI for that library. FFM calls C functions with a C interface; it cannot call into a library whose only entry points are JNI-named functions expecting a JNIEnv. We still put that binding behind a Java interface and grant native access only to its module.
What does jextract not do for us?
It generates low-level bindings, not a good Java API. Ownership of memory, error codes, thread safety of the C library and the lifetime of returned pointers still need a human-written wrapper and tests. It also needs to be run for each target platform when type sizes differ.
7Conclusion
In this answer, we saw that the strongest argument for moving JNI wrappers to FFM is removing the C glue that only one person understands, which makes the bindings plain Java that the whole team can read, build and review. jextract reduces the hand-written part further. We would migrate one wrapper as a pilot behind its existing interface, compare old and new in tests and benchmarks, switch with a flag, and only then repeat. The risks we manage are the Java 22 minimum, platform type sizes, performance that must be measured, and keeping native access limited to the binding module.
Open with
My case would be about risk, not novelty. Each JNI wrapper today has a C glue layer, a native build per platform, and one person who understands it, and a bug in that glue crashes the JVM.
Then, if they want more
With the Foreign Function and Memory API, final since Java 22, the binding becomes plain Java: a symbol lookup, a function descriptor and a method handle, with memory managed by an Arena. jextract can generate most of that from the headers, so we write only a thin Java wrapper.
The plan is to put each wrapper behind a Java interface, pick one as a pilot, run the JNI and FFM versions side by side on the same inputs, benchmark with JMH on our real hardware, and switch with a flag so rolling back is a configuration change.
After a release with no problems we delete the glue and move to the next wrapper.
The risks I would call out are that services on Java 21 cannot use the final API, so the migration follows our Java 25 upgrades; C type sizes such as long differ between platforms, so we test on every target; and performance must be measured, not assumed.
Finally I would grant --enable-native-access only to the binding module, so native access stays contained.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 22, Java 21, Java 24, Java 25
- 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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "Your organisation has several JNI wrappers maintained by one person each. Make the case...": https://scrivio-interview-library.netlify.app/java/foreign-memory-and-native/3764268ec5/