Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Total and Available Memory When > 4 GB Installed

Tags:

delphi

Is there a a way to obtain Total and Available memory when more than 4 GB of memory is installed with Delphi 2010 on Windows 7?

This code does not return more than 3.99 GB:

var
  Memory: TMemoryStatus;

Memory.dwLength := SizeOf( Memory );
GlobalMemoryStatus( Memory );
dwTotalPhys1.Caption := 'Total memory: ' + IntToStr( Memory.dwTotalPhys ) + ' Bytes ' + '(' + FormatByteSize
( Memory.dwTotalPhys ) + ')';
dwAvailPhys1.Caption := 'Available memory: ' + IntToStr( Memory.dwAvailPhys ) + ' Bytes ' + FormatByteSize
( Memory.dwAvailPhys ) + ')';
like image 228
Bill Avatar asked Oct 22 '11 13:10

Bill


2 Answers

You need to use the GlobalMemoryStatusEx. GlobalMemoryStatus is limited to 4gb

I don't know if it's already defined in Delphi with its structure TMemoryStatusEx or not (it would be based on the MEMORYSTATUSEX of the Windows API.)

The fields you'll have to look are ullTotalPhys and ullAvailPhys. They are 64 bit unsigned integers.

I was forgetting, it's supported only by Windows >= 2000, but this shouldn't be a problem anymore.

like image 168
xanatos Avatar answered Sep 20 '22 12:09

xanatos


@Bill
You need to use GlobalMemoryStatusEx. It is not perfect but it is better than GlobalMemoryStatus. How? With GlobalMemoryStatus, on a 4GB computer with Win32, a 32 bit app shows only 2GB installed. With GlobalMemoryStatusEx, the same app will show 3GB installed. A bit closer to the truth!

This code works as it is in Delphi XE (and up):

uses Windows;

function GetSystemMem: string;  { Returns installed RAM (as viewed by your OS) in GB, with 2 decimals }
VAR MS_Ex : MemoryStatusEx;
begin
 FillChar (MS_Ex, SizeOf(MemoryStatusEx), #0);
 MS_Ex.dwLength := SizeOf(MemoryStatusEx);
 GlobalMemoryStatusEx (MS_Ex);
 Result:= Real2Str(MS_Ex.ullTotalPhys / GB, 2)+ ' GB';
end;

Please note that using some API functions will probably never give you the TOTAL amount, if this amount is over 3GB and the OS is Win 32. Why? Because Windows32 itself cannot 'see' all the memory! You need to access the BIOS directly and read the hardware values there. HOWEVER, in some cases it may not be needed to do this: why bother to show that your PC has 4GB RAM if only 3 can be accessed? What I have done IN MY CASE, I changed the message:

Installed RAM: 3GB (or more)

with

Available RAM: 3GB

Again, I don't know if this is suited also in your case.

like image 43
Server Overflow Avatar answered Sep 20 '22 12:09

Server Overflow