The garbage collector (GC) is used in the. NET for memory management, especially it can recover the memory that is needed by the running application.
. NET runtime is a garbage collector, which is a program that is designed to clean up memory. The method is that all dynamically requested memory is allocated to the heap (all languages are handled this way, but in the. NET, the CLR maintains its own managed heap for use by. NET applications). At intervals, when. NET detects that the managed heap for a given process is full and needs to be cleaned, the garbage collector is called. The garbage collector processes all variables in the current code, examines references to objects stored on the managed heap, and determines which objects can be accessed from code-that is, which objects have references. Objects that are not referenced are no longer considered to be accessible from the code and are therefore deleted.
An important aspect of garbage collection is its uncertainty. In other words, there is no guarantee when the garbage collector will be called: the CLR can call it when it decides it needs it. However, you can override this procedure to invoke the garbage collector in your code. This is helpful when testing, but should not be done in a normal program.
. NET garbage Collection