Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I want to fetch the FreePhysicalMemory of os using c#.
i used WMI query for Win32_OperatingSystem class but it is not giving the accurate value
plz help me
TIA

What I have tried:

below is my code

ObjectQuery wql = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql);
ManagementObjectCollection results = searcher.Get(); 
foreach (var result in results)
{
 amountFree = Convert.ToInt32( result["FreePhysicalMemory"]);
}
Posted
Updated 19-Nov-17 22:24pm

Try this:
ManagementObjectSearcher search = new ManagementObjectSearcher("root\\CIMV2", "Select TotalVisibleMemorySize, FreePhysicalMemory from Win32_OPeratingSystem");

foreach (ManagementObject x in search.Get())
    {

    ulong totalMemory = (ulong)x["TotalVisibleMemorySize"];
    ulong freeMemory = (ulong)x["FreePhysicalMemory"];
    }
freeMemory is returned in KB, and seems about right on my system. It's difficult to be precise, because the system free memory varies all the time as processes allocate and free memory as a part of their normal activity.
 
Share this answer
 
Quote:
i used WMI query for Win32_OperatingSystem class but it is not giving the accurate value
How do you know that it is not accurate?

There may be two reasons that the result is not as expected:

  1. You missed that the value is in kilobytes
  2. There may be less free memory than expected when used by the system for prefetching (Prefetcher - Wikipedia[^])
 
Share this answer
 
Comments
sumitk.cadc 20-Nov-17 13:29pm    
hello sir,
i am cross checking the values with task manager's performance tab data.
actually i want to fetch all data of performance and networking data.i am using performance counters for it.
sumitk.cadc 20-Nov-17 13:29pm    
hello sir,
i am cross checking the values with task manager's performance tab data.
actually i want to fetch all data of performance and networking data.i am using performance counters for it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900