Getting VM Memory Usage and Demand Programmatically

A couple of people have recently asked me how to get virtual machine memory usage and memory demand programmatically.  What I am referring to is the information that we display here in Hyper-V Manager:

image

I have blogged about this in the past – but things have changed a bit since then – so I thought I would post an updated code sample.  Right here:


There are two main things to call out about this sample:

  • This information is all exposed on Msvm_SummaryInformation – which is documented here.
  • Memory demand is actually reported as a percentage of the used memory in the underlying API.  So to get the same information as is displayed by Hyper-V Manager – you have to do some math (included in the sample above)

Cheers,
Ben