Article

What is garbage collection

.NET automatically reclaims the memory of objects that are no longer used. The inaccessible objects are identified, their memory is reclaimed, and the managed heap is compacted.

C# programs run in a managed environment. This environment manages memory resources for you. It allocates memory on the managed heap using the new operator. .NET automatically reclaims the memory of objects that are no longer used. But how does the garbage collector know that an object's no longer required? The garbage collector can reclaim the memory of the object when reference(s) to it no longer exist i.e., when the object goes out of scope or when its reference(s) is set to null.

Benefits of garbage collection

You do not need to write the memory de-allocation code and manually track and release memory allocations occupied by a .NET object, and its references to other objects, if any. The garbage collector manages the lifetime of .NET objects and their references, and automatically reclaim their instance memory at some point. This is the main advantage of automatic garbage collection. But if you are a C++ programmer, you are responsible for the release of previously allocated memory. The delete operator is there for this purpose.

Writing the proper code to take care of all the de-allocations is a very time consuming process. For instance, if some object refers to other objects, the application that doesn't have an automatic GC support will itself need to destruct the referred objects as well. However, in .NET, it is the responsibility of the garbage collector to reclaim the memory of the objects being referred to. This enhances your productivity. The code is much smaller, more reliable, and cleaner than before, easy to read and write.

Memory related bugs in languages like C, C++

Automatic garbage collection prevents bugs and security flaws that often occur in apps. like C, C++. for example, dangling pointers, memory leak, access violation, memory allocation failure.

A dangling pointer is one that still points to a memory location of the deallocated memory. In a language like C, if you delete (or deallocate) an object from memory explicitly, it does not modify the value of the associated pointer and the pointer still refers to the same memory location. Now consider that the system reallocates this freed memory to another process and the original program writes data to memory pointed by a dangling pointer. This corrupts the memory and it now contains different data leading to unpredictable behavior, bugs that can be difficult to trace, or cause general protection faults (GPFs) in Windows. The system may become unstable.

C++ projects usually encounter the problem of memory leak. This problem arises when you forget to release the previously-allocated memory. In this scenario, the objects do not get destroyed at all, leading to memory leaks. If some piece of code is referring to an object that is already destroyed, it leads to the access violation error.

In absence of an automatic garbage collector, it is possible that memory allocation might fail due to insufficient free memory. The programmers are aware about object initialization. But they often forget the significance of release of previously allocated memory.

Garbage collection is all about the memory

Garbage collection has nothing to do with the unmanaged resources like file handles, database connections. It is your responsibility to manage them.

An object on a managed heap may acquire the resources like file handles, window handles, database connections, tables. The garbage collection is all about the memory, it has nothing to do with these resources. The runtime doesn't manage these resources, therefore they are also referred to as the unmanaged resources. It is your responsibility to manage these resources.

When garbage collection occurs

It is not simply called when some object goes out of scope. It occurs behind the scenes without the programmer's involvement. It occurs only when necessary for example, when 'new' fails because of insufficient free memory. Or, it occurs intermittently i.e. you cannot precisely tell when garbage collection will take place. You are not sure when your object will finally be destroyed. There is a drawback associated with this, you need to make sure that the unmanaged resources held by an object must be released before it becomes inaccessible.