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:

  1. Ensure you're logged in as root.
  2. Install iovisor BCC:
    dnf install kernel-devel bcc-tools bpftool
  3. Once the RSS is above a concerning amount, attach BCC memleak, replacing $PID with 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.
  4. After the memory (i.e. RSS/RES) has grown a few GB or so, stop memleak:
    pkill -TERM -f "memleak -p"
  5. Upload diag_memleak*

Background
  1. Linux eBPF is a very low level kernel interface so there are rare risks of process crashes or kernel panics.
  2. Consider testing the performance overhead in a test environment. If it's too high, test the --combined-only and/or -s SAMPLE_RATE options.
  3. 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.
  4. 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
  5. On older versions or custom builds, the command may be memleak.py rather than memleak.
  6. Links:
    1. Compiling iovisor BCC for systems without an installable bcc package
    2. memleak source
    3. memleak examples
    4. RHEL9 discussion of iovisor BCC
    5. bcc-tools is available in RHEL since 7.6
    6. Creating custom eBPF scripts
Script to convert memleak output to CSV
  1. 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";
      }
    }
  2. Make it executable:
    chmod +x linuxebpfmemleak.awk
  3. Run it on the memleak output:
    ~/linuxebpfmemleak.awk diag_memleak*