Q: How to get the amount of physical (RAM) memory?

A: By calling GlobalMemoryStatus or GlobalMemoryStatusEx.

Example
Using GlobalMemoryStatus.
Code:
#include <windows.h>
#include <stdio.h>

DWORD GetTotalPhysicalMemory()
{
   
   MEMORYSTATUS memStatus = {0};
   ::GlobalMemoryStatus(&memStatus);
   return memStatus.dwTotalPhys;
}

int main()
{
   const DWORD dwMBFactor = 0x00100000;
   DWORD dwTotalPhys = GetTotalPhysicalMemory();
   printf("Total physical memory: %u MB", dwTotalPhys / dwMBFactor);

   system("pause");
   return 0;
}
Example
Usig GlobalMemoryStatusEx
Code:
#include <windows.h>
#include <stdio.h>

DWORDLONG GetTotalPhysicalMemory()
{
   MEMORYSTATUSEX memStatusEx = {0};
   memStatusEx.dwLength = sizeof(MEMORYSTATUSEX);
   BOOL bRet = ::GlobalMemoryStatusEx(&memStatusEx);
   return memStatusEx.ullTotalPhys;
}

int main()
{
   const DWORD dwMBFactor = 0x00100000;
   DWORDLONG dwTotalPhys = GetTotalPhysicalMemory();
   printf("Total physical memory: %u MB", dwTotalPhys / dwMBFactor);

   system("pause");
   return 0;
}
Notes
  • GlobalMemoryStatusEx must be preferred because:
    • GlobalMemoryStatus function will return the correct amount of physical memory between 2 and 4 GB only if the executable is linked using the /LARGEADDRESSAWARE linker option.
    • GlobalMemoryStatus returns -1 (indicates an overflow) on computers with more than 4 GB of memory.

Related links