The HotSpot JVM divides memory into five primary areas, some of which are thread-private and others shared across all threads:
- JVM Stack (Thread-Private): Created with the thread. Stores 'stack frames' containing local variable tables (primitive types and object references), operand stacks, and method exit information. Throws
StackOverflowError if depth is exceeded or OutOfMemoryError if dynamic expansion fails. - Native Method Stack (Thread-Private): Also known as the 'C Stack', used for Native methods implemented in C. Similar to the JVM stack, it stores frames for local variables and operand stacks. Throws
StackOverflowError and OutOfMemoryError. - PC Register (Thread-Private): Stores the address of the current instruction being executed. It is empty when executing a native method.
- Heap (Shared): Created at JVM startup. All objects and arrays are allocated here. It is the primary area for Garbage Collection (GC) and is divided into the Young Generation (Eden, From Survivor, To Survivor) and the Old Generation. Throws
OutOfMemoryError when space is exhausted. - Method Area (Shared): A logical part of the heap (often called 'Non-Heap'). Stores class information, constant pools, method data, and method code. In HotSpot, this is implemented as PermGen (JDK 7 and earlier) or Metaspace (JDK 8+).
Additionally, Direct Memory (Off-Heap) is memory outside the JVM controlled by the OS but usable by Java. It offers better I/O performance than heap memory but has higher allocation overhead and can throw OutOfMemoryError.