The Java Virtual Machine manages two kinds of memory: heap
memory, both created when it starts.
- Heap memory is the runtime data area from which the JVM allocates memory for all class instances and arrays.
- Non-heap memory includes a method area shared among all threads and memory required for the internal processing or optimization for the JVM. It stores per-class structures such as a runtime constant pool, field and method data, and the code for methods and constructors.
The Memory is managed in memory pools and is managed by memory managers.
- A memory pool represents a memory area that the JVM manages (heap or non-heap). The JVM has at least one memory pool and it may create or remove memory pools during execution. A memory pool can belong to either heap or non-heap memory.
- A memory manager manages one or more memory pools. The garbage collector is a type of memory manager responsible for reclaiming memory used by unreachable objects. A JVM may have one or more memory managers. It may add or remove memory managers during execution. A memory pool can be managed by more than one memory manager.
Initially the VM assigns all objects to the Eden Space
. Garbage collection occurs in each generation when it fills up (uses up its allotted memory), the VM performs a partial garbage collection (minor collection) on that memory pool to reclaim memory used by dead objects. The VM moves (promotes) any remaining objects from the Eden Space
to one of the Survivor Spaces
(promoted to the next older generation). The VM moves objects that live long enough in the Survivor Spaces
to the Tenured Space
in the old generation. When the genured generation fills up, there is a full GC (mayor collection) that is often much slower because it involves all live objects. The permanent generation holds all the reflective data of the virtual machine itself, such as class and method objects.
A generational Collector is free to use a different collection strategy for different generations and perform garbage collection on the generations separately.