skip to content

jmap

jmap gives you a class histogram of live objects or a full heap dump for offline analysis in MAT or VisualVM. Interviewers expect you to mention that dumping a large heap pauses the application at a safepoint.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

What is jmap, and what are its two most common uses for inspecting a running Java application's heap?

level: juniorimportance: must knowfreq 62%

answer

  1. JDK heap-inspection tool, attaches by pid
  2. -histo = class histogram (counts + bytes)
  3. -dump = .hprof snapshot for MAT/VisualVM
  4. 'live' forces a GC first
  5. jcmd is the modern replacement

basics

~20 s

jmap is a JDK command-line tool that inspects a running Java program's heap (its object memory). Its two main uses are printing a histogram of how many objects of each class exist, and dumping the whole heap to a file for offline analysis.

solid answer

~50 s

jmap ("Java memory map") is a diagnostic command-line tool shipped with the JDK that connects to a live JVM by its process id and reports on the heap, the region of memory where Java objects live. Two uses dominate. First, `jmap -histo <pid>` prints a class histogram: for every class, how many live instances exist and how many bytes they occupy, sorted by size. It is the fastest way to see what is filling memory. Second, `jmap -dump:live,format=b,file=heap.hprof <pid>` writes a binary heap snapshot (an .hprof file) to disk; you then open that file in Eclipse Memory Analyzer (MAT) or VisualVM to walk object graphs and find leaks offline. jmap attaches to a process you already started; it does not launch one. On modern JDKs `jcmd` provides the same operations and is the recommended front end.

go deeper

for a junior

Knows jmap inspects a running JVM's heap and can name the two uses: histogram and heap dump.

for a middle

Can run both commands with correct flags, knows 'live' forces a GC, and knows to open the .hprof in MAT/VisualVM.

for a senior

Explains when a histogram suffices vs needing a full dump, the permission/attach model, and that jcmd is the modern front end.

for a principal

Frames jmap within a memory-diagnostics strategy: automated dumps on OOM, safepoint/pause cost in prod, and choosing the lightest tool that answers the question.

## What problem jmap solves A running Java program stores its objects in a region of memory called the **heap**. When a program uses too much memory, runs out of memory (an `OutOfMemoryError`), or slowly grows over hours (a **memory leak** — objects that are kept alive by mistake and never freed), you need to look *inside* the heap to see which objects are there. **jmap** ("Java memory map") is a command-line tool that ships with the **JDK** (Java Development Kit) for exactly this: it attaches to an already-running JVM and reports on its heap. ## Key terms, defined - **JVM (Java Virtual Machine):** the process that runs your Java program. Every running Java app is one JVM process with a numeric **pid** (process id). - **Heap:** the memory area where all Java objects (instances of classes) live. The **garbage collector (GC)** automatically frees objects that are no longer reachable. - **Live object:** an object still reachable from the running program (referenced by a thread, a static field, etc.). Live objects cannot be collected; only *unreachable* ones can. - **Histogram:** a table — here, one row per class with a count and a byte total. - **Heap dump:** a complete snapshot of every object in the heap, written to a file, so you can analyze it later on another machine. ## How you use it You first find the pid with `jps` (a JDK tool that lists running Java processes) or `jcmd`. Then: **1. Histogram — `jmap -histo <pid>`** prints rows like `instances bytes class name`, sorted with the biggest consumers at the top. For example you might see millions of `byte[]` and `java.lang.String` instances. Adding `live` (`-histo:live`) first forces a garbage collection so only reachable objects are counted — useful to tell a real leak from short-lived garbage. The histogram answers "*what kind* of object is eating memory?" but not "*who is holding it*." **2. Heap dump — `jmap -dump:live,format=b,file=heap.hprof <pid>`** writes a binary snapshot. The flags mean: `live` = include only reachable objects (runs a GC first); `format=b` = binary (the standard **.hprof** format); `file=` = output path. You then load `heap.hprof` into a heap analyzer such as **Eclipse MAT (Memory Analyzer Tool)** or **VisualVM**, which shows the object graph, **dominator tree** (which object keeps the most memory alive), and **GC roots** (the reference chains keeping a suspected leak alive). This is the workhorse for finding leaks. ## Important boundaries - jmap **attaches** to a process you already started — it does not run your program for you. - It must run as the **same OS user** that owns the target JVM (or as root) because it uses the JVM's attach mechanism. - On current JDKs, `jcmd <pid> GC.class_histogram` and `jcmd <pid> GC.heap_dump <file>` do the same things and are the recommended modern interface; jmap remains for compatibility.

  • How do you find the pid to give jmap?
    Run `jps` (lists running Java processes with their pids and main classes) or `jcmd` with no arguments, which also lists JVMs.
  • Why might jmap fail to attach to a process?
    Most often a permissions mismatch — jmap must run as the same OS user as the target JVM (or root) to use the attach API; otherwise it is denied.

A histogram is like a grocery receipt totaled by category — it tells you milk cost the most. A heap dump is like a photo of your whole fridge — you can open it later and trace exactly which carton is going bad and who left it there.

saying these in an interview costs you the question

  • Thinking jmap launches or profiles your app continuously — it is a point-in-time attach, not a running profiler
  • Believing -histo tells you what is leaking — it shows counts/bytes by class, not the reference chains holding objects
  • Confusing a heap dump (objects) with a thread dump (stacks) — that is jstack, not jmap

context

open as a page

Explain the flags in `jmap -dump:live,format=b,file=heap.hprof <pid>`. What does each part do, and why does 'live' matter?

level: middleimportance: should knowfreq 48%

basics

~20 s

It dumps the heap to a file. live includes only reachable objects (and runs a garbage collection first), format=b writes the standard binary .hprof format, and file=heap.hprof is the output path. 'live' matters because it filters out short-lived garbage so a leak stands out.

open as a page

When debugging a memory issue, when would you reach for `jmap -histo` versus capturing a full heap dump? What can each tell you that the other cannot?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Use -histo for a quick, cheap look at which classes have the most instances and bytes. Capture a full heap dump when you need to know why objects are kept alive — the reference chains and who is holding them — which only an analyzer working on a dump can show.

open as a page

What is the performance and safepoint impact of dumping a large heap with jmap in production, and how would you mitigate it?

level: principalimportance: should knowfreq 34%

basics

~20 s

Dumping a large heap pauses the application: the JVM must stop all threads at a safepoint to walk the object graph, and live adds a full garbage collection on top. On a multi-gigabyte heap this can mean seconds of freeze plus heavy disk and memory pressure.

open as a page

Given a `jmap -histo` output sample, how do you read it and decide which class to investigate first?

level: middleimportance: nice to knowfreq 30%

basics

~20 s

Each row shows a class with how many instances exist and how many bytes they use, sorted with the biggest memory users at the top. Start by looking at the rows with the most bytes, and watch out for your own application classes rather than just generic ones like byte[] or String.

open as a page