Medium java java-21

What are the JVM memory areas?

The 30 second answer

Each thread has its own program counter, its own JVM stack with a frame per method call holding local variables, and a native method stack. All threads share the heap, where every object and array lives and which the garbage collector manages, and the method area, which holds class data and code. In HotSpot since Java 8, class metadata lives in native memory called Metaspace, not in the heap.

From JVMS 2.5:

AreaScopeHoldsError
pc registerper threadcurrent instructionnone
JVM stackper threadframes: locals, operand stackStackOverflowError, or OutOfMemoryError if it can't grow
Native method stackper threadnative callssame as JVM stack
Heapsharedobjects and arraysOutOfMemoryError: Java heap space
Method areasharedclass structures, run-time constant pool, method codeOutOfMemoryError (HotSpot: Metaspace)

HotSpot specifics: Java 8 removed PermGen and moved class metadata to Metaspace in native memory, limited by -XX:MaxMetaspaceSize (unlimited by default). Interned strings and static field values are on the heap. The process also uses native memory for thread stacks, the code cache, direct ByteBuffers and GC data structures; -XX:NativeMemoryTracking=summary with jcmd <pid> VM.native_memory summary shows the breakdown.

Sources: JVMS 2.5 Run-Time Data Areas, GC Tuning Guide: Other Considerations (Metaspace), Troubleshooting Guide: Diagnostic Tools (Native Memory Tracking)

Sources (0)
See inline citations in the answer above.