delphi 内存管理,定期释放

创建一个定时器,设置每隔N秒触发一次,也就是每隔多久进行一次内存清除!!



//调用

 

procedure TfmMain.Timer1Timer(Sender: TObject);

begin

   TClearMemory.Create(False);

end;

 



ClearMemoryThread单元代码如下:

unit ClearMemoryThread; interface uses Classes, SysUtils, Forms, Windows; type TClearMemory = class(TThread) private protected procedure Execute; override; public constructor Create(Suspended: Boolean); end; implementation //清理内存 procedure ClearMemory; begin if Win32Platform = VER_PLATFORM_WIN32_NT then begin SetProcessWorkingSetSize(GetCurrentProcess, $FFFFFFFF, $FFFFFFFF); Application.ProcessMessages; end; end; //创建线程 constructor TClearMemory.Create(Suspended: Boolean); begin inherited Create(Suspended); FreeOnTerminate := True; {这可以让线程执行完毕后随即释放} end; //线程启动 procedure TClearMemory.Execute; begin ClearMemory; end; end. 

你可能感兴趣的:(delphi 内存管理,定期释放)