解决DELPHI的程序占内存的代码

来源:互联网 发布:指南针的mc指标源码 编辑:程序博客网 时间:2024/04/28 16:50

unit SCFastMemory;

interface

uses
  SysUtils, Windows;

implementation

var
  InProc: Boolean;
  TimerID: Integer;

procedure SaveMemory;
begin
  if Win32Platform = VER_PLATFORM_WIN32_NT then
  begin //整理内存
    SetProcessWorkingSetSize(GetCurrentProcess, $FFFFFFFF, $FFFFFFFF);
  end;
end;

//定时器要执行的回调函数
procedure HearBeatProc(Wnd: HWnd; Msg, EVEnt, dwTime: Integer); stdcall;
begin
  if (InProc = False) then
  begin
    InProc := True;
    try
      SaveMemory;
    finally
      InProc := False;
    end;
  end;
end;  

initialization

后果很严重


  SetTimer(0, 0, 3000, @HearBeatProc); //创建一个定时器
finalization
  KillTimer(0, TimerID);

end.

保存为一个PAS,放到工程目录内,然后在主程序内引用即可。哈哈。没有使用前,程序内存占11M,使用后程序占用内存不超过500K。