使用 TList
作为记录容器。在应用程序中,TList
添加和删除大量的记录。但是,在 delete
后,属性 capacity
永远不会减少,内存也没有被释放。如何解决这个问题?
简单的代码示例:
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));