Dot Net Perls
 

Garbage Collection Visualizations

by Sam Allen

Problem

Observe an example of garbage collection, just to learn more about it and get some insight into the general platform. We may not have a specific bug to fix here, but we want an idea of how it works over time. We want to see a real-world visualization of garbage collection. Also we want to look at some other tools we can observe garbage with.

Solution

There are several tools I want to introduce here. First, I will show my custom application called Memory Watcher, which continually polls Windows for information about a process's working set. Then I will touch on CLRProfier and Process Monitor, both provided by Microsoft.

Visualizing system numbers

My .NET program "Memory Watcher" can observe collections over time, as well. It can also give you an idea of the proportions of the collections. Collections that are very frequent or big could be seen in this chart. The following chart shows the memory usage of a program that runs in the background frequently and regularly.

The sawtooth line shows peaks where the garbage collection is triggered, and then dips where it has already run and the memory is lower. We can see that this program doesn't consume too much memory over time. For more about this, see my article about Memory Watcher and how I used it on web browsers.

Garbage collection and CLRProfiler

You should definitely have CLRProfiler on your system, as it is not only free but very useful. It can help you visualize memory usage of a process. (Note that this only works for .NET processes, so would not work for web browsers, for example.) Here is how to get started with this tool, and then what you might see.

  1. Open CLRProfiler binary (x86)
    Start the exe and then click on Start Application. Next, select a .NET application you want to observe.
  2. Use your app for a few seconds
    Now, use your Windows Forms application for a few seconds--around 15 or 20 would be fine. Your program will likely enter garbage collection (silently) in this time period.
  3. ... and then Exit it
    Exit your custom application and then you will return to CLRProfiler. Now, look at the panel that says "Garbage Collection Statistics" and click on the button. You may see an image similar to the following.

The colorful lines (representing memory) keep building up, until they are flushed away and then the process starts again. The peaks are right before the garbage collections, and the dips are right after the collections. This is a good way to visualize the collections in .NET apps.

Garbage collection and Process Explorer

Open Process Explorer (procexp.exe) as administrator, and you should see columns called "Methods Jitted" and "Gen 0 Collections". When a method is compiled in your .NET program, the "Methods Jitted" column cell will increase. This can give you a rough idea of how big and complex your .NET app is. For more information about memory in Process Explorer, follow these steps.

  1. 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.
  2. Click on .NET tab
    Process Explorer keeps .NET runtime information in a separate tab. Click on that tab, and then you will see a ".NET Performance Objects" drop-down.
  3. Select "Memory"
    Select 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.

Conclusion

These tools can help you keep track of memory usage and understand how garbage collection works in your application better. We can use tools like those provided by Microsoft, or custom tools. Garbage collection is powerful and efficient, but it needs to be kept in check in case our algorithms have a major inefficiency.

© 2008 Sam Allen. All rights reserved.

Ads by The Lounge