使用TList
作为记录容器。申请时,TList
增加和删除大量的记录。但在delete
之后,属性capacity
决不会减少,并且内存不会释放。如何解决这个问题? 简单化的代码示例:在删除TList <>项目后如何正确释放内存?
type
TMyRecord = record
Num : integer;
Str : String
end;
var
MyRecord : TMyRecord;
MyList :TList<TMyRecord>;
MyList := TList<TMyRecord>.Create;
MyRecord.Num := 1;
MyRecord.Str := 'abc';
for i := 0 to 63 do
begin
MyList.Add(MyRecord);
end;
Memo1.Lines.Add('Before deleting');
Memo1.Lines.Add('Count='+IntToStr(MyList.Count));
Memo1.Lines.Add('Capacity='+IntToStr(MyList.Capacity));
for i := 0 to 59 do
begin
MyList.Delete(0);
end;
MyList.Pack; // Here need to somehow free the memory.
Memo1.Lines.Add('After deleting');
Memo1.Lines.Add('Count='+IntToStr(MyList.Count));
Memo1.Lines.Add('Capacity='+IntToStr(MyList.Capacity));
这正是我所需要的。谢谢! – HeathRow