Finding your RAM

I spent a good chunk of my time this morning trying to figure out how to programmatically get the total amount of RAM on a machine because of a performance test project I’m working on. At first I was trying to use the Memory performance counters in .NET, but I wasn’t able to figure out what counters to use. I tried all types of combinations and the results never did add up correctly. I tired doing several searches on Google for RAM and memory, but came up dry. Out of desperation, I fired off an email to one of the programmers on staff to see if they could help me out and gave Google one last shot. This time I did a search for Physical Memory and came across this post: Interrogating Systems with WMI.

This was the first time I had heard anything about the System.Management namespace and it looked pretty interesting. I did some poking around and found it could be very useful in the future for testing purposes.

Anyway, I was able to take what John O’Donnell posted and modify it a little bit to give me exactly what I needed:

ManagementObject ram = new ManagementObject("Win32_ComputerSystem.Name='" + Environment.MachineName + "'");