www.pudn.com > RamOptimizationPack.zip > MainUnit.pas, change:2007-03-09,size:4920b


unit MainUnit; 
 
interface 
 
uses 
  Windows, SysUtils, Classes, Controls, Forms, 
  Dialogs, ShellApi, StdCtrls, janSimScope, psApi, ExtCtrls; 
 
type 
  TMainForm = class(TForm) 
    Scope: TjanSimScope; 
    ScopeGB: TGroupBox; 
    OptionsGB: TGroupBox; 
    OptimizeRAMUsageBtn: TButton; 
    BlogLinkLb: TLabel; 
    ScopeTimer: TTimer; 
    NoOptimizationRb: TRadioButton; 
    TimerOptimizationRb: TRadioButton; 
    IntelligentOptimizationRb: TRadioButton; 
    AllocateMemBtn: TButton; 
    DeallocateMemBtn: TButton; 
    AllocatedMemLb: TLabel; 
    RamOpimizationTimer: TTimer; 
    procedure BlogLinkLbClick(Sender: TObject); 
    procedure FormCreate(Sender: TObject); 
    procedure ScopeTimerTimer(Sender: TObject); 
    procedure AllocateMemBtnClick(Sender: TObject); 
    procedure OptimizeRAMUsageBtnClick(Sender: TObject); 
    procedure DeallocateMemBtnClick(Sender: TObject); 
    procedure IntelligentOptimizationRbClick(Sender: TObject); 
    procedure RamOpimizationTimerTimer(Sender: TObject); 
    procedure TimerOptimizationRbClick(Sender: TObject); 
    procedure NoOptimizationRbClick(Sender: TObject); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  end; 
 
var 
  MainForm: TMainForm; 
  Data : array of Byte; // here we will store our allocated memory 
  MinRamUsage : Integer; 
 
implementation 
 
{$R *.dfm} 
 
function GetCurrentProcessMemoryUsage : Integer; 
// This function checks how much memory our process currently use 
var 
  l_nTmpHandle: HWND; 
  l_pPMC: PPROCESS_MEMORY_COUNTERS; 
  l_pPMCSize: Cardinal; 
begin 
  l_pPMCSize := SizeOf(PROCESS_MEMORY_COUNTERS); 
  GetMem(l_pPMC, l_pPMCSize); 
  l_pPMC^.cb := l_pPMCSize; 
  l_nTmpHandle := GetCurrentProcess; 
  if (GetProcessMemoryInfo(l_nTmpHandle, l_pPMC, l_pPMCSize)) then 
    Result := l_pPMC^.WorkingSetSize 
  else 
    Result := 0; 
  FreeMem(l_pPMC); 
end; 
 
procedure OptimizeRamUsage; 
// This optimizes RAM memory usage 
begin 
  SetProcessWorkingSetSize(GetCurrentProcess, $ffffffff, $ffffffff); 
end; 
 
function GetMinimalRamUsage : Integer; 
// This function tells us how much memory our application can minimally use 
begin 
  OptimizeRamUsage; 
  Result:=GetCurrentProcessMemoryUsage; 
end; 
 
procedure TMainForm.BlogLinkLbClick(Sender: TObject); 
begin 
  ShellExecute(Handle, 'open', 'http://tomksoft.iblogger.org', '', '', 0); // This opens my blog in default webborwser 
end; 
 
procedure TMainForm.FormCreate(Sender: TObject); 
begin 
  Scope.Clear; // Let's clean the scope! It's so dirty... 
end; 
 
procedure TMainForm.ScopeTimerTimer(Sender: TObject); 
var RamUsage : Integer; 
begin 
  RamUsage:=GetCurrentProcessMemoryUsage; // Here, we set how much RAM does our appilcation use now 
  ScopeGB.Caption:='RAM usage: '+FloatToStr(Round(RamUsage/(1024*1024)*100)/100)+'MB'; // Let's set it on the label... 
  Scope.Position:=Round((RamUsage/(1024*1024))/(30)*100); // ... and on the scope 
end; 
 
procedure TMainForm.OptimizeRAMUsageBtnClick(Sender: TObject); 
begin 
  OptimizeRamUsage; 
end; 
 
procedure TMainForm.AllocateMemBtnClick(Sender: TObject); 
begin 
  // This procedure allocates 1MB of RAM 
  SetLength(Data, Length(Data)+1024*1024); 
  AllocatedMemLb.Caption:=IntToStr(Round(Length(Data)/(1024*1024)))+'MB'; 
  AllocateMemBtn.Enabled:=Round(Length(Data)/(1024*1024))<=24; 
  DeallocateMemBtn.Enabled:=Round(Length(Data)/(1024*1024))>0; 
end; 
 
procedure TMainForm.DeallocateMemBtnClick(Sender: TObject); 
begin 
  // This procedure deallocates 1MB of RAM 
  SetLength(Data, Length(Data)-1024*1024); 
  AllocatedMemLb.Caption:=IntToStr(Round(Length(Data)/(1024*1024)))+'MB'; 
  AllocateMemBtn.Enabled:=Round(Length(Data)/(1024*1024))<=25; 
  DeallocateMemBtn.Enabled:=Round(Length(Data)/(1024*1024))>0; 
end; 
 
procedure TMainForm.NoOptimizationRbClick(Sender: TObject); 
begin 
  RamOpimizationTimer.Enabled:=(TimerOptimizationRb.Checked) or (IntelligentOptimizationRb.Checked); 
end; 
 
procedure TMainForm.TimerOptimizationRbClick(Sender: TObject); 
begin 
  RamOpimizationTimer.Enabled:=(TimerOptimizationRb.Checked) or (IntelligentOptimizationRb.Checked); 
end; 
 
procedure TMainForm.IntelligentOptimizationRbClick(Sender: TObject); 
begin 
  MinRamUsage:=Round(0.75*1024*1024)+GetMinimalRamUsage; 
  MessageDlg('Minimal possible RAM usage: '+FloatToStr(Round(MinRamUsage / (1024*1024)*100)/100)+'MB', mtInformation, [mbOK], 0); 
  RamOpimizationTimer.Enabled:=(TimerOptimizationRb.Checked) or (IntelligentOptimizationRb.Checked); 
end; 
 
procedure TMainForm.RamOpimizationTimerTimer(Sender: TObject); 
begin 
  // This is timer procedure for timer and intelligent RAM usage optimization 
  if TimerOptimizationRb.Checked then 
  begin 
    OptimizeRamUsage; 
  end else 
  if IntelligentOptimizationRb.Checked then 
  begin 
    if GetCurrentProcessMemoryUsage>MinRamUsage*1.1 then 
      OptimizeRamUsage; 
  end; 
end; 
 
end.