When we think about memory leaks in Java, we usually do it for Java heap memory leak which is used to store objects and are if they are not garbage collected ever. But there can be scenarios where native memory in the non-heap memory starts leaking. . The objective of this blog post is to discuss native memory leaks, and how we can detect and troubleshoot them.
Java Memory Model
Java memory pools that the application has access to and can allocate from. We all know, java objects are allocated into the Java Heap. Besides the Java Heap, there are other JVM-managed memory spaces such as Metaspace, Stackspace, Native Area and Reserved. In addition, the JVM and the native code of applications can make allocations out of native memory. These native allocations can be done by JNI allocations, allocations made by the JIT compiler, or the creation of new threads by the JVM. As native allocations can be made from several different places out of your code, it is not easy to debug them.
In normal cases sum of the heap and non-heap memory consumed by JVM should be comparable to the total memory consumption (RSS) of the java application. But if RSS is constantly increasing with no increase in heap and non-heap memory, then you have a native memory leak. RSS though is not a very reliable way as it has some issue tracking exact memory consumed by application use PSS instead. It is basically the sum physical and shared memory per mapper.
Server crashing by Linux out of memory killer which is OS mechanism that frees memory when the system gets to a stressful low memory situation.
java.lang.OutOfMemoryError: requested 1234bytes for ChunkPool::allocate. Out of swap space? Internal Error (allocation.cpp:166), pid=1190, tid=20# Error: ChunkPool::allocate
java.lang.OutOfMemoryError : unable to create new native Thread
You may find these two errors in logs which will tell you that your JVM has run out of native memory. This will happen when you have restricted the max memory of your application or the host itself ran out of memory.
- Any application calls malloc() to acquire memory. If it discards the reference without calling free(),the OS thinks the process has memory but it has already lost the reference to it.
- Could be due to JNI allocations, JIT compiler allocations or the creation of threads
- Class loader leak (java.lang.Class instance could not be garbage collected after deploy) due to this metaspace increases and resulting in increase of RSS.
- Leaking third-party code.
- The jcmd Utility : Consists of NMT + Thread/GC/VM monitoring tools + JFR
- Native Memory Tracking: Tracking native memory leak in JVM
- HPROF : For heap profiling
- Java VisualVM: Memory consumption, running threads, classes monitoring.
- The jdb Utility : For connectors and listeners
- The jmap Utility: Heap dump
- The jstack Utility : Thread dump
- gdb: The GNU Project Debugger
- Core dump analysis
Searching for the leak
Confirm a native memory leak First monitor your application JVM heap and non-heap memory using jconsole and watch -n 1 ps v to monitor rss of the application. Check if PSS for your application is growing over time, calculate it by adding the output for the below command:
cat /proc/<pid>/smaps | grep -i 'Pss:'
When you can confirm the growth of PSS over time and heap more or less remaining constant, this confirms a leak in native memory. As mentioned above every application calls malloc/mmap to acquire memory, and is free to give it back. So to search for memory leak you need to monitor these calls. There are some tools like jemalloc, libtcmalloc, valgrind to do so, each of them will require a library to be preloaded before your application starts. Doing so will enable your logging and these logs can be visualized using a profiler.
Tracking malloc calls and heap profiling Here are the steps to use libtcmalloc.
export LD_PRELOAD=<location to libtcmalloc_and_profiler.so.4
Start your application
To view stats use
pprof –gv <log-directory>.
Native Memory Tracking(NMT) It tracks internal memory usage for a HotSpot JVM and can be enabled by:-XX:NativeMemoryTracking=summary NMT data can be accessed using jcmd utility, you can also view the diff using the baseline and diff command.
jcmd <pid> VM.native_memory summary jcmd <pid> VM.native_memory baseline jcmd <pid> VM.native_memory summary.diff
Limitations with the current version: NMT does not track third-party native code memory allocations and JDK class libraries. It can result in a 5% to 10% JVM performance drop, not advised for the production environment.
Tracking process memory To check the heap and non-heap memory consumed by JVM use jconsole and use the top –p/ ps v command to track rss .
ps -o rss <pid>
Check the memory map for a process and find the diff between the two pmap to see the increase in the memory block.
pmap -x <pid> > pmap.1 0000000100102000 56K rwx-- [ heap ] 0000000100110000 2624K rwx-- [ heap ] <--- native heap 00000001FB000000 24576K rw--- [ anon ] <--- Java Heap starts here 0000000200000000 1396736K rw--- [ anon ] 0000000600000000 700416K rw--- [ anon ] diff pmap.1 pmap.9
Use gdb to debug further
gdp -pid [pid] dump memory mem.bin 0x0000000100110000 0x0000000100110000+2624000
Java Heap dump analysis (ClassLoader Leaks) Acquired multiple heap dumps from dev and production environments, and used MAT to find class-loader leaks that might cause an increase in the metaspace.
jmap -dump:format=b,file=heap_dump.hprof <pid>
jstack <pid>> thread_dump.txt
Use MAT to check old class-loader instances which did not get garbage collected due to strong references/ link to the gc roots.