Fully featured low overhead profiler for Java EE and Java SE platforms.
Easy-to-use performance and memory .NET profiler for Windows, Linux and macOS.
Secure and easy profiling in cloud, containers and clustered environments.
Performance monitoring and profiling of Jenkins, Bamboo, TeamCity, Gradle, Maven, Ant and JUnit.

Understanding garbage collection in .NET : GC roots

What is the garbage collector roots (GC roots) in .NET?

In .NET, the process of automatically freeing up memory that is no longer in use is known as garbage collection (GC). The garbage collector identifies objects that are no longer needed and then reclaims the memory that was allocated to them.

The garbage collector roots (GC roots) are a set of references through which an object can be reached from the outside world. These references serve as the starting point for the garbage collector to determine if an object is reachable or not. In other words, GC roots are entry points to the graph of objects in memory. Objects that are reachable through a chain of references from GC roots are considered live objects, and those that are not are considered candidates for garbage collection.

How to inspect GC roots

YourKit .NET Profiler has a special tool for inspecting GC roots, which displays all GC roots in the memory snapshot grouped by types. To view GC roots, use the Memory | GC Roots menu item.

Why are GC roots important?

1. Efficient memory management: By identifying unreachable objects, the garbage collector can reclaim memory, ensuring efficient memory utilization.

2. Preventing memory leaks: Proper identification of GC roots helps the garbage collector prevent memory leaks, which occur when objects that are no longer in use still hold onto memory.

3. Improved application performance: Efficient memory management leads to better application performance, as the application can avoid memory-related bottlenecks.

How to use GC roots when finding memory leaks

Memory leaks occur when objects that are no longer needed still occupy memory due to incorrect references. To find memory leaks, you can analyze the heap dump of your .NET application using YourKit .NET Profiler. Profiler allows you to inspect GC roots and trace the references that prevent certain objects from being garbage collected.

1. Capture memory snapshot: The first step is to capture memory snapshot of your running .NET application. You can do this using YourKit .NET Profiler by capturing memory snapshot.

2. Analyze the memory snapshot: Load the memory snapshot into the YourKit .NET Profiler, and inspect the GC roots and the object graph.

3. Identify suspected leaks: Look for objects that should not be in memory but are still being held due to references from GC roots. Check for unexpected or unnecessary references.

4. Fix and verify: Remove or update the references that cause memory leaks and verify by generating another heap dump to ensure that the leaks are resolved.

Types of GC roots

There are several types of GC roots in .NET runtime:

GC root type Description
Class Classes can hold objects via static fields.
Finalizer reference Objects are scheduled for finalization, but have not yet had their finalizer run are considered root until their finalizer has executed. Once the finalizer has run, the object is no longer rooted and can be collected if there are no other roots keeping it alive.
Handle Provides a means for accessing a managed object from unmanaged memory. The .NET Framework provides different types of handles (such as GCHandle) that allow users to control the garbage collection of specific objects.
Local variable on stack Local variables in active method frames are considered GC roots. These variables usually exist on the stack and reference objects in the heap.
Other Objects hold from garbage collection by .NET runtime for unspecified reasons.

YourKit uses cookies and other tracking technologies to improve your browsing experience on our website, to show you personalized content and targeted ads, to analyze our website traffic, and to understand where our visitors are coming from.

By browsing our website, you consent to our use of cookies and other tracking technologies in accordance with the Privacy Policy.