Reputation: 189
I would like to know how can I get the size of my RAM through C++ (on Windows 7).
Upvotes: 11
Views: 28815
Reputation: 53496
You want to use the GlobalMemoryStatusEx
which returns a MEMORYSTATUSEX
. The field you want is called ullTotalPhys
.
Upvotes: 4
Reputation: 361442
Use GetPhysicallyInstalledSystemMemory
to retrieve the amount of RAM that is physically installed on the computer.
(Note that this requires Windows Vista SP1 or later. The function is not available on earlier versions of the Windows operating system.)
The remarks on MSDN say:
The GetPhysicallyInstalledSystemMemory function retrieves the amount of physically installed RAM from the computer's SMBIOS firmware tables. This can differ from the amount reported by the GlobalMemoryStatusEx function, which sets the ullTotalPhys member of the MEMORYSTATUSEX structure to the amount of physical memory that is available for the operating system to use. The amount of memory available to the operating system can be less than the amount of memory physically installed in the computer because the BIOS and some drivers may reserve memory as I/O regions for memory-mapped devices, making the memory unavailable to the operating system and applications.
The amount of physical memory retrieved by the GetPhysicallyInstalledSystemMemory function must be equal to or greater than the amount reported by the GlobalMemoryStatusEx function; if it is less, the SMBIOS data is malformed and the function fails with ERROR_INVALID_DATA. Malformed SMBIOS data may indicate a problem with the user's computer.
That means, you would also want to look at GlobalMemoryStatusEx
.
Upvotes: 12
Reputation: 189
Okay, guys! I've found the solution by doing this like that [guru mode on]:
#define _WIN32_WINNT 0x0501 // I misunderstand that
#include <windows.h>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex); // I misunderstand that
GlobalMemoryStatusEx (&statex);
cout << "Physical RAM => " << (float)statex.ullTotalPhys/(1024*1024*1024)<< endl;
system("PAUSE");
return EXIT_SUCCESS;
}
I had to define _WIN32_WINNT 0x0501, but i don't know why [guru mode is off].
If somebody could explain me what it is doing and why it doesn't work without it.
One more thing, what is that:
statex.dwLength = sizeof (statex);
Upvotes: 7
Reputation: 11
The 0x501
is the WindowsXP version, i.e. the MEMORYSTATUSEX
struct is not supported by some older Windows versions. Your windef.h probably points to a lower WINVER
than 0x5XX
.
Upvotes: 1
Reputation: 7744
On Windows:
typedef BOOL (WINAPI *PGMSE)(LPMEMORYSTATUSEX);
PGMSE pGMSE = (PGMSE) GetProcAddress( GetModuleHandle( TEXT( "kernel32.dll" ) ), TEXT( "GlobalMemoryStatusEx") );
if ( pGMSE != 0 )
{
MEMORYSTATUSEX mi;
memset( &mi, 0, sizeof(MEMORYSTATUSEX) );
mi.dwLength = sizeof(MEMORYSTATUSEX);
if ( pGMSE( &mi ) == TRUE )
os << "RAM: " << mi.ullTotalPhys / 1048576 << "MB";
else
pGMSE = 0;
}
if ( pGMSE == 0 )
{
MEMORYSTATUS mi;
memset( &mi, 0, sizeof(MEMORYSTATUS) );
mi.dwLength = sizeof(MEMORYSTATUS);
GlobalMemoryStatus( &mi );
os << "RAM: " << mi.dwTotalPhys / 1048576 << "MB";
}
On Linux:
Read /proc/meminfo
.
Upvotes: 5