Skip to main content

What is Garbage collection?

Garbage Collection:


The .NET Framework uses automatic garbage collection to manage memory for all applications. When you use the new operator to create an object, the object's memory is obtained from the managed heap. When the garbage collector decides that sufficient garbage has accumulated that it is efficient to do so, it performs a collection to free some memory. This process is fully automatic, but there are a number of factors that you need to be aware of that can make the process more or less efficient. To understand the principles of garbage collection, you need to understand the life cycle of a managed object:

1. Memory for an object is allocated from the managed heap when you call new. The object's constructor is called after the memory is allocated.
2. The object is used for a period of time.
3. The object dies due to all its references either being explicitly set to null or else going out of scope.
4. The object's memory is freed (collected) some time later. After the memory is freed, it is generally available for other objects to use again.

Allocation
The managed heap can be thought of as a block of contiguous memory. When you create a new object, the object's memory is allocated at the next available location on the managed heap. Because the garbagecollector does not need to search for space, allocations are extremely fast if there is enough memory available. If there is not enough memory for the new object, the garbage collector attempts to reclaim space for the new object.

Collection
To reclaim space, the garbage collector collects objects that are no longer reachable. An object is no longer reachable when there are no references to it, all references are set to null, or all references to it are from other objects that can be collected as part of the current collection cycle.
When a collection occurs, the reachable objects are traced and marked as the trace proceeds. The garbage collector reclaims space by moving reachable objects into the contiguous space and reclaiming the memory used by the unreachable objects. Any object that survives the collection is promoted to the next generation.

Generations
The garbage collector uses three generations to group objects by their lifetime and volatility:
● Generation 0 (Gen 0). This consists of newly created objects. Gen 0 is collected frequently to ensure that short-lived objects are quickly cleaned up. Those objects that survive a Gen 0 collection are promoted to Generation 1.
● Generation 1 (Gen 1). This is collected less frequently than Gen 0 and contains longer-lived objects that were promoted from Gen 0.
● Generation 2 (Gen 2). This contains objects promoted from Gen 1 (which means it contains the longest-lived objects) and is collected even less frequently. The general strategy of the garbage collector is to collect and move longer-lived objects less frequently.

The CLR provides two separate garbage collectors:
● Workstation GC (Mscorwks.dll). This is designed for use by desktop applications such as Windows Forms applications.
● Server GC (Mscorsvr.dll). This is designed for use by server applications. ASP.NET loads server GC but only if the server has more than one processor. On single processor servers, it loads workstation GC.
Server GC is optimized for throughput, memory consumption, and multiprocessor scalability, while the workstation GC is tuned for desktop applications. When using the server GC, the managed heap is split into several sections, one per CPU on a multiprocessor computer. When a collection is initiated, the collectorhas one thread per CPU; all threads collect their own sections simultaneously.
The workstation version of the execution engine (Mscorwks.dll) is optimized for smaller latency. Workstation GC performs collection in parallel with the CLR threads. Server GC
suspends the CLR threads during collection. You might sometimes need the functionality of the server GC for your custom application when hosting it on a multiprocessor computer. For example, you might need it for a Windows service that uses a .NET remoting host and is deployed on a multiprocessor server. In this scenario, you need to develop a custom host that loads the CLR and the server GC version of the garbagecollector.

Popular posts from this blog

iPhone Application for Islamic Duas

Islamic Duas is an app designed for Muslims around the world who don’t get the time to go through Islamic books and are looking for authentic duas to pray. This app is a solution to all your needs as every Muslim knows that a prayer in time can save you from a lot of trouble. This app carries important Islamic duas for different occasions, daily life events and end of prayer duas to seek forgiveness from Allah etc. These Islamic duas are collected from authentic sources and are very beneficial in life of a Muslim. The app is available for free for iOS and Android users. Use the following buttons to download the ap. 

Download from here:

Muslim Some of the Islamic duas included in Muslim Dua Now are: Duas for dressing/undressing/new clothesDuas for entering and leaving restroomDuas Before and After AblutionDuas for hearing AdhanDuas for Entering and leaving mosqueDua against distraction from ShaytanDuas for protection from evilDuas before and after eatingDuas to recite when fearing harm fro…

Nested Collection Models in MVC to Add Multiple Phone Numbers - Part 3

This is Part 3 of the article series. Actually, in this article series we were developing an MVC application that will allow adding multiple phone numbers using Nested Model concept. User will be able to add or remove (zero or more phone numbers, I need minimum two phone numbers for each employee) phone numbers for any single employee. Previous logs: Nested Collection Models in MVC to Add
✔ Read More...
I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address t…

HTML5 and CSS3 Zebra Striping - Look Ma No JavaScript

It was 5 maybe 6 years ago when I first started learning jQuery. One of the first things I did was order the jQuery In Action book. If you have read that book you should remember one of the first examples given, zebra striping a table. To me this example sold me on the idea of jQuery even if I did not actually understand how jQuery worked at the time. With one line of code I could now alternate background colors in a table.
✔ Read More...
I guess you came to this post by searching similar kind of issues in any of the search engine and hope that this resolved your problem. If you find this tips useful, just drop a line below and share the link to others and who knows they might find it useful too.

Stay tuned to my blogtwitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. W…