使用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