How to get Java heap dump from Kubernetes container into a local machine?

What is A Java heap dump?

Imagine you have a bunch of containers running your Java applications in a Kubernetes environment. Each container has its own special space where it stores and manages its memory. If something goes wrong and you suspect a memory issue, you can still use a Java heap dump to help you figure out what’s happening inside those containers. This can help you identify things like memory leaks or excessive memory usage.

Earlier one of our Java application k8s containers has been periodically running out of memory. As we realized the issue, our development team needed to analyze the memory leak of the application.
The primary goal of this article is to quickly get a heap dump at the time of crisis. I would like to explain, How we can get JVM heap memory by manual way.

Let’s take a quick look at the prerequisites to accomplish this task.

  1. You have appropriate permission to exec into the k8s pod. It means you have to kubectl configured a respected environment.
  2. The pod should have Jmap or Jcmd installed
    ============================================================
Let’s do the following step to capture JVM dump and copy it into the local machine

Step-by-Step Guide——->

Step 1: Exec into the Container

First, you need to execute a command inside the Kubernetes pod where your Java application is running. Use the following command:
kubectl exec -it -n [namespace] [pod] /bin/bash

Replace [namespace] with the appropriate namespace and [pod] with the name of the pod containing your Java application.

Step 2: Generate the Java Heap Dump

Once inside the container, use the jmap command to generate the Java heap dump. The heap dump will be saved to a specified file location. The command syntax is as follows:

jmap -dump:[live],format=b,file=[file_path] [PID]

[live]: This option indicates that the heap dump will be captured from a live running JVM.
format=b: Specifies the format of the dump file, which is binary.
file=[file_path]: The path where the heap dump file will be saved inside the container.
[PID]: The Process ID of the Java application. This PID can be found in the Kubernetes pod where the application is running.

Example:
jmap -dump:[live],format=b,file=/home/nonrootuser/dump.bin 001

Step 3: Copy the Dump File to Local Machine

After generating the heap dump, you need to copy it from the Kubernetes pod to your local machine for further analysis. Use the following kubectl cp command:

kubectl cp -n [namespace] [pod]:[absolute path +dump] [destination path]

[namespace]: Replace with the appropriate namespace.
[pod]: The name of the pod where the Java application is running.
[source_file_path]: The absolute path to the heap dump file inside the pod.
[destination_file_path]: The local path on your machine where the heap dump file will be copied.

Example:
kubectl cp -n dump-dev dump-dev-59b8549f54-jzltb:/home/nonrootuser/dump.bin dump.bin

So, even in a fancy Kubernetes containerized setup, a Java heap dump can be your secret weapon for troubleshooting memory issues and keeping your Java applications running smoothly.
Overall, Java heap dumps are valuable tools for diagnosing and resolving memory-related issues in Java applications.

Wrapping Up…

Thanks for keeping up with me till the end of the blog. If you liked the blog please share it amongst your community and do share your views. If you have any questions, let us know through comments !

Blog Pundits:  Mehul Sharma and Sandeep Rawat

OpsTree is an End-to-End DevOps Solution Provider.

Connect with Us

Leave a Reply