Get Virtual Memory (>4GM) by WMI
I used VB6.0 to get total virtual memory and free virtual memory by using WMI query. The problem is that when a system has more than 4GM RAM or disk paging size for memory, the result values are not right. I thought that it may be caused by the variant value type in VB6.0 that cannot handle Uint64 integer type and the values may be truncated.
I write a C# test code to get these values by using WMI objects in Management namespace. To my supprise, I get the same values as before in the case of over 4GM. The following is the code. Is there anything wrong? Or it is just the underneath WMI APIs bug? Is there any way to get these values in other ways in C#?
using System;
using System.Management;
namespace TestConsole
{
public class WMIQuery
{
public WMIQuery(){}
public static void QueryAll()
{
ManagementObjectSearcher searcher = new
ManagementObjectSearcher("select FreeVirtualMemory,
TotalVirtualMemorySize from Win32_OperatingSystem");
foreach (ManagementObject theInstance in searcher.Get())
{
Console.WriteLine("InstancePath: <{0}>", theInstance.Path);
PropertyDataCollection theProperties = theInstance.Properties;
foreach (PropertyData theData in theProperties)
{
Console.WriteLine("\tName: <{0}> Value: <{1}> Type: <{2}>",
theData.Name, theData.Value, theData.Type);
}
}
}
}
}
[1596 byte] By [
David Chu] at [2007-11-11 6:53:36]

# 2 Re: Get Virtual Memory (>4GM) by WMI
That is very interesting point(current versions of 32-bit Windows not supporting any more than 4 GB of addressable memory). Can this be confirmed?
If this is true, the free memory obtained from WMI call should be within this limit as accessible memory. From what I get, the numbers are not correct. At first, it may be around 700MB free and later on it shrinks to less than a limit I am monitor to. Actually, the memory I get by using other tools like task manager is big enough over my limit.
By the way, I found two API functions called GlobalMemoryStatus() and GlobalMemoryStatusEx() from Microsoft MSDN(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/globalmemorystatus.asp). The first one does not support computer more than 4GM. And for this reason, the next function is recommended. I am not sure what API function is used by WMI class. If the first one is used, that explains why the returned results are not correct for computer over 4GM RAM.
Any comments?
# 4 Re: Get Virtual Memory (>4GM) by WMI
After further investigation, I found that it is not a simple 4GM limit problem in Windows. I tried it in both Win 2003 Ent Server and 2003 Std Server PCs with about 4GM phys mem and 1GM page size. The total virtual mem does not look right. It is not a simple math of free phy mem plus free page mem(2,054,920 <> 560,376 + 3,713,860) . I attached my updated codes and the result.
I am confused. What is the correct definition of total virtual memory? Is there way to verify it by retrieving other mem numbers?
By the way, I use winmsd to verify these numbers. The results are consistent with those I get from my code. However, the total virtual mem is less than phy mem. How can it be explained?
public static void QueryAll()
{
string sQuery = "select FreePhysicalMemory, FreeSpaceInPagingFiles, FreeVirtualMemory, SizeStoredInPagingFiles, TotalSwapSpaceSize, TotalVisibleMemorySize, TotalVirtualMemorySize from Win32_OperatingSystem";
QueryWMI(sQuery);
sQuery = "select TotalPhysicalMemory from Win32_ComputerSystem";
QueryWMI(sQuery);
return;
}
private static void QueryWMI(string sWMIQuery)
{
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(sWMIQuery);
foreach (ManagementObject theInstance in searcher.Get())
{
Console.WriteLine("InstancePath: <{0}>", theInstance.Path);
PropertyDataCollection theProperties = theInstance.Properties;
foreach (PropertyData theData in theProperties)
{
Console.WriteLine("\tName: <{0}> Value: <{1}> Type: <{2}>",
theData.Name, theData.Value, theData.Type);
}
}
return;
}
InstancePath: <>
Name: <FreePhysicalMemory> Value: <560,376> Type: <UInt64>
Name: <FreeSpaceInPagingFiles> Value: <3,713,860> Type: <UInt64>
Name: <FreeVirtualMemory> Value: <2,054,920> Type: <UInt64>
Name: <SizeStoredInPagingFiles> Value: <4,096,808> Type: <UInt64>
Name: <TotalSwapSpaceSize> Value: <> Type: <UInt64>
Name: <TotalVirtualMemorySize> Value: <2,097,024> Type: <UInt64>
Name: <TotalVisibleMemorySize> Value: <1,047,536> Type: <UInt64>
InstancePath: <>
Name: <TotalPhysicalMemory> Value(32): <1,072,676,864> Type: <UInt64>