Linux eBPF memleak Recipe
On Linux >= 4.1 (e.g. RHEL >= 7.6), iovisor BCC tools include a memleak
eBPF tool that can be used to dynamically attach to a process and
monitor unfreed native memory allocations in a lightweight way:
- Ensure you're logged in as
root. - Install iovisor BCC:
dnf install kernel-devel bcc-tools bpftool - Once the RSS is above a concerning amount, attach BCC memleak,
replacing
$PIDwith the process ID of the JVM:nohup /usr/share/bcc/tools/memleak -p $PID 300 &> diag_memleak_$(hostname)_$(date +"%Y%m%d_%H%M%S").txt &- Monitor the disk usage of
diag_memleak*txt. The example uses a 300 second interval, so if that is producing too much output but the growth is slow, then detach and re-attach with a larger interval.
- Monitor the disk usage of
- After the memory (i.e. RSS/RES) has grown a few GB or so, stop
memleak:pkill -TERM -f "memleak -p" - Upload
diag_memleak*
Background
- Linux eBPF is a very low level kernel interface so there are rare risks of process crashes or kernel panics.
- Consider testing the performance overhead in a test environment. If
it's too high, test the
--combined-onlyand/or-s SAMPLE_RATEoptions. - In some cases, eBPF may not be able to perform stack walks so you'll only see the proximate shared library. This is usually due to libraries being compiled with frame pointer omission (FPO). For example, IBM Java and IBM Semeru Runtimes are compiled with FPO. eBPF may still be useful in such cases as a first pass to find issues in non-FPO librarires or to get an idea of where the issues may be since eBPF can attach/detach dynamically; otherwise, for IBM Java and IBM Semeru Runtimes, consider using LinuxNativeMemoryTracker (available with IBM Java 8 or contact IBM Support for IBM Semeru Runtimes), although this requires a restart.
- Each iteration prints the 10 largest unfreed allocations (
-T 10), so if you know the leak is currently happening, you can just set the number of iterations to 1 so that it exits after the duration; for example:/usr/share/bcc/tools/memleak -p $PID 300 1 &> diag_memleak_$(hostname)_$(date +"%Y%m%d_%H%M%S").txt - On older versions or custom builds, the command may be
memleak.pyrather thanmemleak. - Links:
Script to convert memleak output to CSV
- In Unix or Cygwin, create
linuxebpfmemleak.awk:#!/usr/bin/awk -f # Example input: # [18:39:56] Top 10 stacks with outstanding allocations: # 134217728 bytes in 1 allocations from stack # 0x00007fbb3b697777 new_heap+0xa7 [libc.so.6] /stacks with outstanding allocations:/ { nrows++; row_time[nrows] = substr($0, 2, index($0, "]") - 2); } /allocations from stack/ { bytes = 0 + $1; want_frame = 1; next; } want_frame { want_frame = 0; if (justlibrary) { driver = $NF; } else { driver = $(NF-1) " " $NF; } gsub(/[\[\]]/, "", driver); if (!(driver in seen)) { seen[driver] = 1; cols[++ncols] = driver; } val[nrows, driver] += bytes; } END { printf "Time"; for (c = 1; c <= ncols; c++) { printf ",%s", cols[c]; } printf "\n"; for (r = 1; r <= nrows; r++) { printf "%s", row_time[r]; for (c = 1; c <= ncols; c++) { printf ",%d", ((r, cols[c]) in val) ? val[r, cols[c]] : 0; } printf "\n"; } } - Make it executable:
chmod +x linuxebpfmemleak.awk - Run it on the memleak output:
~/linuxebpfmemleak.awk diag_memleak*