skip to content

Runtime Memory Areas

Where a running program's memory actually sits — heap generations, per-thread stacks, class metadata, program counters, and off-heap regions — and which parts are shared versus per-thread. Interviewers ask because almost every OutOfMemoryError or StackOverflowError question is really a question about which area filled up.

part ofJVM internalsread it →
on this pageshow

explore

questions

page 2 of 2

The JVM specification states that a thread's program counter register value is undefined while that thread is executing a native (JNI) method. Why, and what does that imply for stack traces?

level: middleimportance: nice to knowfreq 26%

basics

~20 s

The PC register addresses JVM bytecode. Native code is machine code with no bytecode offsets, and its execution point is tracked by the hardware program counter, so the JVM promises nothing about the value until the thread returns to a Java frame.

open as a page

The JVM specification defines StackOverflowError or OutOfMemoryError conditions for most runtime data areas, but defines no error condition for the program counter register. Why is that consistent?

level: seniorimportance: nice to knowfreq 15%

basics

~20 s

Because it is fixed-size and allocated once per thread. It never grows with recursion or allocation, so it cannot be exhausted. The only adjacent failure is being unable to create a thread at all, reported as OutOfMemoryError on thread creation.

open as a page

The JVM specification describes a 'native method stack' alongside the Java virtual machine stack. What is that second stack for, and how does HotSpot actually arrange the two for a running thread?

level: seniorimportance: nice to knowfreq 28%

basics

~20 s

The Java stack holds frames for bytecode methods; the native method stack holds frames for methods written in C or other native languages, reached through JNI, which have no bytecode and no operand stack. The specification allows them to be separate; HotSpot interleaves Java and native frames on one OS thread stack sized by -Xss.

open as a page

How does HotSpot decide how large each thread's private allocation buffer should be, and when, if ever, would you override that decision with tuning flags?

level: principalimportance: nice to knowfreq 18%

basics

~20 s

HotSpot sizes each buffer adaptively from that thread's recent allocation history and the young space available per thread, resizing at each collection. Overriding is rare; the real cases are pathological thread counts, tiny heaps, or workloads where logging shows excessive refills.

open as a page

The weak generational hypothesis has two halves: most objects die young, and references from older objects to younger ones are rare. Describe workloads where each half fails, what that costs at runtime, and how you would change heap or collector strategy in response.

level: principalimportance: nice to knowfreq 30%

basics

~20 s

When most data survives (large caches, in-memory stores, long batch working sets) copying collectors pay to move it repeatedly and the old generation dominates. When old objects are constantly rewritten to point at new ones, write-barrier and remembered-set costs dominate instead. Responses: size the young generation to the real lifetime distribution, reduce mutation of long-lived graphs, or choose a collector whose old-generation work is concurrent.

open as a page

showing 31–35 of 35