Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens during Garbage Collection if Generation 2 is filled?

I'm re-reading CLR via C# right now and have some questions about garbage collection in .NET. In the book, after Generation 0 is filled, garbage collection starts and moves all outstanding object references to the Generation 1.

The same happens when the Generation 1 is filled. But what happens when second generation is filled? There are no other generations to move references. Is the size of Generation 2 expanded by the CLR?

like image 784
spkenny Avatar asked May 27 '09 19:05

spkenny


People also ask

How does generational garbage collection work?

The Generational Garbage Collection ProcessBoth survivor spaces start out empty. When the eden space fills up, a minor garbage collection is triggered. Referenced objects are moved to the first survivor space. Unreferenced objects are deleted when the eden space is cleared.

How many generations will be there in garbage collection?

To optimize the performance of the garbage collector, the managed heap is divided into three generations, 0, 1, and 2, so it can handle long-lived and short-lived objects separately. The garbage collector stores new objects in generation 0.

Which generation is not present in garbage collection?

Creation of object 11 causes the GC to start again which may move some more objects to generation 1. Generation 1 is ignored for Garbage Collection until it reaches its budget size for Garbage collection, which improves the performance of GC.

What does the garbage collector in net 2?

NET's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap.


2 Answers

Yes, it will expand the size of Generation 2 if it can. If there is no more space available you will get an OutOfMemoryException.

like image 121
Andrew Hare Avatar answered Oct 04 '22 04:10

Andrew Hare


The different generations of garbage collection are not about having a particular size but about how old the garbage is. None of the generations have size limitations that I know of.

like image 22
Brian Avatar answered Oct 04 '22 02:10

Brian