Solving OutOfMemoryError: Native Method Stack Trace Analysis


Out of memory errors in Java are fairly common. Some variations of these, however, are rare. In this article, we’ll take a look at an error message that’s seldom seen: java.lang.OutOfMemoryError: reason stack_trace_with_native_method
. This is one of the more difficult errors to debug, especially since documentation on fixing this particular glitch is almost as rare as the error itself.
Out of Memory Errors in Java
The JVM memory is divided into various pools, each with a distinct purpose. This is illustrated in the diagram below. The error java.lang.OutOfMemoryError: reason stack_trace_with_native_method
relates to the JNI area, highlighted in red.
Fig: JVM Memory Areas
It makes sense, then, that there are several different types of OutOfMemoryError
, each with different causes and different debugging strategies.
In fact, there are nine different types of Out of Memory Error that you’re likely to encounter.
The first step in debugging is to carefully read the first line of the error message, which clearly states the reason: in this case, the reason will be stack_trace_with_native_method
.
For more information on the JNI memory area, and how this error relates to it, you may like to read this blog post: Java Out of Memory Error: Reason Stack Trace Native Method.
Out of Memory Error -- Reason: Stack Trace With Native Method
This error message tells us two things:
Memory allocation has failed due to lack of space;
The stack trace contains a native method.
From this, we understand that a native method has been unable to successfully allocate memory. The memory area that has run out of space is the JNI area. This is used by native methods invoked using JNI (Java Native Interface). We only ever encounter this error in programs that include JNI.
JNI is seldom used in modern applications, because:
Much of the functionality that used to be achieved by using native libraries is available directly within Java in later JVM versions;
Newer technologies, such as the FFM (Foreign Function and Memory API), offer better ways of incorporating libraries written in different languages within Java.
If we examine the stack trace which follows this error message, the top frame of the stack will be a native method.
Probable Causes
This error is likely to have been caused by one of the following:
Extensive use of JNI, especially when using multithreading. If this is the case, you can use the
-Xss
switch when invoking the JVM to control the size of native threads.Recursive calls to methods that invoke native code. You should be able to identify this by working back through the stack trace.
Memory leaks within the native code. You will need to debug the native code to solve this.
Debugging java.lang.outofmemoryerror
: Native Method Stack Trace Analysis
For this type of error, heap analysis tools such as HeapHero are not likely to be helpful, since the problem doesn’t involve the heap.
Our primary debugging technique is to use native method stack trace analysis to follow through the logic of the program. Look for recursive method calls, and use the stack to identify the exact point where the program failed.
A few debugging suggestions include:
Native debugging tools such as
DTrace
,pmap
andpstack
. You can learn about these in Oracle’s documentation.Hotspot probes. These are also documented in the Oracle page linked above.
Enabling native memory tracking in the JVM using the switch
-XX:NativeMemoryTracking=summary
. We’d use this in conjunction with requesting a native memory summary viajcmd
, using the syntaxjcmd VM.native_memory summary
.
Conclusion
The error: java.lang.OutOfMemoryError: reason stack_trace_with_native_method
is fortunately rare, since it only occurs when using JNI. It’s one of the most difficult errors to trace. For this type of java.lang.outofmemoryerror
, native method stack trace analysis is the primary debugging tool. Since it doesn’t involve the heap, standard debugging tools are unlikely to help.
Operating-system specific debugging tools can also be extremely useful in tracing the cause of the problem.
In this article, we’ve looked at what the error means, what’s likely to cause it, and some debugging tools that may be helpful.
Your comments and suggestions would be appreciated.
Subscribe to my newsletter
Read articles from Jill Thornhill directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
