我参阅了Borland的文档,说GetMem等函数底层都是基于VirtualAlloc实现的,于是就写了下
列程序来测试,发现在我的256M的机器上可以分配 474 M的内存,为什么,好象比2G少太多了吧?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
PBlock = ^TBlock;
TBlock = record
buf : array[0..1024 * 1024 - 1] of Byte
//1M
end;
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
L : TList;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
B : PBlock;
I : integer;
begin
try
L := TList.Create;
I := 0;
while TRUE do begin
GetMem(B,SizeOf(TBlock));
L.Add(B);
Inc(I);
Label1.Caption := '已经分配内存 ' + IntToStr(I) + ' 兆';
Label1.Refresh;
end;
except
for I := 0 to L.Count - 1 do begin
FreeMem(L.Items,SizeOf(TBlock));
end;
L.Free;
end;
end;
{ MemClass }
end.