Fundamentals of C# Garbage Collection 

Introduction

Garbage collection is a process of managing memory in .Net Framework. This process is done by Garbage collection. Here in this blog, we will discuss, what is Garbage Collection.

Getting Started

In common Language Runtime (CLR) of .Net Framework the Garbage Collector shortly known as GC performs Garbage Collection to release memory from an application or object that is no longer in use.

The GC periodically checks for objects in the managed heap that are no longer being used by the application and performs the necessary operations to reclaim their memory. AS a developer, you don't have to write code to perform memory management tasks. The GC automatically performs Garbage Collection.

For example, in your dot net application each time you create a new object, the common language runtime allocates memory for the object from the managed heap. When the object is no longer use by your application the GC frees the person of memory used by the object.

How Garbage Collector Works

The garbage collector's optimizing engine determines the best time to perform a collection based on the allocations being made. When the garbage collector performs a collection, it releases the memory for objects that are no longer being used by the application. It determines which objects are no longer being used by examining the application's roots.

How ever there are certain conditions that forces the garbage collector to perform collection. These conditions are given below.

  1. The system has low physical memory. This is detected by either the low memory notification from the OS or low memory as indicated by the host.

  2. The memory that's used by allocated objects on the managed heap surpasses an acceptable threshold. This threshold is continuously adjusted as the process runs.

  3. The GC.Collect method is called. In almost all cases, you don't have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.

Phases of Garbase Collection

  1. Marking Phase: In this phase GC find the makes list of lived object.

  2. Relocating Phase: Here GC, updates the references to the objects that will be compacted.

  3. Compacting Phase: This phase reclaims the space occupied by the dead objects and compacts the surviving objects. The compacting phase moves objects that have survived a garbage collection toward the older end of the segment.

How the GC determines the Objects

  1. Stack Roots: Stack variables provided by the just-in-time (JIT) compiler and stack walker. JIT optimizations can lengthen or shorten regions of code within which stack variables are reported to the garbage collector.

  2. Garbage collection Handles: Handles that point to managed objects and that can be allocated by user code or by the common language runtime.

  3. Static Data: Static objects in application domains that could be referencing other objects. Each application domain keeps track of its static objects.

Summary

Automatic memory management can eliminate common problems, such as forgetting to free an object and causing a memory leak or attempting to access memory for an object that's already been freed. I hope you this is helpful to you.

Thanks

Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

1 Comments

  1. I really enjoyed reading your article, and it is good to know the latest updates. Do post more. Offshore Development Center in India. Keep on doing it. I am eagerly waiting for your updates. Thank you!

    ReplyDelete
Previous Post Next Post

نموذج الاتصال