本文整理自网络,侵删。
下面这个例子就是自定义时间清理内存空间。
unit TrimWorkingSet;
interface
implementation
uses
SysUtils, Windows, ExtCtrls;
type
TTrimmer = class(TTimer) //继承自TTimer定时器类
public
procedure TimerTick(Sender: TObject);
end;
procedure TTrimmer.TimerTick(Sender: TObject);
var
CurrentPID, FocusedPID: THandle;
begin
CurrentPID := GetCurrentProcessId;//获取当前进程ID
GetWindowThreadProcessId(GetForegroundWindow, @FocusedPID);//获取顶层窗口进程ID
if (Win32Platform = VER_PLATFORM_WIN32_NT) and //是否运行在windows NT类系统
(CurrentPID <> FocusedPID) then //并且顶层窗口进程不为当前进程时进行内存整理
SetProcessWorkingSetSize(CurrentPID, Cardinal(-1), Cardinal(-1) );
end;
var
Timer: TTrimmer;
initialization
Timer := TTrimmer.Create(nil);
Timer.Interval := 30000;//每半分钟定时检测
Timer.OnTimer := Timer.TimerTick;
finalization
if Assigned(Timer) then
Timer.Free;
end.
相关阅读 >>
Delphi的字符截取函数leftstr,midstr,rightstr的介绍以及字符串拆分
Delphi application.restore; 简单用法
更多相关阅读请进入《Delphi》频道 >>