Get physical memory usage in megabytes
Solution 1
Using PerformanceCounter class, you can get PF Usage details:
PerformanceCounter pageCounter = new PerformanceCounter
("Paging File", "% Usage", "_Total", machineName);
You can find all the categories information here, Process Object.
ADDED, you can also get Available Memory details using PerformanceCounter
:
PerformanceCounter ramCounter = PerformanceCounter
("Memory", "Available MBytes", String.Empty, machineName);
Using PerformanceCounter
, NextValue()
method you can get the available memory value in MB
, later you can compare it with the threshold value to stop the desired Windows Services.
if (ramCounter.NextValue() > thresholdValue)
{
// ... Stop Desired Services
}
Reference: A Simple Performance Counter Application
Solution 2
If you don't mind calling out to a kernel function. The c++ code to do it is:
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
On codeproject you can find out how to call a kernel function from c#:
http://www.codeproject.com/Articles/1285/Calling-API-functions-using-C
Solution 3
Personally I would use with Win32 API GlobalMemoryStatusEx
call through P/Invoke.
You can find out more details here:- http://www.pinvoke.net/default.aspx/kernel32.globalmemorystatusex
ZioN
I love programming, its pretty cool to build things out of nothing. Stackover flow as helped with so many problems. But the greatest thing I learned through the answers, that I am just like every other developer. No one has, like a super power, of developing and has all the best answers. But that we all struggle, and if we struggle enough we find the solution. SOreadytohelp
Updated on June 05, 2022Comments
-
ZioN about 2 years
I am looking for away to get the current amount physical memory used in MB. Something like in the Task Manager
I am current using
PerformanceCounter("Memory", "Available MBytes", true);
but its also including the page files (I believe) which is not what I want. Also I want the option of getting the used and not the available memory.The application I am working on, will monitor the physical memory usage, until the desired threshold is reached. Then it will restart a few windows services.
If you curious as to why I am developing such a program. Some of our programs have memory leaks on the servers, and a we have to restart windows services to release the memory, until we sort out all the memory leaks, I am making this application to help keep the server going, and responsive.
-
ZioN over 11 yearsI am currently using
PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes", String.Empty, machineName);
but this does not give me the physical memory used, only what is available. and it seems to include the page files, which is what I do not want. -
Furqan Safdar over 11 yearsYou actually need to use
new PerformanceCounter("Paging File", "% Usage", "_Total", machineName);
instead of checking for Available Memory. Since you need PF Usage details instead so try using PerformanceCounter class. -
ZioN over 11 yearsYes I can understand what you say to be true. But then how come if we have 6GB of memory on the server, and the threshold is set to, lets say, 4.5GB, that means I must set my app, to check when there is 1.5GB available it does not start the process to restart the windows services, when the memory as shown in the TM is at 4.5GB?
-
Furqan Safdar over 11 yearsFor this you need to have a check on Available Memory instead of PF Usage,
new PerformanceCounter("Memory", "Available MBytes", String.Empty, machineName).NextValue();
. The value will be in MB later you can convert it to GB and then compare it with the threshold. -
Lloyd over 11 yearspinvoke.net/default.aspx/kernel32.globalmemorystatusex for the P/Invoke sig done for you :)
-
ZioN over 11 yearsSo I see I need to use
PerformanceCounter("Memory", "Available MBytes", String.Empty, machineName)
I see my calculate might have been wrong, from the used memory to the available. Having 6GB on your server, does not mean you will have 6000MB exactly, but more like 6143MB. So it did not start the restart process of the windows services, because it still had a few MBs to go, according to the threshold. Can you change your answer to this, so I can make it the right answer. -
Furqan Safdar over 11 yearsEdited, by adding the section of Available Memory.