Memory Heap and Generation

What is Memory Heap?

A heap is a portion of memory which is used for dynamic allocation. The blocks of the memory in the heap are frequently allocated and freed based on the status of the object. The allocation and release of memory take place in an arbitrary order and hence the unreferenced or dead objects exist in the heap. The memory of the unreferenced objects is reclaimed by the Java program called Garbage Collector.

Java objects are created in Heap and timely switched from one part to another called generation. This switching of objects within heap memory depends on the object’s age. Actually, the memory heap is divided into 3 smaller parts or generations. The reason behind this division is to increase the performance of the object allocation process. This object allocation behaviour is also helpful to enhance the overall performance of the JVM.

Memory Heap Generation Structure
Figure 01: Memory Heap

Memory Heap Generation:

Following are the heap generations:

1. New or Young Generation:

New Generation is further divided into three parts known as Eden space, Survivor 1 space and Survivor 2 space. When an object is first created in the heap, memory gets allocated under a new generation inside Eden space and after subsequent minor garbage collection if an object survives so it gets moved to survivor 1 and then survivor 2. Minor garbage collections can be optimized assuming a high object mortality rate.

2. Old or Tenured Generation:

The Old Generation is used to store long-surviving objects. Typically, a threshold is set for the young generation object and when that age is met, the object gets moved to the old generation. Eventually, the old generation needs to be collected. This event is called a major garbage collection. Major garbage collection reclaimed the memory of the old generation.

3. Permanent Generation:

This space is reserved and used by JVM to store Metadata about classes and methods, String pool and Class level details. The permanent generation is populated by the JVM at runtime based on classes in use by the application. In addition, Java SE library classes and methods may be stored here.

This article describes the Heap from a Garbage Collection point of view. In the further article, you come to know about how does GC work?


You may be interested:



Leave a Comment