Foreign function and memory API, JNI
16 questions in 5 subtopics.
JNI basics
What is JNI and what are the steps to call a C function from Java with it? What are its main drawbacks?A service using a JNI-based image library crashes occasionally with an hs_err_pid file and SIGSEGV in the native frame. How do you investigate, and how do you reduce the risk?JNI has been in Java since 1.1, yet the platform now restricts native access and added a new foreign function API. Why did the JDK move this way, and what does it mean for your security posture?Foreign Function and Memory API
Explain the Arena types in the FFM API: global, auto, confined and shared. What safety guarantees does the API give compared with Unsafe?Using the Foreign Function and Memory API (final in Java 22), call the C library function strlen on a Java string. Describe the classes you use.Your organisation has several JNI wrappers maintained by one person each. Make the case for migrating them to the FFM API and describe the plan and risks.With FFM final since Java 22 and native access now requiring explicit permission in recent JDKs, how would you govern native code use across the company for the next decade?Off-heap memory
What is off-heap memory in Java, and why would a service use it? Name the ways to allocate it.A container keeps getting OOM-killed while the Java heap, as shown by GC logs, never exceeds 60 percent of -Xmx. What memory areas outside the heap do you check, and with which tools?A team wants to build an off-heap cache of 30 GB in a latency-sensitive service to avoid GC pauses. What do you ask them, and what alternatives do you consider?For decades, high-performance Java libraries relied onsun.misc.Unsafe. The JDK is now deprecating its memory access methods in favour of VarHandle and the FFM API. How should an organisation respond across its dependency tree?Vector API
What is the Vector API, and what is its status in Java 25? When would you consider it?A performance team wants to ship production code using the incubating Vector API for a 3x speed-up in a scoring service. How do you decide?Sun.misc.Unsafe
What issun.misc.Unsafe, what did libraries use it for, and what is its status in current JDKs? Name the supported replacements for its main uses.Replace this Unsafe-based compare-and-set on a field with a VarHandle, and explain what you gain:private static final long STATE_OFFSET = UNSAFE.objectFieldOffset(Task.class.getDeclaredField("state"));
boolean tryStart() {
return UNSAFE.compareAndSwapInt(this, STATE_OFFSET, 0, 1);
}
sun.misc.Unsafe. JDK upgrades now print warnings and future releases will remove these methods. How would you manage this risk across 200 services?No question matches these choices.