How to do scala heap dump in Kubernetes in Azure
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
A Scala service running on Azure Kubernetes Service is still a JVM process, so heap-dump collection works the same way as for any Java application. The Azure-specific part is mostly about getting cluster access and making sure the dump has somewhere safe to be written, because heap dumps are large and can easily overwhelm container storage.
Get Access to the AKS Cluster and Pod
Start by connecting kubectl to the AKS cluster:
Then identify the target pod and container. If the pod has more than one container, include -c <container-name> in later commands so the dump runs in the correct JVM container.
Use jcmd or jmap Inside the Container
If the image includes JDK tools, the cleanest option is jcmd:
On many containerized JVM apps, the main Java process is PID 1. If you are unsure, inspect the process list first:
If jcmd is unavailable but jmap exists, this also works:
The live option reduces dump size by keeping only live objects, which can make analysis easier during production incidents.
Copy the Dump Out of the Pod
After the dump is written, copy it to your workstation:
You can then open the file in Eclipse MAT, VisualVM, or another heap-analysis tool.
This is often enough for one-off debugging, but it depends on the container having enough writable space for a large .hprof file.
Use a Persistent Volume for Safer Dumps
Heap dumps can be hundreds of megabytes or larger. Writing them to /tmp inside the container is risky if the writable layer is small. A better pattern is to mount a volume dedicated to diagnostics.
Example deployment fragment:
Then dump to that path instead:
In Azure, that PVC might be backed by Azure Disk or Azure Files depending on your storage class and operational needs.
Configure Automatic Dumps on OOM
If the service is crashing with OutOfMemoryError, manual collection may be too late. In that case, configure the JVM to write a dump automatically.
This is especially useful for intermittent production failures because the dump is captured at the time of the crash rather than after a restart erased the evidence.
Common Pitfalls
- Using a JRE-only image leaves you without
jcmdorjmap. If that happens, switch to a JDK-based image for debugging or attach a debug container with the needed tools. - Writing the dump to container-local storage can fail when the file is larger than the writable layer.
- Forgetting the namespace or container name leads to confusing
kubectl execerrors in AKS environments with many workloads. - Taking a heap dump from a busy production JVM can pause the process and consume significant CPU and I/O. Schedule carefully.
Summary
- A Scala service on AKS is still a JVM process, so heap dumps are collected with standard Java tools.
- Use
kubectl execwithjcmdorjmap, then copy the.hproffile out withkubectl cp. - Prefer a mounted diagnostic volume over
/tmpfor large dumps. - Enable automatic OOM heap dumps when the failure is hard to reproduce manually.
Related reading
- How to Dockerfile FROM another Dockerfile?
- How to edit configmap configuration in spring boot kubernetes application during runtime
- how to enable api flags in kubernetes
- How to enable Client Certificate Authentication with Traefik Kubernetes?
- How to download a file from EC2 instance to Local Computer
- How to download data from Amazon's requester pay buckets?
- How to download graphs from tensorboard?
- How to download/search mailing list archives for Apache projects?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.