内存管理六

本例效果图:


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}
//测试堆的内存占用情况
procedure TForm1.Button1Click(Sender: TObject);
var
MyHeap: THandle;
mem: TMemoryStatus;
m1,m2,m3: DWORD;
begin
{获取当前内存可用状态}
GlobalMemoryStatus(mem);
m1 := mem.dwAvailPhys;     {可用物理内存}
m2 := mem.dwAvailPageFile; {可用虚拟内存}
m3 := mem.dwAvailVirtual;  {可用虚拟地址}
{建立私有堆}
MyHeap := HeapCreate(HEAP_NO_SERIALIZE, 1024*1204, 0); {建立 1 M 大小的堆}
{再获取当前内存可用状态}
GlobalMemoryStatus(mem);
m1 := m1 - mem.dwAvailPhys;
m2 := m2 - mem.dwAvailPageFile;
m3 := m3 - mem.dwAvailVirtual;
{显示堆占用内存的情况}
Memo1.Clear;
Memo1.Lines.Add(Format('占用物理内存: %d K', [m1 div 1024]));
Memo1.Lines.Add(Format('占用虚拟内存: %d K', [m2 div 1024]));
Memo1.Lines.Add(Format('占用虚拟地址: %d K', [m3 div 1024]));
{释放堆}
HeapDestroy(MyHeap);
end;
//同上个过程, 外加测试建立堆时页对齐的情况
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;
{建立 1M + 1字节 大小的堆, 但实际大小会按页对齐, 结果应该是: 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('占用物理内存: %d K', [m1 div 1024]));
Memo2.Lines.Add(Format('占用虚拟内存: %d K', [m2 div 1024]));
Memo2.Lines.Add(Format('占用虚拟地址: %d K', [m3 div 1024]));
HeapDestroy(MyHeap);
end;
end.
posted @ 2010-06-17 16:43  delphi中间件  阅读(265)  评论(0编辑  收藏  举报