Fundamentals 14 min read

Understanding JVM Memory Layout: Heap, Metaspace, and Stack Explained

This article explains the JVM memory layout, covering the heap area, its configurable size and generational structure, the Metaspace replacement for PermGen, the thread‑private virtual machine stack and native method stack, as well as the program counter register and related garbage‑collection behavior.

Programmer DD
Programmer DD
Programmer DD
Understanding JVM Memory Layout: Heap, Metaspace, and Stack Explained

Memory Layout

The JVM memory layout defines how Java allocates, manages, and releases memory at runtime, ensuring stable and efficient operation; different JVM implementations may vary slightly in their partitioning strategies.

Heap Area

The Heap is where Java objects are allocated and where Out Of Memory (OOM) errors occur; it is shared among all threads and typically occupies the largest portion of JVM memory.

The heap size can be customized and adjusted at runtime using the -Xms (initial size) and -Xmx (maximum size) parameters; setting them equal can reduce pressure from post‑GC resizing.

The heap is divided into two major regions: the Young generation (also called the new generation) and the Old generation (old generation). Newly created objects start in the young generation and are promoted to the old generation after surviving a configurable number of garbage‑collection cycles.

The young generation consists of an Eden space and two Survivor spaces ( S0 and S1). When Eden fills, a Young Garbage Collection ( YGC) occurs, reclaiming unreachable objects and copying survivors to the opposite Survivor space. If the Survivor space cannot accommodate the objects, they are promoted directly to the old generation.

Object aging is controlled by the -XX:MaxTenuringThreshold parameter (default 15); after an object has been copied between Survivor spaces the specified number of times, it is moved to the old generation. If the old generation cannot hold the object, a Full Garbage Collection ( FGC) is triggered, which may lead to an OOM if memory is still insufficient.

Metaspace

Starting with JDK 8, the former PermGen area was removed and replaced by Metaspace, which is allocated in native memory. Class metadata, static fields, methods, and constants are now stored in Metaspace rather than the heap, allowing dynamic sizing and eliminating the fixed‑size limitation of PermGen.

JVM Stacks (Virtual Machine Stack)

The stack is a LIFO data structure used by the JVM to manage method calls. Each thread has its own private stack composed of stack frames, which contain a local variable table, an operand stack, a dynamic link, and a return address.

The current stack frame holds the method’s local variables and operand stack; execution proceeds by pushing and popping values on this operand stack.

Key components of a stack frame include:

Local variable table – stores method parameters and local variables.

Operand stack – a bucket‑style stack used by bytecode instructions.

Dynamic link – a reference to the constant‑pool entry of the current method for dynamic method resolution.

Return address – the location to jump back to after method completion.

Method exit can occur normally via return bytecodes ( RETURN, IRETURN, ARETURN) or abruptly via exceptions, each ultimately popping the current frame.

Native Method Stacks

The native method stack is also thread‑private and serves native (JNI) methods. It operates outside the JVM’s control, and excessive native usage can reduce JVM stability; native‑heap OOM may be thrown when native memory is exhausted.

Program Counter Register

Each thread has its own program counter (PC) register, which holds the address of the next bytecode instruction to execute. The PC is thread‑local, never shared, and does not cause memory‑overflow errors.

Summary

From a thread perspective, the heap and Metaspace are shared across all threads, while the virtual machine stack, native method stack, and program counter register are private to each thread.

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.

JavaJVMStackHeapMetaspace
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
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.