What are Garbage Collector and what is the role of Garbage Collector in C#?

Asked 06-Jul-2021
Viewed 237 times


What is the need for Garbage Collector in C#? How it works?

1 Answer


Need for Garbage Collector

The Garbage Collector are the scenario to release and allocate the memory in the machine. The Garbage Collector serves as automatic memory manager.

Suppose you create an object of any of the class with a 'new' keyword then object has to get some space in memory. These allocations are very fast. And secondly, there will come a time when you will have no use for that item. Now suppose that object is not used in the program then that object will store that amount of memory without any use. So, here the garbage collector does its job when no object in the program is in use then the amount of memory is disposed by the garbage collector. This process is completed automatically by the garbage collector. When there is not enough memory to allocate an object, the Garbage Collector must collect and dispose of the garbage memory to make the memory available for new allocation.

Garbage Collector in C#
While Developing an application in C#, you don't need to free up the memory manually. It also allocates the object on the managed heap efficiently. And when the objects are no longer in used then it will release those amount of memory for future allocation for objects or any other.