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 pageshowhide
explore
- Heap & Generational Layout1 question
- Thread Stacks & Stack Frames6 questions
- Class-Metadata Area (Metaspace)4 questions
- Program Counter Register4 questions
- Direct / Off-Heap Memory2 questions
- Reachability & GC Roots3 questions
- Object Allocation & TLAB5 questions
- Generational Hypothesis & Tenuring5 questions
- Object Headers & Layout5 questions
questions
page 2 of 2The 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?
basics
~20 sThe 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.
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?
basics
~20 sBecause 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.
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?
basics
~20 sThe 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.
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?
basics
~20 sHotSpot 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.
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.
basics
~20 sWhen 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.
showing 31–35 of 35