B
Bahl
Unregistered / Unconfirmed
GUEST, unregistred user!
{$WARNINGS OFF}
{$HINTS OFF}
unit MaxMem;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TOnProgress = procedure(Sender: TObject;Progress:Integer) of object;
TMaxMem = 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;
implementation
constructor TMaxMem.Create(AOwner: TComponent);
var MS : tMemoryStatus;
begin
inherited Create(AOwner);
MS.dwLength:=sizeof(MS);
GlobalMemoryStatus(MS);
FTotalMemory:=MS.dwTotalPhys;
end;
destructor TMaxMem.Destroy;
begin
inherited Destroy;
end;
Procedure TMaxMem.Abort;
begin
FAbort:=True;
end;
Procedure TMaxMem.DefragAll;
begin
DefragMemory(TotalMemory div (1024*1024));
end;
Function TMaxMem.GetFreeMemory:Integer;
var MS : tMemoryStatus;
begin
MS.dwLength:=sizeof(MS);
GlobalMemoryStatus(MS);
Result:=MS.dwAvailPhys;
end;
procedure TMaxMem.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:=nil;
{ Allocating Memory }
For I:=0 to Limit-1 do
begin
P:=VirtualAlloc(nil, 1024*1024, MEM_COMMIT, PAGE_READWRITE + PAGE_NOCACHE);
Pointers:=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, 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', [TMaxMem]);
end;
end.
这个组件的基本原理参考了"Memory Defragmenter for Windows" 这篇文章,
我过一阵子争取把它翻译并贴上来。
请帮忙将它提前。
{$HINTS OFF}
unit MaxMem;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TOnProgress = procedure(Sender: TObject;Progress:Integer) of object;
TMaxMem = 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;
implementation
constructor TMaxMem.Create(AOwner: TComponent);
var MS : tMemoryStatus;
begin
inherited Create(AOwner);
MS.dwLength:=sizeof(MS);
GlobalMemoryStatus(MS);
FTotalMemory:=MS.dwTotalPhys;
end;
destructor TMaxMem.Destroy;
begin
inherited Destroy;
end;
Procedure TMaxMem.Abort;
begin
FAbort:=True;
end;
Procedure TMaxMem.DefragAll;
begin
DefragMemory(TotalMemory div (1024*1024));
end;
Function TMaxMem.GetFreeMemory:Integer;
var MS : tMemoryStatus;
begin
MS.dwLength:=sizeof(MS);
GlobalMemoryStatus(MS);
Result:=MS.dwAvailPhys;
end;
procedure TMaxMem.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:=nil;
{ Allocating Memory }
For I:=0 to Limit-1 do
begin
P:=VirtualAlloc(nil, 1024*1024, MEM_COMMIT, PAGE_READWRITE + PAGE_NOCACHE);
Pointers:=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, 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', [TMaxMem]);
end;
end.
这个组件的基本原理参考了"Memory Defragmenter for Windows" 这篇文章,
我过一阵子争取把它翻译并贴上来。
请帮忙将它提前。