What are the divisions of the Memory Heap?

The memory heap is divided into three generations.

Generation 0 – Used to store short-lived objects. Frequent Garbage Collection happens in this Generation.

Generation 1 – Used for medium-lived objects.

Generation 2 – Used for long-lived objects.

In .NET, particularly in the context of the Common Language Runtime (CLR), memory management is primarily managed through the concept of garbage collection, which takes place on the managed heap. The managed heap in .NET is divided into three generations: Generation 0, Generation 1, and Generation 2.

  1. Generation 0: This is where newly allocated objects are placed. It’s a short-lived generation and collects objects that are short-lived or temporary. During garbage collection, if an object survives, it’s promoted to Generation 1.
  2. Generation 1: This generation contains objects that have survived one garbage collection cycle in Generation 0. It’s less frequently collected compared to Generation 0. Objects that survive garbage collection in Generation 1 are promoted to Generation 2.
  3. Generation 2: This generation contains long-lived objects. It’s collected less frequently than Generation 1. Objects that survive garbage collection in Generation 2 remain there until the application terminates or until explicitly collected.

The division into generations allows the garbage collector to optimize its collection strategy. Most objects are short-lived, so by collecting Generation 0 frequently, the garbage collector can free up memory quickly without having to traverse the entire heap. As objects survive longer, they are promoted to higher generations, which are collected less frequently, reducing the overhead of garbage collection on long-lived objects. This generational approach helps improve the efficiency of memory management in .NET applications.