Skip to content
JVM Runtime Data Areas Deep Dive: Memory Layout, Object Structure & OOM Troubleshooting

JVM Runtime Data Areas Deep Dive: Memory Layout, Object Structure & OOM Troubleshooting

This article dissects JVM runtime data areas — heap, metaspace, thread stacks, direct memory — with object memory layout details, five OOM types mapped to regions, three production incident case studies, and practical measurement tools (JOL, NMT) for capacity planning.

Architecture Pony
Architecture Pony
Architecture Pony

The article opens by framing JVM memory layout as the trunk of the JVM knowledge tree: every later GC discussion and troubleshooting tool reads from these same regions. It structures the analysis around three threads: a full panorama of data areas, the real in-memory shape of objects, and a five-entry OOM cheat sheet.

Three Production Incidents

Incident 1 — Large query blows up old generation. An operational export of three months of order details assembled millions of rows into a single List. The massive object could not fit in Eden, so it went straight to the old generation (bypassing the generational protection mechanism). Minor GCs became frequent full GCs, and P99 latency jumped from 15 ms to 3 seconds. The fix was to paginate the export; the root cure was to bake a result-set limit into the API contract.

Incident 2 — Thread-pool increase triggers container OOMKilled. An I/O-heavy service raised its thread pool from 200 to 800. Each thread reserves a 1 MB stack ( -Xss1m), adding ~800 MB of native memory outside the heap ( -Xmx). The heap stayed within limits, but the process RSS exceeded the container limit and the kernel killed the pod. The lesson: thread-stack memory is a private ledger priced by thread count × Xss; it must be tracked separately from heap parameters.

Incident 3 — Metaspace slow leak over eight days. A legacy system using CGLIB dynamic proxies created a new class loader per proxy. Metaspace grew ~60 MB/day until MaxMetaspaceSize was hit, throwing OutOfMemoryError: Metaspace. Root cause: class unloading requires the loader itself to become unreachable; leaking one loader leaks all its classes. The fix was to cache and reuse proxy classes or share a single long-lived loader.

1. Technical Analysis: Turning the Ledger Pages

1.1 Data-Area Panorama: Shared vs. Private Boundaries

By thread dimension (Figure 1): Thread-shared — heap, metaspace, code cache, string constant pool — live for the whole JVM process; GC and class loading operate here. Thread-private — VM stack, program counter, native method stack — are created/destroyed with the thread; no GC participates , reclamation is deterministic frame pop. Design motive: shared follows objects (cross-thread passing), private follows execution (call-site context). Outside the five spec areas sits direct memory — not in the JLS five areas, yet often the least predictable chunk in modern services.

Another lens: who prices each region . Shared areas are capped by JVM flags ( -Xmx, -XX:MaxMetaspaceSize); private areas are priced by thread count (stack total = count × Xss, flag only sets unit price); direct memory is priced by traffic (flag can cap but cannot throttle growth rate). Three ledgers, three pricing models — Incident 2 happened because the thread-count ledger was mistakenly booked in the parameter-cap column.

1.2 Heap Generations & Object Flow

Classic layout by age (Figure 2): young gen (Eden 80% + two Survivors 10% each) for short-lived objects; old gen for long-lived. Three promotion rules:

Survive one Minor GC → age +1; age 15 (hard limit from 4-bit field in mark word) → promote.

Dynamic age judgment: if same-age objects exceed half of Survivor, promote the whole batch early (soft threshold).

Large objects go straight to old gen ( -XX:PretenureSizeThreshold or G1 humongous regions) to avoid Survivor copying. Incident 1 abused rule 3: a million-row result set = many large objects, all bypassing Survivor straight into old gen.

The weak generational hypothesis (most objects die young) is the bedrock of GC design (expanded in part 03). Understanding flow explains why report-export workloads (few but huge) are most dangerous to the heap.

G1 footnote: no physical contiguous Eden/Survivor/Old; heap split into 2000+ equal regions, each dynamically playing a generational role — but generational logic persists as logical tags. G1 adds Humongous regions for objects > half a region; multiple contiguous regions stitched together. Incident 1 on G1 shows not frequent full GCs but humongous fragmentation and slower reclamation — physical layout changed, generational accounting still applies.

1.3 Object's Real Anatomy: Header, Body, Padding

new

object in heap = three parts (Figure 3):

