What is A Java heap dump?
Imagine you are running multiple Java applications within containers in a Kubernetes environment. Each container manages its own memory, but when something seems to be wrong, a Java heap dump becomes a useful tool for diagnosing the problem.This can help detect problems such as memory leaks or abnormally high memory consumption.
Recently, one of our Java application containers in Kubernetes started running out of memory intermittently. Noticing this, our development team immediately sprung into action and analyzed the suspected memory leak in the application.
The main purpose of this article is to show you how to quickly get a heap dump at these critical moments. I will walk you through the manual steps needed to reclaim JVM heap memory when you need it the most.
Let’s take a quick look at the prerequisites to accomplish this task.
- You have appropriate permission to exec into the k8s pod. It means you have to kubectl configured a respected environment.
- The pod should have Jmap or Jcmd installed
[ Are you looking DevSecOps Managed Services]
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
After moving to the container, you can generate a Java heap dump using the jmap command. This will save the heap dump to a specified file location. The structure of this command 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 creating the heap dump, the next step is to transfer it from the Kubernetes pod to your local machine for in-depth analysis. You can get it using 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
Even in a sophisticated Kubernetes container environment, using Java heap dumps can be a powerful strategy for troubleshooting memory issues and ensuring that your Java applications work seamlessly.In short, Java heap dumps are an essential tool for identifying and resolving memory-related challenges in Java applications. As a trusted DevOps service provider, we help enterprises implement such best practices to achieve greater reliability and performance in their cloud-native ecosystem.
Conclusion
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 ! As a trusted provider of cloud engineering services, we help organizations simplify Kubernetes adoption, optimize containerized workloads, and build flexible, scalable cloud-native platforms.