Get available memory for a program

Hi,

Is there an easy way to retrieve the available RAM memory on a pc, just like the Windows NT Task Manager shows.

I start some processes in my progam that take a lot of memory, so I want to check at startup whether there's enough free RAM memory available on the machine for my program to run.

Thanks,
JDN
LVL 1
JDNAsked:
Who is Participating?
 
gemartiConnect With a Mentor Commented:
Which value do you want?

Take your pick


\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
GlobalMemoryInfo: TMemoryStatus; // holds the global memory status information
begin
   {set the size of the structure before the call}
  GlobalMemoryInfo.dwLength := SizeOf(GlobalMemoryInfo);

  {retrieve the global memory status...}
  GlobalMemoryStatus(GlobalMemoryInfo);

    {and display the information}
  Edit1.Text := IntToStr(GlobalMemoryInfo.dwLength)+' bytes';
  Edit2.Text := IntToStr(GlobalMemoryInfo.dwMemoryLoad)+' %';
  Edit3.Text := IntToStr(GlobalMemoryInfo.dwTotalPhys)+' bytes';
  Edit4.Text := IntToStr(GlobalMemoryInfo.dwAvailPhys)+ ' bytes';
  Edit5.Text := IntToStr(GlobalMemoryInfo.dwTotalPageFile)+ ' bytes';
  Edit6.Text := IntToStr(GlobalMemoryInfo.dwAvailPageFile)+ ' bytes';;
  Edit7.Text := IntToStr(GlobalMemoryInfo.dwTotalVirtual)+ ' bytes';
  Edit8.Text := IntToStr(GlobalMemoryInfo.dwAvailVirtual)+' bytes';
end;

end.
0
 
geobulCommented:
Hi,

try this:

procedure TForm1.Button1Click(Sender: TObject);
var
 PMemStat: ^TMemoryStatus;
begin
 New(PMemStat);
 GlobalMemoryStatus(PMemStat^);
 label1.Caption := Format ('Physical Memory Available to Windows: %10.0n KB', [pMemstat.dwAvailVirtual / 1024]);
 Dispose(PMemStat);
end;

About pMemstat.dw... from Help:

dwLength

Indicates the size of the structure. The calling process should set this member prior to calling GlobalMemoryStatus.

dwMemoryLoad

Specifies a number between 0 and 100 that gives a general idea of current memory utilization, in which 0 indicates no memory use and 100 indicates full memory use.

dwTotalPhys

Indicates the total number of bytes of physical memory.

dwAvailPhys

Indicates the number of bytes of physical memory available.

dwTotalPageFile

Indicates the total number of bytes that can be stored in the paging file. Note that this number does not represent the actual physical size of the paging file on disk.

dwAvailPageFile

Indicates the number of bytes available in the paging file.

dwTotalVirtual

Indicates the total number of bytes that can be described in the user mode portion of the virtual address space of the calling process.

dwAvailVirtual

Indicates the number of bytes of unreserved and uncommitted memory in the user mode portion of the virtual address space of the calling process.

Regards, Geo
0
 
geobulCommented:
Ops, the right example should be:

label1.Caption := Format ('Physical Memory Available to Windows: %10.0n KB', [pMemstat.dwTotalPhys / 1024]);
0
 
MoondancerCommented:
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if they are still open in 14 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20034754.html
http://www.experts-exchange.com/questions/Q.20060247.html
http://www.experts-exchange.com/questions/Q.20078853.html
http://www.experts-exchange.com/questions/Q.20090838.html
http://www.experts-exchange.com/questions/Q.20107595.html
http://www.experts-exchange.com/questions/Q.20141644.html
http://www.experts-exchange.com/questions/Q.20156077.html
http://www.experts-exchange.com/questions/Q.20174438.html
http://www.experts-exchange.com/questions/Q.20191040.html
http://www.experts-exchange.com/questions/Q.20199286.html
http://www.experts-exchange.com/questions/Q.20201528.html
http://www.experts-exchange.com/questions/Q.20225611.html
http://www.experts-exchange.com/questions/Q.20229350.html
http://www.experts-exchange.com/questions/Q.20229409.html
http://www.experts-exchange.com/questions/Q.20230450.html
http://www.experts-exchange.com/questions/Q.20239406.html
http://www.experts-exchange.com/questions/Q.20247213.html
http://www.experts-exchange.com/questions/Q.20257951.html
http://www.experts-exchange.com/questions/Q.20270004.html
http://www.experts-exchange.com/questions/Q.20275196.html
http://www.experts-exchange.com/questions/Q.20282031.html

To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20242687.html

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed in the link below
http://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @14 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.