MEMUSG - Memory Usage

How to inspect the total of memory used by a specific program with C#.

Versão em português

I’m a crazy guy when I’m using a computer. I am very dissatisfied with the current programs, moreover with their memory consumption. Recently, I got insane when my system starts to slow down and I realized that my browser was using more than 3GB of RAM. Important information about: there was just 4 tabs open!

Well, despite that craziness, I also need to inspect the performance of some of my job applications and I needed to get a memory consumption summary.

For example: in some Windows versions, there isn’t a simple way to show the total of memory that an application like Google Chrome is using.

If you inspect the Task Manager application, you will see few memory consumption in first look, but, when you scroll down the list, you realize that there is even more process related with Google Chrome with more memory consumption that you’ve saw on the top of the list.

Therefore I decided to do a little research and try to make up a console application that I pass the name of the program or the process as a parameter and can get the total of RAM that is being used by it. In the end of this post, I put the original link where that post was based on.

This post article is that you can use the Windows command prompt (aka cmd.exe) to run the program and get the total memory used by a procces.

In the code below I show how the memusg.cs works.

using System; using System.Diagnostics; public static class memusg { static void Main(string[] args) { if(args == null || args.Length == 0) { Console.WriteLine("Usage: memusg <process name>"); return; } getallmemoryusage(args[0]); } private static void getallmemoryusage(string pName) { double f = 1024.0; Process[] localByName = Process.GetProcessesByName(pName.ToLower()); decimal max = 0m; foreach (Process p in localByName) { max += (decimal)(p.PrivateMemorySize64 / f / f); } Console.WriteLine("Total private memory usage: {0:N0} MB", max); } }

The main target of that code is to find out for the process or program sent as argument and sum the private memory use (in MB) of all the process with same name printing the final value on the console.

That feature it’s executed on the getallmemoryusage() method that receive the process name as argument.

To start it’s necessary crate an double object to express the total amount of memory used in megabytes that will by used as a divisor.

The localByName object stores the process list got by the Process class.

Note that you must include the System.Diagnostics namespace in your using clauses to can use the Process class.

Moving on there is a loop where the process list is read and all the process with the argument name have its memory usage added to the “max” variable. The value is already stored in MB using the division mentioned above.

That’s it! And you? What is your craziness with the modern programs?


  1. Post Original no MSDN
  2. Classe System.Diagnostics.Process