Wednesday, May 20, 2009

Monitoring Commit Charge in Windows

I noticed that when the Commit Charge gets to be near or above my Physical memory (RAM) that my system starts getting slower. I wanted a simple way to monitor this and be alerted when a specified threshold is exceeded.

If you want to see what Commit Charge is, just open up Windows Task Manager and look at the status bar at the bottom of the Window. This is the value we want.

As it turns out WMI makes it extremely easy to do this. Below is a snippet of C# code that brings up an alert when Commit Charge > threshold.

public static void CheckCommitCharge()
{
System.Diagnostics.PerformanceCounter perfCounter = new System.Diagnostics.PerformanceCounter("Memory", "Committed Bytes");
long commitChargeInMB = perfCounter.RawValue / 1024 / 1024;

long threshold = 1900;
if (commitChargeInMB > threshold)
{
MessageBox.Show("Your system may become sluggish if you don't close some applications. \r\nThe Commit Charge is " + commitChargeInMB + " MB, but it should be below " + threshold + ".");
}}
It is actually quite easy to use this snippet. Follow the high level steps below to use it.
  1. Create a Win Forms application in Visual Studio.
  2. Open Program.cs and comment out the Application.Run(new Form1()); line, and put CheckCommitCharge(); in its place.
  3. Build the executable and run from a Windows scheduled task. Schedule the task to your comfort level. Remember, it could get annoying if you don’t quit some apps and you are reminded every 10 seconds or something. :)

No comments: