tags:

views:

107

answers:

4

Hello. I have a question about the property TotalVirtualMemory in the VB class ComputerInfo. I have checked this property from code on several different computers and the number is always 2047 MB. This is strange because I know for a fact that the virtual memory on these computers differ (~1GB, ~2GB and ~3.5GB). Does anyone know why it is like this and is there any other way to get the total and available virtual memory?

Best regards Daniel

A: 

I'm not sure, but it probably tops to the maximum memory that a 32-bit program can allocate (2GB). Have you tried to test it on a 64-bit machine?

Dario Solera
This is not the maximum memory a 32-bit program can *allocate*, it's the maximum memory a 32-bit program can map into its *address space* at the same time.
OregonGhost
+2  A: 

You can use WMI to get memory information:

ManagementObjectSearcher mgmtObjects = new ManagementObjectSearcher("Select * from Win32_OperatingSystem");

    foreach (var item in mgmtObjects.Get())
    {
        Console.WriteLine("FreeVirtualMemory:" + item.Properties["FreeVirtualMemory"].Value);
        Console.WriteLine("TotalVirtualMemorySize:" + item.Properties["TotalVirtualMemorySize"].Value);
    }
Tom Frey
This works like a charm if you change "Win32_ComputerSystem" to "Win32_OperatingSystem" as Martin says below. Thanks!
Daniel Enetoft
+2  A: 

This might be a case where WMI will provide the correct information for you. Have a browse around the avaliable objects using Scriptomatic, the Win32_PageFile object might contain what you are after.

Edit: It's Win32_OperatingSystem, not Win32_ComputerSystem, but otherwise Tom Frey's code is correct.

Martin
ups, you're right, thanks for pointing that out
Tom Frey
+1  A: 

TotalVirtualMemory does not return the amount of virtual memory on the computer. Rather, it returns the virtual address space available to the program.

This will almost always be 2GB, which is the amount of address space allocated to an operating program by windows (at least by win32). For more information on Virtual Address Space, you can check out "Advanced Windows" by Jeffrey Richter, which goes into far more detail.

To get this information, you can add a reference to System.Management.dll and run this

System.Management.ManagementObject logicalMemory = new  
     System.Management.ManagementObject("Win32_LogicalMemoryConfiguration.Name=\"LogicalMemoryConfiguration\"");

Console.WriteLine("Total virtual memory: {0}",logicalMemory["TotalVirtualMemory"].ToString());

On my work system, this outputs 2809756.

This object also supports these properties

  uint32 AvailableVirtualMemory;  //in Kb
  string Caption;
  string Description;
  string Name;                    //NO LONGER SUPPORTED
  string SettingID;
  uint32 TotalPageFileSpace;      //in Kb
  uint32 TotalPhysicalMemory;     //in Kb
  uint32 TotalVirtualMemory;      //in Kb

I hope this helps.

Cub