VWhat happens when the JVM runs out of heap memory?
Interviewer questionJVM
Interviewers for Java roles often move from the language to the runtime: how memory is organised, how objects are cleaned up and what you do when a production service runs out of memory. Even a mid-level candidate is expected to know the main ideas and the right tools.
Give answers in this order: what the part does, how it works in simple terms, and how you would see it in a real system, for example in a GC log or a heap dump. Be careful with version-specific details, and say "in modern Java" or "it depends on the version" when you are not sure.
- ✓Words → phrases → sentences → speaking
- ✓Answer meaning
- ✓Language
Common questions and sample answers
How does garbage collection work in the JVM?
What the interviewer wants to hear. Reachability from roots, the generational idea, the difference between short and long pauses, and at least one named collector.
Sample answer to practise aloudThe JVM frees the memory of objects that can no longer be reached from the roots, such as thread stacks and static fields. Most objects die young, so the heap is usually divided into a young generation and an old generation. New objects go to the young area, and a small, fast collection cleans it. Objects that survive move to the old area, which is collected less often. Some phases stop the application, and we call that a stop-the-world pause. In modern Java, G1 is usually the default collector, and it tries to keep pauses short. ZGC and Shenandoah aim for even shorter pauses. I choose by the pause and throughput requirements, and I check the GC logs.
What are the main memory areas of the JVM?
What the interviewer wants to hear. Heap, thread stacks, metaspace and native memory, and what is stored where. Bonus: which error points to which area.
Sample answer to practise aloudThe biggest area is the heap, where objects are created and where the garbage collector works. Each thread has its own stack, which holds method frames with local variables and references. When the stack gets too deep, for example with endless recursion, we get a StackOverflowError. Class metadata is stored in metaspace, which uses native memory and replaced the old permanent generation in Java 8. There is also memory outside the heap: thread stacks, direct buffers and the code cache with compiled code. This is important in containers, because the process uses more memory than the heap size alone.
How does class loading work in the JVM?
What the interviewer wants to hear. Loading, linking and initialisation, the parent delegation model, and the difference between ClassNotFoundException and NoClassDefFoundError.
Sample answer to practise aloudThe JVM loads classes lazily, when they are first needed. A class loader finds the bytecode and creates the class. Then the class is linked, which means it is verified and prepared, and finally it is initialised: static fields are set and static blocks run. Class loaders form a hierarchy with parent delegation. A loader first asks its parent to load the class, and only then tries by itself. This protects core classes like String from being replaced. ClassNotFoundException happens when code asks for a class by name and it is not found. NoClassDefFoundError usually means that the class existed at compile time but is missing at runtime, or that its static initialisation failed.
How do you investigate a memory leak or an OutOfMemoryError?
What the interviewer wants to hear. A practical sequence: confirm with GC logs and metrics, take a heap dump, analyse it, find the reference chain and fix the cause.
Sample answer to practise aloudFirst I check whether it is really a leak. In the GC logs or the metrics, I look at whether the memory after a full collection keeps growing. I make sure the JVM writes a heap dump on OutOfMemoryError, or I take one with jcmd. Then I open it in a tool like Eclipse Memory Analyzer and look for the biggest objects and the path from a GC root that keeps them alive. Typical causes are static collections that only grow, caches without a size limit, listeners that are never removed and ThreadLocal values in long-living threads. After the fix, I watch the memory again under similar load.
How do you tune a JVM for a production service?
What the interviewer wants to hear. Measure first; heap size, collector choice, container limits and warm-up; one change at a time with a clear goal.
Sample answer to practise aloudI start with a goal: low latency, high throughput or small memory use. Then I measure, and I never tune by guessing. I enable GC logging, set the maximum heap with Xmx, and often set the initial heap to the same size to avoid resizing. In a container, I leave room for non-heap memory, so I set the heap as a percentage of the container limit, for example with MaxRAMPercentage. I change one parameter at a time and test under realistic load. I also remember that the JIT compiler needs warm-up, so the first minutes after a start are slower, and a load test must run long enough to show the real picture.
How to say the key terms
- JVM
- J-V-M
- heap
- heep (long "ee", not "hip")
- metaspace
- MET-uh-spayss
- bytecode
- BYTE-code
- JIT
- jit (rhymes with "bit"), or J-I-T
- allocation
- al-uh-KAY-shun
- generation
- jen-uh-RAY-shun
Useful phrases for answering
- Under the hood, the JVM…
- The GC logs show that…
- I would take a heap dump and look at…
- The trade-off is between pause time and throughput.
- It depends on the JVM version and the collector.
- The object stays reachable because…
- I would measure under realistic load first.
Common mistakes to avoid
- Mixing up the Java memory model with the JVM memory areas. The first is about visibility between threads; the second is about heap, stack and metaspace. Say which one you mean.
- Saying "the garbage collector deletes unused objects" and stopping. Explain reachability from roots and the idea of generations. This is the most common weak point.
- Suggesting a bigger heap as the answer to a memory leak. A larger heap only delays the failure. Say that you find the reference that keeps the objects alive.
- Quoting flags from memory without the version. Many options changed between Java versions, so say "in recent versions" and check the documentation.
- Pronouncing "heap" like "hip". It has a long vowel, as in "cheap". Practise the key words aloud, because a wrong vowel can change the word you mean.
Frequently asked questions
Do I need to know GC algorithms in detail?
You need the main ideas: reachability, generations, pauses, and one or two collectors with their goals. Details such as region sizes or tuning flags are expected only for senior or performance-focused roles.
Which JVM tools should I mention?
Name the ones you used: GC logs, jcmd, jstack for thread dumps, Java Flight Recorder and a heap analyser such as Eclipse Memory Analyzer. If you only used a monitoring dashboard, say that and explain what you looked at.
Should I mention the Java version?
Yes. Say which version your last project used and note that some behaviour differs between versions. It shows that you know the runtime changes, and it protects you from claiming something that is true for only one version.
Reading an answer is not the same as saying it. Practise each question out loud, then compare the meaning and the language of your own answer.
Try SpeakRole ↗Other topics in this area