Question 22 · Section 3

What tools help analyze memory?

4. Be careful with dumps → STW pause 5. NMT for Native Memory 6. gceasy.io for GC log analysis 7. Arthas for complex environments

Language versions: English Russian Ukrainian

Junior Level

Memory analysis tools:

Tool For what When
Eclipse MAT Dump analysis After OOME
VisualVM Monitoring Development
jcmd Quick check Production
JProfiler Profiling Development

Most important: Eclipse MAT (free)

  • Opens heap dump
  • Shows which objects take most memory
  • Finds leaks

Middle Level

jcmd (command line utility)

jcmd <pid> GC.class_histogram   # List of classes
jcmd <pid> GC.heap_dump dump.hprof  # Take dump
jcmd <pid> VM.native_memory summary  # Native Memory

Eclipse MAT

Key features:
  → Dominator Tree (who retains most memory)
  → Path to GC Roots (reference chain to root)
  → OQL (queries to dump)
  → Leak Suspects Report (automatic analysis)

JFR and JMC

Java Flight Recorder (built into JVM):
  → Overhead < 1%
  → Can keep enabled in production

JDK Mission Control (GUI):
  → JFR record visualization
  → OldObjectSample → leaks without dump

Commercial Tools

Tool Pros Cons
JProfiler Live profiling Paid
YourKit Allocation tracking Paid
gceasy.io GC log analysis Cloud

Senior Level

Dump Danger

Heap Dump = Stop-The-World!
  → 32 GB Heap on HDD → 30-60 second pause
  → 32 GB Heap on SSD → 5-15 seconds

In Kubernetes:
  → Liveness Probe timeout → pod killed!

Solution for large Heaps:
  → JFR OldObjectSample (no STW)
  → class_histogram (pause < 1 sec)

OQL Examples

-- Find all HashMaps with > 1000 elements
SELECT map FROM java.util.HashMap map
WHERE map.size > 1000

-- Find duplicate strings
SELECT toString(s.value) as val, count(*) as cnt
FROM java.lang.String s
GROUP BY toString(s.value)
HAVING count(*) > 100

-- Find ThreadLocal leaks
SELECT tl, tl.referents.@length as size
FROM java.lang.ThreadLocal tl
WHERE tl.referents.@length > 100

Arthas (Alibaba)

# Interactive diagnostics on server
java -jar arthas-boot.jar

[arthas]$ dashboard     # Overview Heap/Native/Metaspace
[arthas]$ heapdump      # Take dump
[arthas]$ vmtool        # Object inspection
[arthas]$ memory        # Memory details

NMT (Native Memory Tracking)

-XX:NativeMemoryTracking=detail

jcmd <pid> VM.native_memory detail

# Shows:
# Java Heap, Class, Thread, Code, GC
# Compiler, Internal, Symbol, Arena Chunk
→ Find leaks outside Heap

Best Practices

  1. jcmd > jmap (modern standard)
  2. JFR for production monitoring
  3. MAT for dump analysis
  4. Be careful with dumps → STW pause
  5. NMT for Native Memory
  6. gceasy.io for GC log analysis
  7. Arthas for complex environments

Senior Summary

  • jcmd = modern CLI standard
  • MAT = Dominator Tree + Path to GC Roots
  • JFR = < 1% overhead, production-safe
  • NMT = Native Memory Tracking
  • Dump = STW (all threads stop). JVM must freeze object graph in consistent state: no allocations, deallocations, or reference changes during dump. On large Heap, dump can take 10-60 seconds.
  • OQL = SQL for dumps
  • Arthas = interactive diagnostics

Interview Cheat Sheet

Must know:

  • Eclipse MAT — dump analysis: Dominator Tree, Path to GC Roots, OQL, Leak Suspects Report (free)
  • jcmd — modern CLI standard: GC.class_histogram, GC.heap_dump, VM.native_memory
  • JFR + JMC — production monitoring, < 1% overhead; OldObjectSample → leaks without dump
  • NMT (-XX:NativeMemoryTracking=detail): Native Memory Tracking — shows Java Heap, Class, Thread, Code, GC, Internal
  • Dump = STW: on 32 GB HDD → 30-60 seconds; in Kubernetes Liveness Probe timeout → pod killed
  • OQL — SQL-like queries to dump: find large HashMaps, duplicate strings, ThreadLocal leaks
  • Arthas (Alibaba): interactive diagnostics on server — dashboard, heapdump, vmtool, memory

Common follow-up questions:

  • Why is jcmd better than jmap? — jmap is deprecated; jcmd is modern standard, uses JVM Attach API, more commands
  • When is JFR better than dump? — Heap > 64 GB: dump too large, takes long to capture/analyze; JFR < 1% overhead, no STW
  • What does NMT show that Heap Dump doesn’t? — Native Memory: Code Cache, Thread Stacks, GC structures, Internal — everything outside Heap
  • Why does dump require STW? — JVM must freeze graph in consistent state: no allocations/deallocations during dump

Red flags (DO NOT say):

  • “I take dumps in production without warning” — STW 30-60 seconds; in Kubernetes pod will be killed
  • “jmap is the best tool” — jmap is deprecated; use jcmd
  • “Heap Dump shows all JVM memory” — Heap Dump only On-Heap; Native Memory requires NMT

Related topics:

  • [[21. What is a memory leak and how to detect it]]
  • [[23. What is a heap dump]]
  • [[24. How to get a heap dump]]
  • [[19. What happens on OutOfMemoryError]]
  • [[18. What are -Xms and -Xmx parameters]]