We help IT Professionals succeed at work.

How to Free up the Ram

Dinkleburger
Dinkleburger asked
on
Hello All

Anybody can give me Complete source code for procedure
 when i press Button1
  the RAM on my computer is free up
 or even Force the ram to free as much as possible

thankyou in advance
DB
Comment
Watch Question

Commented:
You can try allocate large memory buffer, and then free it.
May be memory manager free som ram.

Author

Commented:
Sounds good but can you show me some working source code i am a new at delphi
thanks
Commented:
There is little example. It is realy work, but you may experiment with buffer size. The size of the buffer influences productivity.

unit frmuMain;

interface

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

type
  TfrmMain = class(TForm)
    btnFreeMem: TButton;
    cbxBufSize: TComboBox;
    Label1: TLabel;
    memInfo: TMemo;
    procedure btnFreeMemClick(Sender: TObject);
    procedure ShowMemStatusInfo;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

function TryFreeMem(const aBufSize:Integer):boolean;

implementation

{$R *.DFM}

function TryFreeMem(const aBufSize:Integer):boolean;
var
  tmpBuffer: PChar;
  oldCursor:TCursor;
begin
  oldCursor:=Screen.Cursor;
  Result:=True;
  try
    GetMem(tmpBuffer, aBufSize);
    try
      Screen.Cursor:=crHourGlass;
      FillChar(tmpBuffer^, aBufSize, 0); //May take some time for large buffer
    finally
      Screen.Cursor:=oldCursor;
      FreeMem(tmpBuffer);
    end;
  except
    Result:=False;
  end;
end;

procedure TfrmMain.ShowMemStatusInfo;
var
  MemStat:TMemoryStatus;
  fMemPercent:Extended;
begin
  MemStat.dwLength := SizeOf(TMemoryStatus);
  GlobalMemoryStatus(MemStat);
  fMemPercent:=(MemStat.dwTotalPhys-MemStat.dwAvailPhys);
  fMemPercent:=fMemPercent*100/MemStat.dwTotalPhys;
  fMemPercent:=Round(fMemPercent);
  With MemStat do
  begin
    memInfo.Lines.Add(format('Mem usage: '+'%f%%',[fMemPercent]));
    memInfo.Lines.Add(format('Total physical: '+'%d',[dwTotalPhys]));
    memInfo.Lines.Add(format('Available physical: '+'%d',[dwAvailPhys]));
    memInfo.Lines.Add(format('Total virtual: '+'%d',[dwTotalVirtual]));
    memInfo.Lines.Add(format('Available virtual: '+'%d',[dwAvailVirtual]));
  end;
end;

procedure TfrmMain.btnFreeMemClick(Sender: TObject);
begin
  memInfo.Lines.Clear;
  memInfo.Lines.Add('Memory status before:');
  ShowMemStatusInfo;
  TryFreeMem(StrToInt(cbxBufSize.Text)* 1048576);
  memInfo.Lines.Add('');  
  memInfo.Lines.Add('Memory status after:');
  ShowMemStatusInfo;
end;

end.

//////////////////////////////////////////////////////////////////
// frmuMain.dfm

object frmMain: TfrmMain
  Left = 191
  Top = 109
  Width = 331
  Height = 321
  Caption = 'Memory manager test'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 8
    Top = 8
    Width = 49
    Height = 13
    Caption = 'Buffer size'
  end
  object btnFreeMem: TButton
    Left = 96
    Top = 24
    Width = 75
    Height = 25
    Caption = 'Free Mem'
    TabOrder = 0
    OnClick = btnFreeMemClick
  end
  object cbxBufSize: TComboBox
    Left = 8
    Top = 24
    Width = 73
    Height = 21
    ItemHeight = 13
    TabOrder = 1
    Text = '8'
    Items.Strings = (
      '8'
      '16'
      '24'
      '32'
      '64'
      '80'
      '96'
      '112'
      '128'
      '144'
      '160'
      '176'
      '192'
      '224'
      '256'
      '512')
  end
  object memInfo: TMemo
    Left = 0
    Top = 64
    Width = 323
    Height = 230
    Align = alBottom
    TabOrder = 2
  end
end
JDN

Commented:
Hi Dinkleburger,

This is a suggestion someone on this board gave me:

SetProcessWorkingSetSize(GetCurrentProcess, $FFFFFFFF, $FFFFFFFF);

Place this code under Button1, and your Ram (in the Windows Task Manager) decreases dramatically.

JDN

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.20267246.html
http://www.experts-exchange.com/questions/Q.20279387.html
http://www.experts-exchange.com/questions/Q.20279679.html
http://www.experts-exchange.com/questions/Q.20280760.html
http://www.experts-exchange.com/questions/Q.20281381.html
http://www.experts-exchange.com/questions/Q.20280833.html
http://www.experts-exchange.com/questions/Q.20294654.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

Explore More ContentExplore courses, solutions, and other research materials related to this topic.