Quick revision
The short answer
First we save what a restart would destroy: docker inspect output and the recent logs with docker logs --since 30m. Then docker stats and docker top show the container's CPU, memory and processes. Inside the container, jcmd gives several thread dumps a few seconds apart, heap information and a 60 second JFR recording that we copy out with docker cp. If the image has no JDK tools, we run a JDK container that shares the target's PID namespace with --pid=container:<name> and use jcmd from there.
What to remember
In this answer, we gathered incident evidence from a busy Java container without restarting it. We saved docker inspect and recent logs first, confirmed CPU and memory with docker stats and docker top, took several thread dumps with jcmd and compared per-thread CPU, checked the heap, recorded 60 seconds of JFR and copied it out with docker cp, and used a JDK container sharing the PID namespace when the image had no tools.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
First we save what a restart would destroy: docker inspect output and the recent logs with docker logs --since 30m. Then docker stats and docker top show the container's CPU, memory and processes. Inside the container, jcmd gives several thread dumps a few seconds apart, heap information and a 60 second JFR recording that we copy out with docker cp. If the image has no JDK tools, we run a JDK container that shares the target's PID namespace with --pid=container:<name> and use jcmd from there.
Why this question is asked
During an incident, the fastest fix is often a restart, and a restart throws away the only evidence of what went wrong. The next time it happens, we are no wiser. Interviewers ask this question to see whether we can collect useful evidence quickly and safely, with nothing more than the Docker CLI and the tools in the JDK, and whether we know what each piece of evidence tells us. In this answer, we will preserve the container's state first, confirm what is using the CPU, take and compare thread dumps, look at memory and GC, record JFR, and handle images that do not ship JDK tools.
1Save What a Restart Would Lose
Someone may restart the container while we are still looking, so we start by saving the cheap, perishable evidence to files on the host. docker inspect holds the configuration, limits, environment and state. docker logs holds the application's output; --since 30m limits it to the incident window. These two commands take seconds.
Notice the 2>&1: docker logs replays the container's stderr on our stderr, and Java stack traces are often written there.
2Confirm What Is Using the CPU
docker stats shows live CPU, memory, network and disk I/O per container; --no-stream prints one snapshot, which is easy to save. docker top lists the processes inside the container as seen from the host. Together they tell us whether the Java process itself is busy, whether the container is near its memory limit, and whether something else is running in it.
CPU above 100 percent means more than one core. Memory at 85 percent of the limit is a hint that the CPU might be the garbage collector working hard, which the next steps will confirm or rule out.
3Take Several Thread Dumps
A thread dump lists every Java thread with its state and stack. One dump is a snapshot; three to five dumps taken five to ten seconds apart show which threads stay busy and where. jcmd is the JDK's diagnostic command tool. Inside the container the Java process is usually PID 1, so we pass 1. Recent JDKs print cpu= and elapsed= for each thread, so comparing the cpu value of the same thread across dumps shows which threads are burning CPU.
8 messages in order. Play them, or step through them one at a time.
Each step is read-only for the application; only JFR adds a small overhead, usually around 1 percent.
A request thread that used 15 seconds of CPU in 15 seconds, always inside the same regular expression, is a strong lead, for example a catastrophic backtracking pattern on some input. If instead the busy threads are named GC Thread or G1 Conc, the problem is memory, not code.
4Check Memory and Record JFR
jcmd 1 GC.heap_info prints the heap's size and occupancy per region or generation. If the heap is nearly full and stays full after collections, the CPU is going into GC and the next step is a class histogram (jcmd 1 GC.class_histogram) or, carefully, a heap dump, which pauses the JVM and can be very large.
Java Flight Recorder (JFR) records CPU samples, allocations, GC pauses, locks and I/O with low overhead. A 60 second recording during the incident is often the single most useful artefact, because it shows the hot methods and the GC behaviour together. JFR writes inside the container, so we copy the file out with docker cp.
We open rec.jfr later in JDK Mission Control, or summarise it with jfr print --events jdk.ExecutionSample rec.jfr | head on any machine with a JDK.
5When the Image Has No JDK Tools
Many production images contain only a JRE or are distroless, so jcmd and even sh are missing. Docker lets one container join another's PID namespace: with --pid=container:orders, a new container sees the target's processes as its own. We start a full JDK image that way and run jcmd from it. The tools container should run as the same user as the Java process, because the attach mechanism checks ownership; check the details for your JDK version. The JDK version should match the target's major version.
If attaching is not possible at all, a SIGQUIT still makes the JVM print a thread dump to its stdout: docker kill --signal=QUIT orders sends it without stopping the process, and docker logs then shows the dump. Despite its name, docker kill with QUIT does not kill a Java process.
6Follow-up questions
Why take several thread dumps instead of one?
A single dump cannot tell a busy thread from one that happened to be running at that instant. Several dumps a few seconds apart show which threads stay RUNNABLE in the same code and, with the cpu= values, how much CPU each used between dumps.
Is it safe to take a heap dump during the incident?
It stops the JVM while the dump is written and can produce a file as large as the heap, which may fill the container's disk. It is very valuable for memory leaks, but we take it only after the cheaper evidence is saved, and we write it to a volume with enough space.
What if the problem is in native code or the kernel, not Java?
Then Java tools show threads in RUNNABLE inside native methods or nothing obviously busy. Host tools such as top -H on the process's PID, perf or async-profiler in native mode help, and docker top gives the host PID to target.
7Conclusion
In this answer, we gathered incident evidence from a busy Java container without restarting it. We saved docker inspect and recent logs first, confirmed CPU and memory with docker stats and docker top, took several thread dumps with jcmd and compared per-thread CPU, checked the heap, recorded 60 seconds of JFR and copied it out with docker cp, and used a JDK container sharing the PID namespace when the image had no tools.
Open with
My first priority is to keep the evidence, because the first thing someone will want to do is restart. So I save docker inspect output and docker logs --since 30m to files on the host.
Then, if they want more
Then docker stats --no-stream tells me CPU, memory against the limit and I/O for the container, and docker top shows which process inside is busy. Next I take three or four thread dumps a few seconds apart with docker exec orders jcmd 1 Thread.print, since Java is usually PID 1 in the container.
Comparing them, and the per-thread cpu values that recent JDKs print, shows which threads are burning CPU and in which code, for example a request thread stuck in a regex, or GC threads if memory is the problem. I check jcmd 1 GC.heap_info to see whether the heap is nearly full.
Then I start a JFR recording with jcmd 1 JFR.start duration=60s filename=/tmp/rec.jfr and copy it out with docker cp for analysis in Mission Control.
If the image has no JDK tools, I run a JDK container with --pid=container:orders and the same user, and use jcmd from there, or send SIGQUIT with docker kill --signal=QUIT to get a thread dump in the logs. Only after all that would I agree to restart.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Kubernetes documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "During an incident, a Java container in a Docker host (not Kubernetes) uses 100 percent...": https://scrivio-interview-library.netlify.app/spring-boot/containers-and-kubernetes/ff6342780a/