Scrivio Interview library

Foreign function and memory API, JNI

16 questions in 5 subtopics.

JNI basics

ConceptFoundations6 min read
What is JNI and what are the steps to call a C function from Java with it? What are its main drawbacks?
DebuggingIntermediate7 min read
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?
LeadershipPrincipal7 min read
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

ConceptIntermediate6 min read
Explain the Arena types in the FFM API: global, auto, confined and shared. What safety guarantees does the API give compared with Unsafe?
CodingIntermediate6 min read
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.
ScenarioAdvanced8 min read
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.
LeadershipPrincipal7 min read
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

ConceptFoundations7 min read
What is off-heap memory in Java, and why would a service use it? Name the ways to allocate it.
DebuggingIntermediate7 min read
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?
DesignAdvanced7 min read
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?
LeadershipPrincipal6 min read
For decades, high-performance Java libraries relied on sun.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

ConceptIntermediate6 min read
What is the Vector API, and what is its status in Java 25? When would you consider it?
LeadershipAdvanced7 min read
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

ConceptFoundations7 min read
What is sun.misc.Unsafe, what did libraries use it for, and what is its status in current JDKs? Name the supported replacements for its main uses.
CodingIntermediate7 min read
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);
}
LeadershipAdvanced6 min read
Your platform depends on several libraries (an old serialisation library, a cache, a networking stack) that use sun.misc.Unsafe. JDK upgrades now print warnings and future releases will remove these methods. How would you manage this risk across 200 services?