Scrivio Interview library

Memory areas and object layout

23 questions in 8 subtopics.

Heap and generations

ConceptFoundations7 min read
Explain the weak generational hypothesis and how the heap is organised in Parallel GC compared with G1.
ScenarioIntermediate7 min read
A service runs with -Xmx4g on G1. GC logs show old generation occupancy after each young GC climbing from 1 GB to 3.5 GB over six hours; then a full GC takes it back to 1 GB and the cycle repeats. Is this a memory leak? How do you tell, and what do you change?
DesignAdvanced6 min read
For a stateless JVM service with steady traffic, the team must choose between running a few large instances with 32 GB heaps or many small instances with 4 GB heaps. How do you decide?
LeadershipPrincipal6 min read
JVM memory management moved from many separately sized areas (a fixed PermGen, fixed young and old sizes) toward adaptive, region-based and self-sizing designs. Explain why, and how this trend should shape your company's memory configuration standards.

Thread stacks

ConceptFoundations6 min read
What is stored on a Java thread stack, and what causes StackOverflowError compared with OutOfMemoryError: unable to create native thread?
DebuggingIntermediate6 min read
A container with a 2 GiB memory limit is OOM-killed by the kernel. Heap usage was about 1 GiB of -Xmx1200m. The application has around 1,500 platform threads. Walk through where the rest of the memory went and what you would change.
ScenarioAdvanced6 min read
An organisation plans to cut memory across 200 services by moving request handling from large platform thread pools to virtual threads. What memory effects do you expect, and how do you roll it out?

Metaspace

ConceptFoundations5 min read
What lives in metaspace, how is it different from the PermGen used before Java 8, and which flags control it?
LeadershipPrincipal6 min read
HotSpot removed PermGen in JDK 8 and replaced it with metaspace in native memory, and later made metaspace return memory to the OS more promptly. Why was this change made, what did it fix and not fix, and what lesson does it hold for running JVMs in containers today?

Code cache

ConceptFoundations5 min read
What is the code cache in HotSpot, what is stored there, and how can you see how full it is?
DebuggingIntermediate6 min read
A large service logs "CodeCache is full. Compiler has been disabled." and latency degrades gradually over the following hours. Explain what is happening and how you fix it.

Direct and native memory

ConceptFoundations6 min read
What is the difference between a heap ByteBuffer and a direct ByteBuffer in Java NIO? When is the memory of a direct buffer freed?
DebuggingIntermediate6 min read
A Java process reaches 6 GB resident memory with -Xmx3g. Native memory tracking totals look reasonable and far below 6 GB. What else could be using native memory, and how do you find it?
DesignAdvanced6 min read
Define a memory budget policy for JVM services running in Kubernetes. It should cover heap, non-heap areas and the container limit so that teams stop getting kernel OOM kills.
LeadershipPrincipal6 min read
Java gained several ways to manage memory outside the heap over the years: direct ByteBuffers, sun.misc.Unsafe, and the Foreign Function and Memory API, final in JDK 22. For systems that keep large data off heap, what long-term strategy do you set?

Object headers and layout

ConceptFoundations6 min read
What does an object header contain in 64-bit HotSpot, and what information does the mark word hold?
InternalsIntermediate6 min read
On 64-bit HotSpot with default compressed oops and compressed class pointers, estimate the size of new Object(), an Integer, and a String holding 5 ASCII characters. How would you verify your estimate?
ScenarioAdvanced6 min read
JDK 25 offers compact object headers as a product feature that is off by default. Your platform team asks whether to enable it across the fleet. How do you decide and roll it out?

Compressed oops

ConceptFoundations5 min read
Why does a reference field take 4 bytes on most 64-bit JVM heaps but 8 bytes on some? What setting decides this?
ConceptIntermediate6 min read
What are compressed oops, why does a heap of 32 GB or more lose them, and why can -Xmx31g give more usable space and better performance than -Xmx33g?

Escape analysis and scalar replacement

ConceptFoundations5 min read
Does Java ever allocate objects on the stack? Explain what escape analysis lets HotSpot do instead.
InternalsIntermediate6 min read
Consider:
record Point(int x, int y) {}
long sum(int n) {
long s = 0;
for (int i = 0; i < n; i++) {
Point p = new Point(i, i);
s += p.x() + p.y();
}
return s;
}
Will this allocate n Point objects in steady state? How would you check?
LeadershipPrincipal6 min read
Project Valhalla aims to add value classes that let the JVM flatten objects and avoid headers and indirection, but this is not a final feature in JDK 25. How should a company with memory-heavy Java systems plan around it over several years?