Friday, March 18, 2011

Determine Memory Usage of a Process

Today's post becomes, after the long preceeding posts, a bit shorter again.
I want to show you, how to determine the memory usage of a specific process.
The property needed for that is called WorkingSet64 from the class System.Diagnostics.Process. It returns the number of bytes, which are occupied by the process in the RAM.
To read out this number for some process, this first has to be "grabbed", for example with the function Process.GetProcessesByName() (some background information about that I provided in this post).
The function looks for processes of the given name and saves them in the resulting array.
The following code determines the memory usage of the given process by determining above mentioned property of the first process in the list - so if there are multiple processes of the same name, the code has to be altered probably.
The example determines the memory usage of the first opened Notepad window:

Process[] Application;
Application = Process.GetProcessesByName("notepad");
long MemorySize = Application[0].WorkingSet64;

No comments:

Post a Comment