N
nisky
Unregistered / Unconfirmed
GUEST, unregistred user!
这是我在“深度历险”论坛看到的一个问题,有关内存被吃掉,代码如下,我这里列出的代码做了一些修改,但是问题依然存在,希望各位能解释一下为什么?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TOpp=Class(TPersistent)
public
Scale:array[1..10000]of Integer;
Constructor Create;virtual;
Destructor Destroy;override;
end;
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
Opp:TOpp;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TOpp }
constructor TOpp.Create;
begin
inherited Create;
end;
destructor TOpp.Destroy;
begin
inherited Destroy;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MS1:TMemoryStatus;
//i:Integer;
begin
//for i:=1 to 10 do
//begin
//Memo1.Lines.Add(Format('第%d次',));
MS1.dwLength:=SizeOf(TMemorystatus);
FillChar(MS1,SizeOf(TMemorystatus),0);
GlobalMemoryStatus(MS1);
Memo1.Lines.Add(Format('MS1-Free=%d',[MS1.dwAvailPhys]));
Opp:=TOpp.Create;
MS1.dwLength:=SizeOf(TMemorystatus);
FillChar(MS1,SizeOf(TMemorystatus),0);
GlobalMemoryStatus(MS1);
Memo1.Lines.Add(Format('MS1-Create=%d',[MS1.dwAvailPhys]));
Opp.Free;
Opp:=nil;
MS1.dwLength:=SizeOf(TMemorystatus);
FillChar(MS1,SizeOf(TMemorystatus),0);
GlobalMemoryStatus(MS1);
Memo1.Lines.Add(Format('MS1-Destory=%d',[MS1.dwAvailPhys]));
//end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
MS:TMemoryStatus;
begin
GlobalMemoryStatus(MS);
Memo1.Lines.Add(Format('Now=%d',[MS.dwAvailPhys]));
end;
end.
我的测试结果为:
MS1-Free=0
MS1-Create=0
MS1-Destory=32768
MS1-Free=45056
MS1-Create=0
MS1-Destory=32768
MS1-Free=45056
MS1-Create=0
MS1-Destory=32768
MS1-Free=45056
MS1-Create=0
MS1-Destory=32768
MS1-Free=45056
MS1-Create=0
MS1-Destory=32768
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TOpp=Class(TPersistent)
public
Scale:array[1..10000]of Integer;
Constructor Create;virtual;
Destructor Destroy;override;
end;
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
Opp:TOpp;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TOpp }
constructor TOpp.Create;
begin
inherited Create;
end;
destructor TOpp.Destroy;
begin
inherited Destroy;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
MS1:TMemoryStatus;
//i:Integer;
begin
//for i:=1 to 10 do
//begin
//Memo1.Lines.Add(Format('第%d次',));
MS1.dwLength:=SizeOf(TMemorystatus);
FillChar(MS1,SizeOf(TMemorystatus),0);
GlobalMemoryStatus(MS1);
Memo1.Lines.Add(Format('MS1-Free=%d',[MS1.dwAvailPhys]));
Opp:=TOpp.Create;
MS1.dwLength:=SizeOf(TMemorystatus);
FillChar(MS1,SizeOf(TMemorystatus),0);
GlobalMemoryStatus(MS1);
Memo1.Lines.Add(Format('MS1-Create=%d',[MS1.dwAvailPhys]));
Opp.Free;
Opp:=nil;
MS1.dwLength:=SizeOf(TMemorystatus);
FillChar(MS1,SizeOf(TMemorystatus),0);
GlobalMemoryStatus(MS1);
Memo1.Lines.Add(Format('MS1-Destory=%d',[MS1.dwAvailPhys]));
//end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
MS:TMemoryStatus;
begin
GlobalMemoryStatus(MS);
Memo1.Lines.Add(Format('Now=%d',[MS.dwAvailPhys]));
end;
end.
我的测试结果为:
MS1-Free=0
MS1-Create=0
MS1-Destory=32768
MS1-Free=45056
MS1-Create=0
MS1-Destory=32768
MS1-Free=45056
MS1-Create=0
MS1-Destory=32768
MS1-Free=45056
MS1-Create=0
MS1-Destory=32768
MS1-Free=45056
MS1-Create=0
MS1-Destory=32768