There's more to this one than you might at first suspect. Yeah, the basic
premise is very straight-forward. This sample provides you a means to determine
the amount of physical memory installed in the machine, the total size of the
machine's swap file, and the size of the virtual address space the operating
system has provided you with. For each of these metrics, you may obtain both the
total and available amounts. All this information is returned by the drop-in
ready CMemoryStatus class that's included with this download.
I've written a little sample utility that exercises CMemoryStatus by monitoring
(on a timer) how much of each of the memory amounts mentioned above are
available. It then offers you the ability to fully utilize your process's entire
address space. When you press the "Start" button in this test app, it begins
allocating chunks of memory 25 megabytes in a gulp until it's all gone. Note the
changes below in the Available memory entries on the "before" (right) and "after"
(left) forms
below:

That might be semi-interesting in and of itself, in many circumstances. Where
it gets really interesting is if you happen to be running on a system that
supports large address spaces. By default, this is any 64-bit operating
system, or 32-bit operating systems (Win2k+) where the boot.ini file has been
tweaked with a /3G switch. Ready for a shock? Check out how this same utility
runs on Windows 7 x64:

By toggling a single bit in your application's PE header, you can now
allocate nearly 4GB of data! At least when you're running on an x64 system. More
info is available in my recent article in Visual Studio Magazine
Online (see below). But here's the gist of it:
D:\Code\Samples\MemStatus>editbin /largeaddressaware memmax.exe
Microsoft (R) COFF Binary File Editor Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
D:\Code\Samples\MemStatus>
Pretty cool? So far, yeah, I think so! More testing required to be really
sure. Let me know if you run into troubles with this.
CMemoryStatus Enumerations
Public Enum MemoryTypes
memPhysical = 0
memPagefile = 1
memVirtual = 2
End Enum
CMemoryStatus Properties
| Available |
Returns the number of bytes available given one of the three
MemoryTypes enum values. |
| Change |
Returns the change in the number of bytes available, given one
of the three MemoryTypes enum values, since the last time the
Refresh method was called. |
| Load |
Returns the approximate percentage of physical memory that is in
use. |
| Total |
Returns the total number of bytes given one of the three
MemoryTypes enum values. |
CMemoryStatus Methods
| FormatBytes |
Returns a nicely formatted string, representing the number
passed, in Bytes, KB, MB, GB, or TB, as appropriate. |
| Refresh |
Calls GlobalMemoryStatusEx and refreshes all the cached
statistics provided by the class. |
To use CMemoryStatus, just drop it into your project, create a new instance, and
start querying it:
Public Sub Main()
Dim msg As String
Dim ms As CMemoryStatus
Set ms = New CMemoryStatus
msg = msg & "TotalPhysical = " & ms.FormatBytes(ms.Total(memPhysical), True) & vbCrLf
msg = msg & "AvailPhysical = " & ms.FormatBytes(ms.Available(memPhysical), True) & vbCrLf
msg = msg & "TotalPageFile = " & ms.FormatBytes(ms.Total(memPagefile), True) & vbCrLf
msg = msg & "AvailPageFile = " & ms.FormatBytes(ms.Available(memPagefile), True) & vbCrLf
' editbin /largeaddressaware memstatus.exe
msg = msg & "TotalVirtual = " & ms.FormatBytes(ms.Total(memVirtual), True) & vbCrLf
msg = msg & "AvailVirtual = " & ms.FormatBytes(ms.Available(memVirtual), True) & vbCrLf
Debug.Print msg
Clipboard.Clear
Clipboard.SetText msg
MsgBox msg, vbInformation, "Memory Status"
End Sub