内存整理的原理是??
解决方案 »
- 请问如何改鼠标在DBGRIDEH某列上的形状?
- 游戏,让我很罪恶感
- delphi7.0运行常报错
- 如何开机运行程序而屏蔽其它程序(桌面)
- 如何使用Treeview控件
- 怎么样在sqlerver中update一个图片字段,对应的delphi的代码怎么写 ?
- 用户按下回车键,即执行"确定"按钮的click事件,该怎么做?
- 请问那儿有delphi 6.0 的update2.0可下载
- 关于控件中使用hook的问题,(chechy,zswang,windindance你们快来呀看看呀)......
- 李维系统篇第二章MdDemo打不开(在windows2000下),出现如下提示:
- 请问一个简单问题,datasource属性设置
- 还有多少人是搭积木式开发的?
// Jazarsoft VCL Pack (JVP) //
////////////////////////////////////////////////////////////////////////////////
// //
// TITLE : MaxMem //
// CLASS : TjsMaxMem //
// VERSION : 1.2 //
// AUTHOR : Jazarsoft VCL Development Team //
// CREATED : 20 October 2000 //
// MODIFIED : 23 December 2001 //
// WEBSITE : http://www.jazarsoft.com/ //
// WEB-SUPPORT : http://www.jazarsoft.com/vcl/forum/ //
// EMAIL-SUPPORT: [email protected], [email protected] //
// LEGAL : Copyright (C) 2000-2001 Jazarsoft, All Rights Reserved. //
// //
////////////////////////////////////////////////////////////////////////////////
// //
// This code may be used and modified by anyone so long as this header and //
// copyright information remains intact. //
// //
// The code is provided "AS-IS" and without WARRANTY OF ANY KIND, //
// expressed, implied or otherwise, including and without limitation, any //
// warranty of merchantability or fitness for a particular purpose.? //
// //
// In no event shall the author be liable for any special, incidental, //
// indirect or consequential damages whatsoever (including, without //
// limitation, damages for loss of profits, business interruption, loss //
// of information, or any other loss), whether or not advised of the //
// possibility of damage, and on any theory of liability, arising out of //
// or in connection with the use or inability to use this software.牋 //
// //
////////////////////////////////////////////////////////////////////////////////
// //
// HISTORY: //
// 1.0 - [o] Initial Development //
// 1.1 - [+] Abort Procedure //
// - [+] DefragAll Procedure //
// 1.2 - [o] Change classname to TjsMaxMem //
// //
// LEGEND: //
// [o] : Information //
// [+] : Add something //
// [-] : Remove something //
// [*] : Fix //
// //
////////////////////////////////////////////////////////////////////////////////{$WARNINGS OFF}
{$HINTS OFF}
unit jsMaxMem;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type
TOnProgress = procedure(Sender: TObject;Progress:Integer) of object; TjsMaxMem = class(TComponent)
private
FAbort : Boolean;
FTotalMemory : Integer; { Your Computer RAM in Bytes }
FWorking : Boolean;
FOnProgress : TOnProgress;
FOnBeforeDefrag ,
FOnAfterDefrag : TNotifyEvent;
Function GetFreeMemory:Integer; { In Bytes }
protected
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure DefragMemory(MemoryLimit:Integer); { Watchout! In MEGABYTES }
Procedure Abort;
Procedure DefragAll;
published
property TotalMemory : Integer Read FTotalMemory;
property FreeMemory : Integer Read GetFreeMemory; { Events }
property OnBeforeDefrag : tNotifyEvent Read FOnBeforeDefrag Write FOnBeforeDefrag;
property OnProgress : TOnProgress Read FOnProgress Write FOnProgress;
property OnAfterDefrag : tNotifyEvent Read FOnAfterDefrag Write FOnAfterDefrag;
end;procedure Register;implementationconstructor TjsMaxMem.Create(AOwner: TComponent);
var MS : tMemoryStatus;
Begin
inherited Create(AOwner);
MS.dwLength:=sizeof(MS);
GlobalMemoryStatus(MS);
FTotalMemory:=MS.dwTotalPhys;
End;destructor TjsMaxMem.Destroy;
Begin
inherited Destroy;
End;Procedure TjsMaxMem.Abort;
Begin
FAbort:=True;
End;
Procedure TjsMaxMem.DefragAll;
Begin
DefragMemory(TotalMemory div (1024*1024));
End;Function TjsMaxMem.GetFreeMemory:Integer;
var MS : tMemoryStatus;
Begin
MS.dwLength:=sizeof(MS);
GlobalMemoryStatus(MS);
Result:=MS.dwAvailPhys;
End;procedure TjsMaxMem.DefragMemory(MemoryLimit:Integer);
var
Pointers : Array [0..1024] of Pointer;
Limit ,
I2,
I : Integer;
P : Pointer;
Step : Integer;
Steps : Integer;
begin
FAbort := False;
If FWorking then Exit;
FWorking:=True;
Limit:=MemoryLimit;
If Limit>1024 then Limit:=1024;
If Assigned(FOnBeforeDefrag) then FOnBeforeDefrag(Self);
{ Calculating how many steps...}
Steps:=(MemoryLimit*2); Step:=0; { Clean pointer...}
For I:= 0 to Limit do Pointers[I]:=nil; { Allocating Memory }
For I:=0 to Limit-1 do
Begin
P:=VirtualAlloc(nil, 1024*1024, MEM_COMMIT, PAGE_READWRITE + PAGE_NOCACHE);
Pointers[I]:=p;
asm
pushad
pushfd
mov edi, p
mov ecx, 1024*1024/4
xor eax, eax
cld
repz stosd
popfd
popad
end;
Inc(Step);
If Assigned(FOnProgress) then OnProgress(Self,Round((Step/Steps)*100));
If FAbort then
Begin
For I2:=0 to I do
Begin
VirtualFree(Pointers[I2], 0, MEM_RELEASE);
End;
Step:=(MemoryLimit*2);
FWorking:=False;
If Assigned(FOnAfterDefrag) then FOnAfterDefrag(Self);
Exit;
End;
end; { DeAllocating Memory } For I:=0 to Limit-1 do
Begin
VirtualFree(Pointers[i], 0, MEM_RELEASE);
Inc(Step);
If Assigned(FOnProgress) then OnProgress(Self,Round((Step/Steps)*100));
If FAbort then
Begin
{ Warning! : Force abort, w/o de-allocating memory }
Step:=(MemoryLimit*2);
FWorking:=False;
If Assigned(FOnAfterDefrag) then FOnAfterDefrag(Self);
Exit;
End;
End; FWorking:=False; If Assigned(FOnAfterDefrag) then FOnAfterDefrag(Self);
End;
procedure Register;
begin
RegisterComponents('Jazarsoft', [TjsMaxMem]);
end;end.
然后清零以便操作系统把这些内存分配到实际内存中
然后再释放所有分配的内存我想问,为什么这样能起到内存整理?