Platform SDK: Memory

GlobalMemoryStatusEx

The GlobalMemoryStatusEx function obtains information about the system's current usage of both physical and virtual memory.

BOOL GlobalMemoryStatusEx(
  LPMEMORYSTATUSEX lpBuffer   // memory status structure
);

Parameters

lpBuffer
[in/out] Pointer to a MEMORYSTATUSEX structure. GlobalMemoryStatusEx stores information about current memory availability in this structure.

Return Values

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

You can use the GlobalMemoryStatusEx function to determine how much memory your application can allocate without severely impacting other applications.

The information returned by the GlobalMemoryStatusEx function is volatile. There is no guarantee that two sequential calls to this function will return the same information.

Example

The program following shows a simple use of the GlobalMemoryStatusEx function.

//  Sample output:

//  c:\>globalex
//  78 percent of memory is in use.
//  There are   65076 total Kbytes of physical memory.
//  There are   14248 free Kbytes of physical memory.
//  There are  150960 total Kbytes of paging file.
//  There are   88360 free Kbytes of paging file.
//  There are  1fff80 total Kbytes of virtual memory.
//  There are  1fe770 free Kbytes of virtual memory.
//  There are       0 free Kbytes of extended memory.

#define _WIN32_WINNT 0x0500

#include <windows.h>

// Use to change the divisor from Kb to Mb.

#define DIV 1024
// #define DIV 1

char *divisor = "K";
// char *divisor = "";

// Handle the width of the field in which to print numbers this way to
// make changes easier. The asterisk in the print format specifier
// "%*I64d" takes an int from the argument list, and uses it to pad 
// and right-justify the number being formatted.
#define WIDTH 7

void main(int argc, char *argv[])
{
  MEMORYSTATUSEX statex;

  statex.dwLength = sizeof (statex);

  GlobalMemoryStatusEx (&statex);

  printf ("%ld percent of memory is in use.\n",
          statex.dwMemoryLoad);
  printf ("There are %*I64d total %sbytes of physical memory.\n",
          WIDTH, statex.ullTotalPhys/DIV, divisor);
  printf ("There are %*I64d free %sbytes of physical memory.\n",
          WIDTH, statex.ullAvailPhys/DIV, divisor);
  printf ("There are %*I64d total %sbytes of paging file.\n",
          WIDTH, statex.ullTotalPageFile/DIV, divisor);
  printf ("There are %*I64d free %sbytes of paging file.\n",
          WIDTH, statex.ullAvailPageFile/DIV, divisor);
  printf ("There are %*I64x total %sbytes of virtual memory.\n",
          WIDTH, statex.ullTotalVirtual/DIV, divisor);
  printf ("There are %*I64x free %sbytes of virtual memory.\n",
          WIDTH, statex.ullAvailVirtual/DIV, divisor);

  // Show the amount of extended memory available.

  printf ("There are %*I64x free %sbytes of extended memory.\n",
          WIDTH, statex.ullAvailExtendedVirtual/DIV, divisor);

}

Requirements

  Windows NT/2000: Requires Windows 2000.
  Windows 95/98: Unsupported.
  Header: Declared in Winbase.h; include Windows.h.
  Library: Use Kernel32.lib.

See Also

Memory Management Overview, Memory Management Functions, MEMORYSTATUSEX