Header — Mark Word (8 bytes: hash, GC age, lock state, biased thread ID; four states reuse same slot) + Klass Pointer (4 bytes compressed, points to class metadata in metaspace).

Instance data — fields packed by width: long/double 8, int/float 4, short/char 2, byte/boolean 1, reference 4 (compressed oops); superclass fields first.

Alignment padding — round up to 8-byte multiple; hence empty object = 16 bytes fixed.

This answers two production questions: why default tenuring threshold is 15 (only 4 bits for age in mark word); why 100k objects with few fields consume several times more memory than wide objects (12-byte header fixed overhead, half the space is header for tiny objects). Measure precisely with JOL, don't guess.

1.4 Stack & Stack Frame: Method Call Scene

Each thread owns a VM stack; each call pushes a frame (Figure 4) containing:

Local variable table (params + locals, 32-bit slots)

Operand stack (bytecode execution workspace)

Dynamic linking (symbolic → direct reference)

Return address

Two takeaways: (1) Stack depth exceed → StackOverflowError (infinite recursion典型), tunable via -Xss. (2) Locals die on the frame : method return pops whole frame, variables vanish. This is the theoretical basis for escape analysis (part 06): if an object never escapes the frame, it need not be heap-allocated — scalar replacement does exactly that. Capacity view: stack memory = thread count × Xss; Incident 2's 800 MB was this ledger left unbooked.

