Java’s memory structure is managed by the Java Virtual Machine (JVM) and is divided into different regions that help optimize performance and manage memory efficiently.
We can divide Java memory in three different parts.
- Heap memory
- Non-heap/Stack memory
- Others
Heap Memory
- Store objects
- There is no order of allocating the memory.
- Garbage Collector is used to delete the unreferenced objects from the heap.
- Heap memory is shared with all the threads.
- Heap also contains the String Pool.
- When heap memory goes full, it throws “java.lang.OutofMemoryError”
Non-heap/Stack Memory
All the memory except heap are called non-heap/stack memory.
- Store temporary variables and separate memory blocks for methods.
- Store primitive data types.
- Store Reference of the heap Objects.
- Strong reference,
- Weak reference(as soon as GC run, it will remove),
- Soft reference( GC removes it, if very very urgent).
- Each thread has its own stack memory.
- Variables within a SCOPE are only visible and as soon as any variable goes out of the SCOPE, it gets deleted from the Stack (in LIFO order).
- When Stack memory goes full, it throws “java.lang.StackOverflowError”.
Others Memory
Java’s heap memory is divided into two parts:
- Young Generation
- Old Generation (also called Tenured Generation).

The Garbage Collector (GC) uses these divisions to manage memory efficiently and free up space from objects that are no longer in use.
Young Generation (Eden, Survivor spaces)
When new objects are created using new keywords, these are stored in this section. When it gets full, then GC on. This GC is called Young Collection or Minor GC.
The Young Generation is further divided into three parts:
- Eden Space: Where all new objects are initially allocated.
- Survivor Spaces (S0 and S1): Two smaller spaces that hold objects that survive one or more garbage collection cycles from the Eden space.
Mark and Sweep in Young Generation:
- When a new object is created, it is allocated in the Eden space.
- The Garbage Collector (in this case, a Minor GC) runs when the Eden space is full. It uses the Mark-Sweep-Compact algorithm:
- Mark: The GC first “marks” live objects (those still referenced by active threads or other objects).
- Sweep: It then “sweeps” (removes) all unmarked objects, freeing up space in Eden.
- Compact: The live objects are compacted to reduce fragmentation.
After a Minor GC, surviving objects from Eden space are moved to one of the Survivor spaces (S0 or S1), when unreferenced objects are deleted. Don’t need to stop program execution when Minor GC runs. In each Garbage Collector cycle live objects are moved from one survivor space to another survivor space and store a count of which object completed how many cycles.
Promotion to Old Generation:
- Objects that survive several GC cycles in the Young Generation are promoted to the Old Generation.
- Each time an object survives a GC cycle, its age increases.
- Once an object’s age crosses a certain threshold (e.g., tenure threshold defined by the JVM), it is promoted to the Old Generation.
The decision to promote to the Old Generation ensures that long-lived objects are not frequently moved around in the Young Generation, which improves performance.
Old Generation
The Old Generation stores long-lived objects that have survived multiple Minor GCs. When the Old Generation fills up, a Major GC or Full GC occurs.
- Like in the Young Generation, GC marks and sweeps through the Old Generation, removing unused objects.
- A Full GC is more expensive (time-consuming) than a Minor GC because it involves both the Young and Old Generations.
- During the process, the garbage collector pauses the program’s execution to perform the search for garbage objects, which can result in a temporary slowdown in the program’s performance.
GC in Action: From Eden to Old Generation
- Eden Allocation: New objects are created and placed in Eden.
- Minor GC: When Eden fills up, a Minor GC is triggered:
- Mark: The GC marks live objects in Eden.
- Sweep: The GC sweeps away dead (unreferenced) objects.
- Survivor Spaces: Live objects are moved to one of the two Survivor spaces (S0 or S1).
- Survivor Promotion: After surviving several GC cycles in Survivor spaces, objects are promoted to the Old Generation.
- Major/Full GC: When the Old Generation is full, a Major or Full GC is triggered to clean up the Old Generation.
MetaSpace
Before Java 8, the JVM used a memory region called PermGen (Permanent Generation) to store class metadata, method data, bytecode, stack and variable size, and class hierarchy information. However, PermGen had a fixed size limit, and once it was full, it would cause an OutOfMemoryError, leading to JVM crashes.
In Java 8, PermGen was replaced with Metaspace, which serves the same purpose but with key improvements. Metaspace is part of native memory rather than the JVM heap, allowing it to grow dynamically based on the application’s needs. This eliminates the fixed-size limitation of PermGen, improving memory management and reducing the risk of crashes.
Summary
- Young Generation: Eden (new objects), Survivor spaces (objects that survived a GC).
- Old Generation: Long-lived objects.
- GC Process: Mark, sweep, compact in the Young Generation. Promotion to the Old Generation happens based on the object’s survival and tenuring threshold.
- Metaspace: Introduced in Java 8 and replaced PermGen.
This structure and process allow for efficient memory management, reducing fragmentation, and handling both short-lived and long-lived objects appropriately.
Resource
- https://www.javatpoint.com/java-garbage-collection-interview-questions
- https://www.ibm.com/topics/garbage-collection-java
- https://medium.com/javarevisited/jvm-architecture-32def70b6de#:~:text=The%20runtime%20data%20area%20is,and%20local%20variables%20are%20stored).
- https://www.java67.com/2020/02/50-garbage-collection-interview-questions-answers-java.html#goog_rewarded