'How can I get information about RAM and virtual memory
I need to get information about how much total RAM and how much virtual memory. And also how much virtual memory and RAM is currently used by the system, as in Task Manager. At the moment, I just figured out how to get the total amount of RAM, I have difficulties with the rest
ObjectQuery wql = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql);
ManagementObjectCollection results = searcher.Get();
double totalMemory = 0;
foreach (ManagementObject item in results)
{
var res = Convert.ToDouble(item["TotalVisibleMemorySize"]);
totalMemory = Math.Round((res / 1024), 2);
}
Solution 1:[1]
Firstly you can add references to your project. In the opening form select Assemblies and after that select Framework. You can see a list of Assemblies. Select "System. Management" from the list and add it to the project. Then use this code:
public string ConvertKB(string str)
{
int val = Int32.Parse(str);
val = (int)((val / 1024) / 1024);
return val.ToString() + " GB";
}
public void GetRAMUse()
{
ObjectQuery wql = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql);
ManagementObjectCollection results = searcher.Get();
foreach (ManagementObject result in results)
{
listBox1.Items.Add("Total Visible Memory: " + ConvertKB(result["TotalVisibleMemorySize"].ToString()));
listBox1.Items.Add("Free Physical Memory: " + ConvertKB(result["FreePhysicalMemory"].ToString()));
listBox1.Items.Add("Total Virtual Memory: " + ConvertKB(result["TotalVirtualMemorySize"].ToString()));
listBox1.Items.Add("Free Virtual Memory: " + ConvertKB(result["FreeVirtualMemory"].ToString()));
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ramin Faracov |