Understanding Garbage Collection in VB.NET

Garbage collection is the mechanism to releasing memory from unused objects and components of the application. Languages such as C++ do not have any garbage collection system; therefore developers have to manually clean the memory. Java and Visual Basic provides the automatic garbage collection system. But in Java the garbage collection process starts automatically with intimating the user.

In .NET Framework, garbage collector is implemented as a separate thread. This thread will always be running at the back end. Since garbage collector always run at the back end there will be extra overhead for the memory. Therefore garbage collector is given the lowest priority in .NET Framework. In .NET Framework, garbage collection system is based on the Mark and Compact algorithm that involves removing of objects that have gone out of scope and compacts all the remaining objects at the beginning of the address space. The space allotted for the application is also referred to as managed heap. When a new object needs to be instantiated and if there is not enough room in the application’s managed heap, the garbage collection process starts.

Moreover, to optimize the usage of garbage collector in .NET Framework, the collector implements the concept of generations. This concept allows the system to find any freed-up space by looping through younger objects. According to this concept the older an object is the less likely it is to be out of scope during garbage collection. Therefore, it is most likely the developer’s checks the newest objects first and then check the older objects if space is still needed.

There are three generations that the garbage collector in .NET Framework supports. They are Generation 0, Generation 1, and Generation 2. The Generation 0 will be smaller in size, Generation 1 will be medium, and Generation 2 will be larger in size. The objects in Generation 0 are objects that have been created since the last garbage collection, objects in Generation 1 are objects that still remain after one garbage collection, and objects in Generation 2 are objects that are still in scope after multiple garbage collections. Thus garbage collection in .NET Framework uses the Mark and Compact algorithm and generation technology to efficiently work with applications.


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.