Garbage Collection

Garbage collection: GC

Garbage collection is complex. But it makes programs that use it much simpler. We want an idea of how GC works over time. We look at a real-world visualization.

Garbage collection visualization

First, I show a custom .NET Framework application called Memory Watcher. It continually polls Windows for information about a process's working set.

Find icon

Visualize system numbers. My program "Memory Watcher" can observe GC over time. It also gives you an idea of the sizes of the collections.

Garbage collection

The sawtooth line shows peaks where the garbage collection is triggered. It then dips after garbage collection has run. The memory is lower.

Program icon

You should have CLRProfiler on your system. It is free and useful. It can help you visualize memory usage of a process. It works for .NET processes.

Use CLR Profiler: MSDN

Open CLRProfiler binary (x86). Start the exe and then click on Start Application. Next, select a .NET application you want to observe.


Use your application. Now, use your Windows Forms application for a few seconds. Your program will likely enter garbage collection (silently).


Exit your custom application. Now you will return to CLRProfiler. Look at the panel that says "Garbage Collection Statistics" and click on the button.

The colorful lines, representing memory, keep building up. Then they are flushed away. The peaks are before the garbage collections, and the dips are after the collections.

Memory Usage
Just-in-time compiler: JIT

Open Process Explorer—you will see the columns "Methods Jitted" and "Gen 0 Collections". When a method is compiled in your .NET program, the "Methods Jitted" column cell will increase.

Process Explorer

Select a .NET process in the list. Your .NET application will be in the list in Process Explorer. Right click on it and then select properties. You will see another dialog box.

Framework: NET

Click on the .NET tab. Process Explorer keeps .NET runtime information in a separate tab. Here you will see a ".NET Performance Objects" drop-down.

Select Memory—this is the memory menu item in the drop-down menu. You will see lots of information about .NET performance. This can be useful for debugging or as a general picture.

We looked at tools that help you keep track of memory usage and understand how garbage collection works. We can use custom tools, or those provided by Microsoft.

C#: .NET