OpenJDK Terminology

The Heap, Stack, and Metaspace

July 03, 2026

The JVM divides memory into several distinct regions, each serving a different purpose.

The Heap is where object instances live. When you call new, the object is allocated on the heap. The heap is managed by the garbage collector, which periodically reclaims memory from objects that are no longer reachable. You control the initial and maximum heap size with -Xms and -Xmx JVM flags.

The Stack is per-thread memory that holds stack frames for method calls. Each frame contains local variables, the operand stack used for expression evaluation, and metadata about the method call. Stack memory is automatically freed when a method returns. Deep recursion can exhaust the stack, causing a StackOverflowError. Stack size is configured with -Xss.

Metaspace (called PermGen in Java 7 and earlier) stores class metadata — the structure of loaded classes, method bytecode, and constant pool data. Unlike the old PermGen, Metaspace lives in native memory outside the Java heap and grows automatically by default, though you can cap it with -XX:MaxMetaspaceSize. A OutOfMemoryError: Metaspace usually signals a class-loader leak, where new class definitions are being generated continuously without the old ones being unloaded.

More reading on Foojay: Debugging RAM: Java Garbage Collection – Java Heap Deep Dive

Comments (0)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Comments (0)

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

No comments yet. Be the first.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard