17

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

Amrith Raj Herle
  • 773
  • 1
  • 6
  • 18

1 Answers1

24
  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.
Amrith Raj Herle
  • 773
  • 1
  • 6
  • 18
  • i am getting below error "jmap": executable file not found in $PATH": unknown command terminated with exit code 126. – Vineeth NG Feb 01 '21 at 08:19
  • 1
    A side note: better to run jmap without "live" to prevent GC from running before heap dump – vinayhudli May 18 '21 at 16:51
  • 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: – Ankireddy Polu May 28 '21 at 16:28
  • 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 – Raja Nagendra Kumar Aug 06 '21 at 15:33
  • Please remove `/tmp` from example you provided in p.4. or add `/tmp` in p.3 – Viktor Taranenko Sep 29 '21 at 07:55
  • 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-restricted (build 11.0.13+8-LTS) OpenJDK 64-Bit Server VM 21.10-(Zulu-11.52+13-linux-musl-x64)-Microsoft-Azure-restricted (build 11.0.13+8-LTS, mixed mode) – chandu ram Nov 25 '21 at 00:41
  • 1
    @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 https://home.robusta.dev/java for the latter – Natan Yellin Apr 12 '22 at 12:43