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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With