Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get physical memory usage in megabytes

I am looking for away to get the current amount physical memory used in MB. Something like in the Task Manager

enter image description here

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.

like image 285
ZioN Avatar asked Jan 15 '23 00:01

ZioN


1 Answers

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

like image 55
Furqan Safdar Avatar answered Jan 25 '23 10:01

Furqan Safdar