Two oft-tested details: String constant pool — moved from PermGen to heap in JDK 7; String.intern() semantics changed (pre-7 copied into PermGen, post-7 only puts reference into heap's String Table) — outdated interview answers abound. Compressed oops — 64-bit JVM uses 4-byte references for heap objects, enabled by default up to ~32 GB heap; beyond that compression fails and objects grow — hence the "don't exceed 31 GB" rule. Both remind: memory layout must be discussed with JDK version; cross-version copy-paste is as dangerous as cross-collector parameter copy-paste.

1.5 Metaspace: Class Metadata's Independent Ledger

JDK 8 removed PermGen; class metadata moved to native Metaspace. Contents: class structure (fields, methods, bytecode), runtime constant pool, method bytecode. Two flags: MetaspaceSize = waterline triggering first metaspace GC (common misread as initial size); MaxMetaspaceSize = hard cap (unset = physical memory, dangerous in containers).

Class metadata reclamation is stricter than objects: object GC checks reachability; class unloading needs three conditions — all instances of the class collected, its defining ClassLoader collected, and the java.lang.Class object unreferenced. Incident 3's CGLIB leak kept loaders reachable, so classes never unloaded. Hot-deploy frameworks, frequent dynamic proxies, Groovy scripting engines are top risk scenarios.

1.6 Direct Memory: The Fourth Off-Spec Ledger

NIO DirectByteBuffer avoids heap↔kernel copy by allocating in native memory. Cost: not on regular GC path; reclaimed via Cleaner only when the Java DirectByteBuffer object is GCed. When heap pressure is low, direct memory can pile up. Netty's PooledByteBuf, Kafka client, JDBC drivers all use it. Flag -XX:MaxDirectMemorySize caps (default ≈ -Xmx); exhaustion throws OutOfMemoryError: Direct buffer memory. Observe via NMT (Native Memory Tracking), demonstrated in previous article's jcmd checkup. Of the four container ledgers, this one spins out of control most easily : heap has Xmx, stack has thread count, metaspace has cap, direct memory's growth curve follows only business traffic.

1.7 Five OOM Cheat Sheet: Exception Keyword Is the Map

All regions + OOM forms combined into one lookup table (Figure 5): java heap space / GC overhead limit exceeded → heap Metaspace → class metadata unable to create new native thread → thread stack (× Xss or OS limit) direct buffer memory → direct memory requested array size exceeds VM limit → single huge array request

Usage: exception keyword → locate region → find matching flag → analyze growth curve; qualify issue in ≤10 minutes. Special callout: container OOMKilled has no exception stack, no dump — kernel kills process (exit 137). Defense isn't JVM flags but NMT observation + pod memory alert; this discipline was set in the previous article.

2. Production Practice: One Parameter Set & One Measurement Method

2.1 Config 1: Full Memory Flags Mapped to Ledgers

All five ledgers' flags collected, each tagged with its ledger:

# ==== Memory Flags Full Set (each owns one ledger) ====
# --- Ledger 1: Heap ---
-Xms4g -Xmx4g          # start=max, avoid resize jitter (part 01)
-Xmn1g                 # young gen 1/4 (not recommended manual in G1)
-XX:SurvivorRatio=8    # Eden:S0:S1 = 8:1:1 (Fig 2 ratio)
-XX:MaxTenuringThreshold=15  # promotion age soft threshold (hard cap 15)
-XX:PretenureSizeThreshold=1m  # >1M large object straight to Old (Serial/ParNew)
# --- Ledger 2: Metaspace ---
-XX:MetaspaceSize=256m       # first metaspace GC waterline
-XX:MaxMetaspaceSize=512m    # hard cap: mandatory for dynamic-class services
# --- Ledger 3: Thread Stack ---
-Xss1m                 # 1000 threads = 1G, book separately
# --- Ledger 4: Direct Memory ---
-XX:MaxDirectMemorySize=1g   # default≈Xmx, tune by actual load test for Netty

2.2 Config 2: Object Size Measurement & Memory Checkup Scripts

"How big is this object? Where does this service's memory go?" — don't guess. Two toolchains: JOL for single-object precision, NMT for process-ledger precision.

JOL: Print Object Memory Layout

# ==== JOL: Print Object Memory Layout ====
// pom: org.openjdk.jol:jol-core:0.17
import org.openjdk.jol.info.GraphLayout;
Order o = new Order();         // business object
// Single-object layout: header/fields/padding byte-by-byte
System.out.println(
ClassLayout.parseInstance(o)
.toPrintable());
// Retained graph: total size of entire reference tree
System.out.println(GraphLayout
.parseInstance(o).totalSize());

# Output sketch (64-bit JVM, compressed oops):
# OFFSET  SIZE  TYPE  VALUE
#      0     4  (header: Mark Word)
#      4     4  (header: Klass Ptr)
#      8     4   int  amount
#     12     1   byte status
#     13     3       (alignment padding)
# => one 1-int + 1-byte object = 16B

NMT: Process Four-Ledger Checkup

# ==== NMT: Process Four-Ledger Checkup ====
# 1) Start with -XX:NativeMemoryTracking=summary
# 2) Delta method for growth (pre-sale baseline vs peak)
jcmd 1 VM.native_memory baseline
# After 1 hour peak traffic:
jcmd 1 VM.native_memory summary.diff
# Output sketch (watch delta column):
#   Java Heap:  2936012  delta=0
#   Class:       104857  delta=+1024  <- classes creeping up
#   Thread:       819200  delta=0
#   Internal:      20480  delta=0
#   Other:       1048576  delta=+524288
#                <- direct memory grew half a GB!
# 3) Alert gauge: Total committed / Pod limit

Delta method's key: baseline comparison, not absolute values . Large balance isn't scary; scary is the ledger that shouldn't grow but does — Metaspace creeping daily = Incident 3 signal; Other (direct memory) rising with traffic and never falling = leak precursor. Bake diff into daily patrol script (part 09 will merge into monitoring).

One habit worth forming: every OOM postmortem adds a "which ledger was it" entry . Heap, metaspace, stack, direct memory, container — five boxes; force each incident into one. Three months later you own a memory portrait of your service: which ledger is most active, which OOM visits most often. Ledger thinking's value isn't memorizing theory, but turning every incident into a stroke on the portrait.

2.3 Postmortem: Metaspace Leak Location in Three Steps

Incident 3's full location process is reproducible.

Qualify : NMT diff shows Class region only rising; jcmd GC.class_stats shows class count climbing steadily — metaspace leak confirmed (heap analysis misses it because metaspace isn't in heap, jmap -histo won't see it).

Find source : jcmd GC.class_loader_stats (or MAT class-loader view) shows loaders with $$EnhancerByCGLIB classes under distinct DummyClassLoader s, count growing with business calls.

Cure : cache/reuse dynamic proxies (CGLIB supports it) or load all proxy classes under one persistent loader. Three steps later Metaspace curve flattens.

Class leak signature is "leak at class-loader granularity" — this insight outlives any specific tool.

2.4 Two High-Frequency Misconceptions

Misconception 1: SurvivorRatio=8 means 8:1:1 takes exact effect. In G1 this flag is largely ineffective — G1 has no fixed physical Eden/Survivor partitions; regions are dynamically assigned. Young gen size controlled by -Xmn or G1NewSizePercent. Flags must match collector; don't copy parameters across generations.

Misconception 2: Set -Xmn huge to reduce Minor GCs. Larger young gen → longer Minor GC (more live objects to copy) and squeezes old gen → faster promotion → heavier Full GCs. Generational tuning is balance, not one-sided boost — part 03 will detail this balance.

2.5 Five-Ledger Capacity Table Template

Condense this article's method into a capacity table: one row per ledger, four columns — Ledger name, Flag cap, Actual growth curve (load test / NMT observation), Risk tag . Example: Heap ( Xmx 4G / peak 3.6G stable / low risk), Metaspace (cap 512M / peak 180M stable / watch dynamic classes), Thread stack (800 × 1M / linear with pool config / re-estimate on pool change), Direct memory (cap 1G / fluctuates with traffic / watch diff at peak), Container (limit 6G / Total committed 5.2G / 13% headroom tight). Refill this table on every scale-out, pool change, version upgrade — it's the memory equivalent of "pre-deploy capacity planning", same methodology as the Kafka article.

3. Core Source Code: Four Segments to Grasp Memory Ops

3.1 TLAB Lock-Free Allocation (HotSpot Simplified)

// HotSpot object allocation main path (C++ simplified)
oop CollectedHeap::allocate(size_t size) {
// 1. Try thread-local TLAB first
if (tlab.available() >= size) {
  oop obj = tlab.allocate(size);
  // Bump-the-pointer: just top += size, no lock
  return obj;
}
// 2. TLAB full: request new TLAB
// 3. Still no: global Eden allocation (CAS)
return allocate_in_eden(size);
}
// Large objects bypass all above, straight to old gen
if (size >= pretenure_threshold)
  return allocate_in_old(size);

Why object allocation is "one of Java's fastest ops": TLAB reduces most allocations to a single pointer addition, no CAS even. Generational design's essence is allocation speed — Eden contiguous + bump-pointer; old-gen collectors (e.g., CMS) with fragmented allocation are far costlier.

3.2 Mark Word's Reused Slots (Java-Side View)

// markOop is HotSpot's Mark Word definition
// 64-bit five-state reuse (simplified):
// | unused:25 | hash:31 | age:4 | biased:1
//   | lock:2 |  <- unlocked
// | thread:54 | epoch:2 | age:4 | 1 | 01
//    <- biased
// | ptr:62 | 00  <- lightweight (stack lock record)
// | ptr:62 | 10  <- heavyweight (monitor)
// | obj_hash:31 | 11  <- GC mark
// age field 4 bit => MaxTenuringThreshold
//    hard source of upper bound 15

Five semantics in one slot, distinguished by last 2 lock bits — classic extreme memory compression. Generational age gets only 4 bits not by theory but by space budget: every bit in the 8-byte mark word has an owner. "Why is the parameter this value" traced to memory layout, answer often surfaces automatically.

3.3 DirectByteBuffer Allocation & Reclamation

// java.nio.DirectByteBuffer constructor (simplified)
DirectByteBuffer(int cap) {
  // 1. Ask unsafe for native memory
  base = unsafe.allocateMemory(size);
  // 2. Accounting: Bits.totalCapacity +=
  //      capacity (MaxDirectMemorySize
  //      exceeded → OOM thrown here)
  // 3. Register Cleaner: native memory freed
  //      ONLY when Java object is GCed
  cleaner = Cleaner.create(this,
    new Deallocator(base, size, cap));
}
// Risk: low heap pressure -> Java object not GCed
//      -> Cleaner not triggered -> direct memory piles up
// Fix: explicit System.gc() hint or
//      Netty's own reclaim pool (GC-independent)

This source explains 1.6's "pile-up" mechanism: direct memory's life hangs on a Java object's GC — indirect, laggy, uncontrollable. That's exactly why Netty builds its own pool — don't hand lifecycle to GC. Also explains why some services' RSS doesn't drop after traffic falls: direct memory still queued in Cleaner.

3.4 Class Unloading's Three Conditions (JVM Spec Wording)

// HotSpot decides if a class can unload (simplified):
bool can_unload(Klass* k) {
  ClassLoaderData* cld = k->class_loader_data();
  // Condition 1: class has no live instances
  if (k->has_instances()) return false;
  // Condition 2: its ClassLoader is unreachable
  //   (entire cld pending destruction)
  if (cld->is_alive()) return false;
  // Condition 3: java.lang.Class object has no refs
  if (k->is_referenced()) return false;
  return true;  // all three must hold
}
// CGLIB new loader each time => is_alive always true
// => never unload => Metaspace monotonic rise

Most overlooked is condition 2: class unloading's minimum unit is the loader, not the class . Want to unload a single class? Impossible. This dictates dynamic class generation best practice — reuse loaders, bind "class lifetime" to "loader lifetime". Incident 3's cure applies this principle.

4. Seven Ledger Mnemonics

Object Header Ledger: Every object fixed 12-byte header (compressed oops); pure 1-byte field object actually 16 bytes — header 75%. Massive tiny-object scenarios (e.g., cache KV) consider array flattening or field trimming.

Generational Ledger: Large object straight to old = "bypass protection"; one million-row export = manually manufacturing promotion storm. Result-set cap in API spec beats heap parameter tuning by an order of magnitude.

Thread Ledger: Stack memory = thread count × Xss, outside Xmx. Before raising pool from 200 to 800, book the 600 MB stack ledger in capacity table — memory tuning is global ledger, not heap local ledger.

Metaspace Ledger: Class unloading unit = loader. Dynamic proxies must cache/reuse; hot-deploy environments add "loader leak" to routine patrol metrics.

Direct Memory Ledger: Only ledger GC doesn't watch; growth curve follows traffic. MaxDirectMemorySize explicit cap + NMT delta patrol — two gates, neither optional.

Parameter Ledger: SurvivorRatio ineffective in G1, oversized Xmn breeds Full GC — flags pair with collector, cross-generation parameter copy is tuning taboo.

Location Ledger: OOM exception keyword maps directly to region, 10-minute qualification; but container OOMKilled has no keyword — rely on NMT diff + alert, two accident classes, two radar sets.

5. High-Frequency Interview Questions (with Answers)

20 Q&A pairs covering: shared vs private areas & why private needs no GC; why heap generational & three-zone ratios; three paths to old gen (large object, dynamic age, age 15); why tenuring threshold 15 hard limit (4 bits in mark word); object layout & empty object 16 bytes; mark word contents & reuse design; stack frame contents & StackOverflowError conditions; local variable table as GC roots; PermGen vs Metaspace & removal rationale; class unloading three conditions & loader granularity; MetaspaceSize common misread; direct memory definition & why off-heap; direct memory pile-up & Netty pool rationale; -Xss tuning risks; five OOM keywords → regions; container OOMKilled vs JVM OOM & defense; TLAB & near-free allocation; Survivor purpose & two-zone copy; GC roots & stack frame relation; production quantification (JOL, GraphLayout, NMT + jcmd GC.class_stats).

6. One-Page Cheat Sheet

Six memory-layout sentences: ① Shared (heap/metaspace) follows objects, private (stack/PC) follows threads; ② Object three-part: 12B header + instance data + 8-byte align, mark word one-slot-five-uses; ③ Age 4 bits → cap 15 — parameter's "why" hides in layout; ④ Three promotion paths: age out, dynamic judgment, large object direct; ⑤ Class unload by loader, dynamic proxy must cache; ⑥ Direct memory off GC radar, cap + NMT patrol dual gates.

7. Summary & Next Episode Preview

This article finishes the memory ledger: shared heap & metaspace, private stack, off-spec direct memory, plus real object layout in heap — from here, "memory" is no longer a black box but five ledgers you can book, reconcile, and audit . Three incidents map to three ledger blind spots: generational flow (large object direct), private stack (thread-count product), metaspace (loader leak). Next is the series centerpiece: GC. Part 03 "GC Basics & Generational Collection" answers "how garbage is identified, how collection executes, where STW comes from" — laying three-color marking, copying, mark-compact foundations for G1 and ZGC.

Hands-on homework: run jcmd 1 VM.native_memory summary on your service, plug four ledger numbers into capacity table per this article; then JOL-print your core business object layouts, calculate header overhead ratio — starting next episode, these numbers become the bedrock of GC tuning.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JVMMemory ManagementMetaspaceruntime data areasOOM troubleshootingJOLobject layoutdirect memoryNMTthread stack
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.