Quick revision
The short answer
Start with the hs_err_pid file the JVM wrote: it names the faulting frame and native library, the thread that crashed and its Java stack, which tells us which call into the image library failed. Then reproduce in a test environment with -Xcheck:jni to catch misuse of the JNI API, and look for the usual causes: use of freed memory, local references used across calls, missing exception checks and unsafe concurrent use of the library. To reduce the risk, isolate native work in a separate process or service, and keep the library version pinned and tested under load.
What to remember
In this answer, we saw that a native crash kills the whole JVM because native code has no safety nets. We read the hs_err_pid file for the faulting frame, library, thread and Java stack, reproduced with -Xcheck:jni and memory tools, and looked at the usual causes: freed memory, local references used across calls, missing exception checks and unsafe concurrent use. To reduce the risk, we isolate native work in a separate process or service and pin and load-test the library version.
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.
Start with the hs_err_pid file the JVM wrote: it names the faulting frame and native library, the thread that crashed and its Java stack, which tells us which call into the image library failed. Then reproduce in a test environment with -Xcheck:jni to catch misuse of the JNI API, and look for the usual causes: use of freed memory, local references used across calls, missing exception checks and unsafe concurrent use of the library. To reduce the risk, isolate native work in a separate process or service, and keep the library version pinned and tested under load.
Why this question is asked
A SIGSEGV in a native frame is one of the most unsettling production failures in Java: no exception, no stack trace in our logs, just a process that disappears and a file full of hexadecimal. It is also one of the most common problems with JNI-based libraries. Interviewers ask this to see whether we can read the crash report, narrow down the cause methodically and then design so a native bug cannot take the service down again. In this answer, we will recap why native crashes kill the JVM, read an hs_err_pid file, reproduce with -Xcheck:jni and other tools, go through the usual causes, and then reduce the risk with isolation and testing.
1Why a Native Bug Kills the Whole JVM
Java code runs with safety nets: array accesses are bounds-checked, null dereferences throw NullPointerException, and memory is managed by the garbage collector. Native code called through JNI, the Java Native Interface, has none of these. It runs in the same operating system process as the JVM, with full access to its memory.
When native code reads or writes an address it should not, the operating system sends the process a signal, SIGSEGV (segmentation fault) on Linux. The JVM catches the signal, sees that it happened in code it does not control, writes a crash report and aborts. Every thread, and every request in flight, dies with it. That is why one bad image can take down a whole instance.
2Read the hs_err_pid File
On a fatal error the JVM writes hs_err_pid followed by the process id and .log, in the working directory by default, or where -XX:ErrorFile points. We should make sure it lands somewhere that survives a container restart, such as a mounted volume. The first lines are the most useful.
| Section | What it tells us |
|---|---|
| Signal and si_addr | Kind of fault; tiny address suggests a null pointer |
| Problematic frame | Library and function where it crashed (C = native) |
| Current thread | Which thread, and that it was in native code |
| Java frames | Which Java call led into the library, with what arguments |
| Dynamic libraries | Exact library versions loaded |
| Other threads | Was another thread in the same library at the time? |
Notice what each part tells us. The problematic frame starts with C, meaning native code, and names the library and function: resize_bilinear in libimgproc.so. The thread is a request thread in state _thread_in_native, so it was inside our JNI call. The si_addr of 0x18 is a tiny address, which usually means a null pointer plus a field offset. The Java frames show which Java method made the call and with what kind of arguments: resize was passed a long, which is very often a native pointer handle.
Further down, the file lists the loaded libraries with versions, the JVM flags, and the other threads. If several crashes all point at the same frame, we have a strong lead. We also enable core dumps (ulimit -c unlimited, or the container equivalent) so we can open the crash in gdb with the library's debug symbols.
3Reproduce With -Xcheck:jni and Friends
In a test environment we replay the traffic that crashed, ideally the same images, with extra checking switched on. The JVM option -Xcheck:jni makes HotSpot validate arguments to JNI functions: invalid or stale references, calls made while an exception is pending, wrong types, and similar misuse. It prints warnings or aborts with a clear message at the first problem, which is much closer to the cause than a later crash.
It slows calls down, so it belongs in testing, not production. For memory errors inside the library itself, a build of the library with AddressSanitizer reports the exact line that used freed memory or overflowed a buffer, and running the native part under Valgrind can do similar work more slowly. A stress test that calls the library from many threads at once often turns an occasional crash into a reliable one.
4The Usual Causes
Most JNI crashes come from a short list. Using freed memory is the first: Java code calls close() or dispose() on an image and another thread is still processing it, or a cached native handle outlives the object it points to. Local references used across calls are the second: a local reference stored in a static variable in C and used in a later call is invalid by then, and the crash appears far from the bug.
Missing exception checks come next: after a JNI call that throws, such as allocating a large array, the native code continues as if it succeeded, and uses a NULL result. And finally, unsafe concurrent use: many native libraries are not thread-safe, or are thread-safe only per context object. If our service calls the library from 200 request threads sharing one handle, two threads can corrupt the library's internal state.
- Read hs_err_pid: frame, thread, Java stacksame frame every time?
- Replay the traffic with -Xcheck:jniJNI misuse shows up early
- JNI warning found?YesFix reference or exception handlingNoLook inside the library
- Crash only under concurrency?YesSerialise or pool library handlesNoRun an AddressSanitizer build
- Fix, then stress test under loadand pin the library version
5 steps in order. Play them, or step through them one at a time.
5Reducing the Risk
Even after a fix, native code stays risky, so we limit the blast radius. The strongest option is isolation: run image processing in a separate process or a separate small service. If the native library crashes, it kills a worker, which is restarted, and the main service sees a failed call it can retry or report. The cost is serialisation and a network or IPC hop, which for image resizing is usually small compared with the work itself.
Inside the process, we can at least make the usage safe: one library context per thread or a small pool of contexts, all handles wrapped in Java objects with clear ownership and try-with-resources, and no native handle shared after close. Then we pin the library version, both the JAR and the native binary inside it, and only upgrade after a load test that runs representative images, including unusual and corrupted ones, from many threads for long enough to catch rare crashes. Finally, where possible, we consider replacing the JNI library with a pure Java implementation or an FFM-based binding, which removes the C glue code where many of these bugs live.
6Follow-up questions
Why does the crash happen only occasionally?
Memory bugs depend on timing and on what else is in memory. Freed memory may still hold valid data for a while, so using it works until something else reuses that memory. Races between threads need two requests to overlap in a particular way. That is why stress tests with many threads help reproduce them.
Can we catch a SIGSEGV in Java and keep running?
No. A fault in native code leaves the process in an unknown state, and the JVM deliberately aborts. The only reliable protection is to keep the native code out of the process that serves requests.
Where does the JVM write hs_err_pid in a container?
By default in the working directory of the process, which disappears with the container. We set -XX:ErrorFile to a path on a mounted volume, for example /dumps/hs_err_pid%p.log, so the file survives the restart.
7Conclusion
In this answer, we saw that a native crash kills the whole JVM because native code has no safety nets. We read the hs_err_pid file for the faulting frame, library, thread and Java stack, reproduced with -Xcheck:jni and memory tools, and looked at the usual causes: freed memory, local references used across calls, missing exception checks and unsafe concurrent use. To reduce the risk, we isolate native work in a separate process or service and pin and load-test the library version.
Open with
First I would read the hs_err_pid file, ideally from several crashes. The problematic frame tells me which native library and function faulted, the current thread tells me it was a request thread in native code, si_addr tells me whether it looks like a null pointer, and the Java frames show which call into the image library led there and with what arguments.
Then, if they want more
Then I would reproduce in a test environment, replaying the same images with many threads, and run with -Xcheck:jni, which flags JNI misuse like stale references or calls with a pending exception much closer to the cause. If the problem is inside the library, an AddressSanitizer build or a core dump in gdb helps.
The usual causes are use of freed memory, for example an image disposed while another thread uses it, local references cached across calls, missing exception checks after JNI calls, and using a non-thread-safe library from many threads.
To reduce the risk, the best option is to move image processing into a separate process or small service, so a crash kills a worker and not the main service. I would also pin the library version and only upgrade after load tests with real and corrupted images.
About this answer
- Last updated
- 12 November 2026
- 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 "A service using a JNI-based image library crashes occasionally with an hs_err_pid file...": https://scrivio-interview-library.netlify.app/java/foreign-memory-and-native/e36d7b8e69/