Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 4 OF 4 · 2 MIN READ

Diagnosing High CPU, Memory Leaks and Slow Java Services

Senior Java interviews ask what you would do when a service has high CPU, growing memory or slow responses in production. The answer is a calm process with JDK tools: thread dumps, heap dumps, GC logs, JFR and metrics.

What you will learn

  • Find the thread burning CPU
  • Find a memory leak from a heap dump
  • Use Java Flight Recorder for low-overhead profiling
  • Know the Spring Boot Actuator endpoints that help

Key concepts

Thread dump

A snapshot of every thread's stack (jstack <pid> or jcmd <pid> Thread.print). Take three a few seconds apart; threads stuck in the same place show the problem: a deadlock, a lock everyone waits on, or a hot loop.

Heap dump

Every object in the heap (jcmd <pid> GC.heap_dump file.hprof). Open it in Eclipse MAT or VisualVM and look at the dominator tree for what holds the most memory.

JFR

Java Flight Recorder records CPU samples, allocations, locks, GC and I/O with about 1% overhead, safe in production. Analyse in JDK Mission Control.

Step by step

High CPU runbook

top -H -p <pid>                       # find the hottest thread id (decimal)
printf "%x\n" <tid>                  # convert to hex
jstack <pid> | grep -A 20 "nid=0x<hex>"  # see what that thread is running

# or record a minute of profiling
jcmd <pid> JFR.start duration=60s filename=/tmp/rec.jfr

Memory leak runbook

  • Confirm: old-gen usage after each full GC keeps rising
  • Take two heap dumps some time apart
  • Compare the dominator trees: which collection keeps growing?
  • Follow the path to GC roots to find who holds it (a static cache, a listener, a ThreadLocal)

Examples

Useful Actuator endpoints

management.endpoints.web.exposure.include=health,info,metrics,prometheus,threaddump,heapdump

GET /actuator/metrics/jvm.memory.used
GET /actuator/metrics/http.server.requests?tag=uri:/api/orders
GET /actuator/threaddump
# protect these endpoints: heapdump can contain secrets and personal data

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Write a tiny app that leaks memory through a static map, then find it with a heap dump.
  • Create a deadlock, take a thread dump and read the "Found one Java-level deadlock" section.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

Responses are slow but CPU is low. What do you check?

Threads are waiting, not working: look at a thread dump for threads blocked on locks, a full connection pool (HikariCP pending connections), slow downstream calls without timeouts, or long GC pauses. Traces show which dependency is slow.

Summary

Use thread dumps for CPU and hangs, heap dumps and the dominator tree for leaks, GC logs for pauses, JFR for low-overhead profiling, and Actuator metrics to see trends, protected from public access.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus