Quick revision
The short answer
We would treat native code as a controlled dependency: an approved list of native libraries, each with an owner, pinned versions and a security review; FFM as the default for new integrations, with JNI only when a library ships nothing else; native access granted only to the modules or jars that need it; builds and tests for every platform we deploy to, including ARM; and a standing preference for pure Java or an out-of-process service whenever the performance budget allows, because native code is the one thing that can crash the whole JVM.
What to remember
In this answer, we saw that native code needs its own governance because it bypasses the JVM's safety and brings per-platform builds and security patches. The JDK now helps by requiring explicit native access, which makes native code visible. Our policy has five parts: an approved list with owners, versions and reviews; FFM by default with JNI only when unavoidable; native access granted only to the modules that need it; builds and tests for every platform including ARM; and a preference for pure Java or out-of-process services. A short approval path and CI enforcement keep the policy useful for years.
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.
We would treat native code as a controlled dependency: an approved list of native libraries, each with an owner, pinned versions and a security review; FFM as the default for new integrations, with JNI only when a library ships nothing else; native access granted only to the modules or jars that need it; builds and tests for every platform we deploy to, including ARM; and a standing preference for pure Java or an out-of-process service whenever the performance budget allows, because native code is the one thing that can crash the whole JVM.
Background to this question
For most of Java's history, native code was a quiet corner of a codebase: a JNI wrapper here, a vendor .so file there. Recent JDKs are changing that by asking applications to state explicitly which code may use native access. That turns native code into something a company can actually see and govern. A principal engineer is asked this question to see whether they can set a policy that lasts a decade without blocking teams that genuinely need native libraries. In this answer, we will look at why native code needs special treatment, what the JDK now enforces, the five parts of a governance policy, how a team would get a new library approved, and how we keep the policy alive over time.
1Why Native Code Deserves Its Own Policy
Pure Java code runs inside the JVM's safety net: an array index out of bounds throws an exception, memory is freed by the garbage collector, and a bug in one library rarely takes down the process. Native code has none of that. A wrong pointer in C writes over memory that belongs to the JVM, and the result is a crash with an hs_err file instead of a stack trace. A native library also brings its own security patches, its own build per operating system and CPU, and often its own license terms.
So a native dependency is more expensive than a Java one in every way that matters for a long-lived platform. The goal of governance is not to forbid it, but to make each use deliberate, owned and contained.
2What the JDK Now Enforces
Since Java 22, the Foreign Function and Memory API is final, and its riskiest methods, such as Linker.downcallHandle and MemorySegment.reinterpret, are restricted. Calling them prints a warning unless the code has been granted native access. Java 24 extended the same idea to JNI (JEP 472): loading a native library through JNI also warns without permission. The JDK's stated direction, often called integrity by default, is that these warnings become errors in a future release, so check your version for the current stage.
This gives us a lever we never had before. If our applications run with native access granted only to named modules, then any other library that starts using native code shows up as a warning in tests or a failure in CI. Native code stops being invisible.
3The Five Parts of the Policy
The first part is an approved list of native libraries. Each entry names the library, the versions we allow, an owning team, the platforms it is built for, and the date of its last security review. Owners are people, not committees: the owning team watches security advisories for that library and upgrades it across services.
The second part is the default technology. New native integrations use FFM, because the bindings are pure Java, reviewable by any Java engineer, and generated from headers with jextract. JNI is allowed only when a library ships nothing else, for example a vendor SDK that exposes only JNI entry points.
The third part is containment. Native access is granted with --enable-native-access to the specific module or jar that holds the binding, never with a blanket ALL-UNNAMED on a large class path when we can avoid it. That keeps a surprise native dependency from slipping in through a transitive library.
The fourth part is platform coverage. Every approved library must be built and tested for every platform we deploy to. This matters more each year because many fleets now mix x64 and ARM servers, and a library that exists only for x64 quietly blocks a cheaper ARM migration.
The fifth part is a preference order: pure Java first, then an out-of-process service, then in-process native code. If a pure Java library is within the performance budget, we take it. If native code is truly needed but is risky, running it in a separate process behind a local socket or gRPC means a crash restarts that process, not our service.
| Field | Example |
|---|---|
| Library | libvips (image processing) |
| Allowed versions | 8.15.x, upgrades through the owning team |
| Owner | Media platform team |
| Binding | FFM via jextract, module com.acme.imaging |
| Platforms | linux-x64, linux-aarch64 |
| Last security review | 2026-09, next due 2027-03 |
| Fallback | Out-of-process resizer service |
4How a Team Gets a New Library Approved
A policy that only says no will be bypassed, so the approval path must be short. A team that wants a new native library fills in a one-page request: what it does, why pure Java is not enough (with a benchmark number, not a feeling), the platforms it needs, and who will own it. A small group of reviewers, a security engineer and someone from the platform team, answers within a few days.
- Is a pure Java library within the performance budget?YesUse pure Java, no review neededNoContinue the review
- Could it run out of process within the budget?YesRun it as a separate serviceNoIn-process native code
- Does the library offer a C API?YesBind with FFM and jextractNoJNI allowed as an exception
- Name an owner and the platformsincluding ARM if we run it
- Security review and add to the approved listnative access for that module only
5 steps in order. Play them, or step through them one at a time.
Most requests end at the first or second question.
Notice that the first question is always whether pure Java is good enough. For example, if a pure Java image library resizes a photo in 40 ms and the native one in 15 ms, and the endpoint's budget is 200 ms, the native library is not worth a decade of ownership. If the budget is 20 ms, it is.
5Keeping the Policy Alive for a Decade
A ten-year policy has to survive JDK upgrades, new hardware and staff changes. We enforce the parts that can be automated: CI runs tests with illegal native access denied, so an unapproved native library fails the build, and a dependency scanner flags artifacts that contain .so, .dll or .dylib files that are not on the list. Each LTS release of the JDK, roughly every two years, is a checkpoint where we review the list, retire libraries nobody uses, and check whether pure Java alternatives have caught up.
We also measure. A dashboard shows how many services use native code, which libraries, and whether they are FFM or JNI. Over time, the number of JNI bindings should fall and the number of owners per library should never be zero. Those two numbers tell leadership whether the policy is working.
6Follow-up questions
Why not simply forbid native code?
Because some needs are real: hardware SDKs, codecs, cryptographic modules and machine learning runtimes often exist only as native libraries. A ban pushes teams to hide native code in vendor jars or to build slower workarounds. A clear approval path with containment gives us visibility and keeps the risk where we can see it.
What about native code inside third-party jars, such as Netty's native transports?
They count too. Netty, compression libraries and database drivers sometimes bundle native code. They go on the approved list like any other library, with the same owner and platform rules, and the native access flag shows us when one of them starts using it.
Does an out-of-process service not add latency?
Yes, a local call over a socket costs tens to hundreds of microseconds, plus serialisation. For many workloads, such as document conversion or image processing that takes milliseconds anyway, that cost is small compared with the benefit of a crash being isolated. For tight inner loops it is too expensive, and in-process FFM is the right choice.
7Conclusion
In this answer, we saw that native code needs its own governance because it bypasses the JVM's safety and brings per-platform builds and security patches. The JDK now helps by requiring explicit native access, which makes native code visible. Our policy has five parts: an approved list with owners, versions and reviews; FFM by default with JNI only when unavoidable; native access granted only to the modules that need it; builds and tests for every platform including ARM; and a preference for pure Java or out-of-process services. A short approval path and CI enforcement keep the policy useful for years.
Open with
I would treat native code as a high-risk dependency that we allow on purpose, not by accident. First, an approved list of native libraries: each with an owning team, allowed versions, the platforms it is built for, and a regular security review.
Then, if they want more
Second, new integrations use the Foreign Function and Memory API, since bindings are plain Java and jextract can generate them from headers; JNI only when a vendor ships nothing else.
Third, containment: now that the JDK warns, and later will refuse, when code uses native access without permission, we grant --enable-native-access only to the specific binding module, and CI runs with illegal native access denied so anything unapproved fails the build.
Fourth, every native library must be built and tested for every platform we deploy, including ARM, so it never blocks a hardware move. Fifth, the preference order is pure Java, then an out-of-process service, then in-process native code, because a native crash takes down the whole JVM.
To keep it practical I would make approval a one-page request answered within days, starting with the question of whether pure Java meets the performance budget, and review the list at every LTS upgrade.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 24, Java 22
- 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 "With FFM final since Java 22 and native access now requiring explicit permission in...": https://scrivio-interview-library.netlify.app/java/foreign-memory-and-native/05b7c1f115/