unit Unit1;

interface

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

type 
  TForm1 =class (TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    Memo2: TMemo;
    procedure Button1Click (Sender: TObject);
    procedure Button2Click (Sender: TObject);
  end ;

var
  Form1: TForm1;

implementation

{$ R * .dfm}

// Test the memory usage of the heap
procedure TForm1.Button1Click (Sender: TObject);
var
  MyHeap: THandle;
  mem: TMemoryStatus;
  m1, m2, m3: DWORD;
begin
   {Get the current memory available state}
  GlobalMemoryStatus (mem);
  m1: = mem.dwAvailPhys;      {Available physical memory}
  m2: = mem.dwAvailPageFile; {Available virtual memory}
  m3: = mem.dwAvailVirtual;   {available virtual address}

  {Create private heap}
  MyHeap: = HeapCreate (HEAP_NO_SERIALIZE, 1024*1204, 0); {Build 1 M size heap}

  {Fetch the current available memory status}
  GlobalMemoryStatus (mem);
  m1: = m1-mem.dwAvailPhys;
  m2: = m2-mem.dwAvailPageFile;
  m3: = m3-mem.dwAvailVirtual;

  {Show the memory usage of the heap}
  Memo1.Clear;
  Memo1.Lines.Add (Format ('Use of physical memory:% d K', [m1 div 1024]));
  Memo1.Lines.Add (Format ('Use of virtual memory:% d K', [m2 div 1024]));
  Memo1.Lines.Add (Format ('Occupy virtual address:% d K', [m3 div 1024]));

  {Release heap}
  HeapDestroy (MyHeap);
end ;

// Same as the previous process, plus test page alignment when the heap is established
procedure TForm1.Button2Click (Sender: TObject);
var
  MyHeap: THandle;
  mem: TMemoryStatus;
  m1, m2, m3: DWORD;
begin
  GlobalMemoryStatus (mem);
  m1: = mem.dwAvailPhys;     
  m2: = mem.dwAvailPageFile; 
  m3: = mem.dwAvailVirtual;

  {Build a heap of 1M + 1 byte size, but the actual size will be page-aligned, the result should be: 1M + 4K}
  MyHeap: = HeapCreate (HEAP_NO_SERIALIZE, 1024*1204 + 1, 0);

  GlobalMemoryStatus (mem);
  m1: = m1-mem.dwAvailPhys;
  m2: = m2-mem.dwAvailPageFile;
  m3: = m3-mem.dwAvailVirtual;

  Memo2.Clear;
  Memo2.Lines.Add (Format ('Use of physical memory:% d K', [m1 div 1024]));
  Memo2.Lines.Add (Format ('Use of virtual memory:% d K', [m2 div 1024]));
  Memo2.Lines.Add (Format ('Occupy virtual address:% d K', [m3 div 1024]));

  HeapDestroy (MyHeap);
end ;

end .