How to get a heap dump from Kubernetes k8s pod?

17,893
  1. Log in to the K8S and exec into the Pods where your java application is running.
kubectl exec -it herle-deployment-pod-5757d89d85-wrpc9 bash
  1. get the process id (top command)

  2. Create java heap dump

jmap -dump:live,format=b,file=<file_name>.bin <process_id>

Example:

jmap -dump:live,format=b,file=application_heap_dump.bin 1
  1. Copy the heap dump from pod to your local machine.
kubectl cp <pod_name>:<heap_file> <your local destination directory>

Example:

kubectl cp herle-deployment-pod-5757d89d85-wrpc9:/tmp/application_heap_dump.bin /Users/amritharajherle/Desktop/application_heap_dump.bin
  1. Use any memory leak analysis tool. I'm using the Eclipse's Memory Analyzer plugin.
  • Open the heap dump file

Open the heap dump file

  • select leak suspect report

select leak suspects report

  • You can check the number of objects and retained heap space. Also some possible leak suspects.
Share:
17,893
Amrith Raj Herle
Author by

Amrith Raj Herle

IoT Enthusiast,Interested in Big Data,Data Analytics, AI &amp; Machine learning. Docker ¦ Kubernetes ¦ Micro Service ¦ Jenkins ¦ IBM Cloud ¦ PostgreSQL ¦ DB2 ¦ Cloudant ¦ Java(Hibernate, JPA, GWT, Sencha GXT) ¦ Tibco BW ¦ SonarQube ¦ Apache Camel ¦ openAPI ¦ osgi ¦ TensorFlow ¦ Arduino ¦ IBM RAD certified application developer ¦ Agile Development ¦ IBM BPM Java API ¦ AppWay Client onboarding tool (BPMN) ¦ PyQT skills.

Updated on July 25, 2022

Comments

  • Amrith Raj Herle
    Amrith Raj Herle almost 2 years

    Please provide a simple step by step guide to looking into java heap dump from a Kubernetes pod.

  • Vineeth NG
    Vineeth NG about 3 years
    i am getting below error "jmap": executable file not found in $PATH": unknown command terminated with exit code 126.
  • vinayhudli
    vinayhudli almost 3 years
    A side note: better to run jmap without "live" to prevent GC from running before heap dump
  • Ankireddy Polu
    Ankireddy Polu almost 3 years
    kill -3 JAVA_PID Note the process ID number of the Java process (e.g. using top, a grep on ps -axw, etc.) and send a QUIT signal to the process with the kill -QUIT or kill -3 command 1. For example:
  • Gustavo Alexandre
    Gustavo Alexandre over 2 years
    jmap does not exist in adoptopenjdk/openjdk8:alpine-slim java image, however it mostly likey jcmd would be there use this command jcmd 2960 GC.heap_dump c:/temp/heapdump.hprof
  • Admin
    Admin over 2 years
    Please remove /tmp from example you provided in p.4. or add /tmp in p.3
  • chandu ram
    chandu ram over 2 years
    Getting jmap not found error. Using the jdk. openjdk version "11.0.13" 2021-10-19 LTS OpenJDK Runtime Environment 21.10-(Zulu-11.52+13-linux-musl-x64)-Microsoft-Azure-restric‌​ted (build 11.0.13+8-LTS) OpenJDK 64-Bit Server VM 21.10-(Zulu-11.52+13-linux-musl-x64)-Microsoft-Azure-restric‌​ted (build 11.0.13+8-LTS, mixed mode)
  • Natan Yellin
    Natan Yellin about 2 years
    @VineethNG if you don't have jmap present in the image then you can use either ephemeral containers or something like robusta.dev (disclaimer: I'm the maintainer). See home.robusta.dev/java for the latter