unit fmMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, uMyObject, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    btnAllocate: TButton;
    Button3: TButton;
    Button4: TButton;
    ListBox1: TListBox;
    lbBefore: TListBox;
    lbAfter: TListBox;
    btnFree: TButton;
    lbFree: TListBox;
    Button2: TButton;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure btnAllocateClick(Sender: TObject);
    procedure btnFreeClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  aList : TList = nil;

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  aObj : TMyObject;
  sData : String;
begin
  sData := '';
  aObj := TMyObject.Create;
  Self.Caption := 'TMyObject : ' + IntToStr(aObj.InstanceSize);
  try
    ListBox1.Items.Add(IntToStr(aObj.iValue));
    ListBox1.Items.Add(FloatToStr(aObj.fValue));
    ListBox1.Items.Add(aObj.sValue);

    for I := 1 to 10 do    // Iterate
    begin
      sData := sData + IntToStr(aObj.iArray[I]) + ':';
    end;    // for
    Delete(sData, Length(sData), 1);
    ListBox1.Items.Add(sData);
  finally // wrap up
    FreeAndNil(aObj);
  end;    // try/finally
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  hs : THeapStatus;
begin
  hs := GetHeapStatus;

  lbBefore.Items.Add('TotalAddrSpace : ' + IntToStr(hs.TotalAddrSpace));
  lbBefore.Items.Add('TotalUncommitted : ' + IntToStr(hs.TotalUncommitted));
  lbBefore.Items.Add('TotalCommitted : ' + IntToStr(hs.TotalCommitted));
  lbBefore.Items.Add('TotalAllocated : ' + IntToStr(hs.TotalAllocated));
  lbBefore.Items.Add('TotalFree : ' + IntToStr(hs.TotalFree));
  lbBefore.Items.Add('FreeSmall : ' + IntToStr(hs.FreeSmall));
  lbBefore.Items.Add('FreeBig : ' + IntToStr(hs.FreeBig));
  lbBefore.Items.Add('Unused : ' + IntToStr(hs.Unused));
  lbBefore.Items.Add('Overhead : ' + IntToStr(hs.Overhead));
  lbBefore.Items.Add('HeapErrorCode : ' + IntToStr(hs.HeapErrorCode));
end;

procedure TForm1.Button4Click(Sender: TObject);
var
  hs : THeapStatus;
begin
  hs := GetHeapStatus;

  lbAfter.Items.Add('TotalAddrSpace : ' + IntToStr(hs.TotalAddrSpace));
  lbAfter.Items.Add('TotalUncommitted : ' + IntToStr(hs.TotalUncommitted));
  lbAfter.Items.Add('TotalCommitted : ' + IntToStr(hs.TotalCommitted));
  lbAfter.Items.Add('TotalAllocated : ' + IntToStr(hs.TotalAllocated));
  lbAfter.Items.Add('TotalFree : ' + IntToStr(hs.TotalFree));
  lbAfter.Items.Add('FreeSmall : ' + IntToStr(hs.FreeSmall));
  lbAfter.Items.Add('FreeBig : ' + IntToStr(hs.FreeBig));
  lbAfter.Items.Add('Unused : ' + IntToStr(hs.Unused));
  lbAfter.Items.Add('Overhead : ' + IntToStr(hs.Overhead));
  lbAfter.Items.Add('HeapErrorCode : ' + IntToStr(hs.HeapErrorCode));
end;

procedure TForm1.btnAllocateClick(Sender: TObject);
var
  I: Integer;
  aObj : TMyObject;
begin
  if (not Assigned(aList)) then
    aList := TList.Create;

  Button3Click(Self);

  for I := 1 to 10000 do    // Iterate
  begin
    aObj := TMyObject.Create;
    aList.Add(aObj);
  end;    // for
  Button4Click(Self);
end;

procedure TForm1.btnFreeClick(Sender: TObject);
var
  aObj : TMyObject;
  I: Integer;
  hs : THeapStatus;
begin
  hs := GetHeapStatus;
  if Assigned(aList) then
  begin
    lbFree.Clear;
    for I := aList.Count - 1 downto 0 do    // Iterate
    begin
      aObj := TMyObject(aList[I]);
      FreeAndNil(aObj);
    end;    // for

    FreeAndNil(aList);
  end;
  lbFree.Items.Add('TotalAddrSpace : ' + IntToStr(hs.TotalAddrSpace));
  lbFree.Items.Add('TotalUncommitted : ' + IntToStr(hs.TotalUncommitted));
  lbFree.Items.Add('TotalCommitted : ' + IntToStr(hs.TotalCommitted));
  lbFree.Items.Add('TotalAllocated : ' + IntToStr(hs.TotalAllocated));
  lbFree.Items.Add('TotalFree : ' + IntToStr(hs.TotalFree));
  lbFree.Items.Add('FreeSmall : ' + IntToStr(hs.FreeSmall));
  lbFree.Items.Add('FreeBig : ' + IntToStr(hs.FreeBig));
  lbFree.Items.Add('Unused : ' + IntToStr(hs.Unused));
  lbFree.Items.Add('Overhead : ' + IntToStr(hs.Overhead));
  lbFree.Items.Add('HeapErrorCode : ' + IntToStr(hs.HeapErrorCode));
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  hs : THeapStatus;
begin
  hs := GetHeapStatus;
  lbFree.Items.Add('TotalAddrSpace : ' + IntToStr(hs.TotalAddrSpace));
  lbFree.Items.Add('TotalUncommitted : ' + IntToStr(hs.TotalUncommitted));
  lbFree.Items.Add('TotalCommitted : ' + IntToStr(hs.TotalCommitted));
  lbFree.Items.Add('TotalAllocated : ' + IntToStr(hs.TotalAllocated));
  lbFree.Items.Add('TotalFree : ' + IntToStr(hs.TotalFree));
  lbFree.Items.Add('FreeSmall : ' + IntToStr(hs.FreeSmall));
  lbFree.Items.Add('FreeBig : ' + IntToStr(hs.FreeBig));
  lbFree.Items.Add('Unused : ' + IntToStr(hs.Unused));
  lbFree.Items.Add('Overhead : ' + IntToStr(hs.Overhead));
  lbFree.Items.Add('HeapErrorCode : ' + IntToStr(hs.HeapErrorCode));
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  lbFree.Clear;
  Button2Click(Sender);
end;

end.
