#include <windows.h>
#include <stdio.h>
#include <tchar.h>
// Convert bytes to MB
#define DIV 1048576
// Specifies the width for right-justifying the output.
#define WIDTH 10
int main()
{
// A structure containing information about the system's memory usage, including physical, virtual, and paging files.
MEMORYSTATUSEX statex;
// initializes the structure and ensures the correct memory size is passed to the API function.
statex.dwLength = sizeof(statex);
// fetches the memory-related information and populates the statex structure.
GlobalMemoryStatusEx(&statex);
// The asterisk in: "%*I64d" takes an integer argument and uses it to pad and right justify the number
// "%*I64d" pads the number with spaces to occupy WIDTH characters, aligning it to the right.
/*
dwMemoryLoad: Percentage of memory currently in use.
ullTotalPhys: Total physical memory available on the system.
ullAvailPhys: Free physical memory available.
ullTotalPageFile: Total size of the paging file.
ullAvailPageFile: Free size of the paging file.
ullTotalVirtual: Total size of the virtual memory.
ullAvailVirtual: Free size of the virtual memory.
ullAvailExtendedVirtual: Free size of extended virtual memory (rarely used).
*/
// Display memory details in MB
_tprintf(TEXT("There is %*ld percent of memory in use.\n"), WIDTH, statex.dwMemoryLoad);
_tprintf(TEXT("There are %*I64d total Mbytes of physical memory.\n"), WIDTH, statex.ullTotalPhys / DIV);
_tprintf(TEXT("There are %*I64d free Mbytes of physical memory.\n"), WIDTH, statex.ullAvailPhys / DIV);
_tprintf(TEXT("There are %*I64d total Mbytes of paging file.\n"), WIDTH, statex.ullTotalPageFile / DIV);
_tprintf(TEXT("There are %*I64d free Mbytes of paging file.\n"), WIDTH, statex.ullAvailPageFile / DIV);
_tprintf(TEXT("There are %*I64d total Mbytes of virtual memory.\n"), WIDTH, statex.ullTotalVirtual / DIV);
_tprintf(TEXT("There are %*I64d free Mbytes of virtual memory.\n"), WIDTH, statex.ullAvailVirtual / DIV);
_tprintf(TEXT("There are %*I64d free Mbytes of extended memory.\n"), WIDTH, statex.ullAvailExtendedVirtual / DIV);
return 0;
}
/*
run:
There is 93 percent of memory in use.
There are 32701 total Mbytes of physical memory.
There are 2105 free Mbytes of physical memory.
There are 131005 total Mbytes of paging file.
There are 29604 free Mbytes of paging file.
There are 134217727 total Mbytes of virtual memory.
There are 134213583 free Mbytes of virtual memory.
There are 0 free Mbytes of extended memory.
*/