I'm loading a lot of data from files, so I was testing the difference between TList and TArray memory consumption. And it seems TList uses a lot more memory.
This is my simple test:
procedure TForm8.Button1Click(Sender: TObject);
type
TDataRec = record
ID: integer;
Name: string;
end;
var vDataRec: TDataRec;
vDataList: TList<TDataRec>;
vArray: TArray<TDataRec>;
i, vMax: integer;
begin
vMax := 10000000;
vDataList := TList<TDataRec>.Create;
try
for i := 1 to vMax do
begin
vDataRec := Default(TDataRec);
vDataRec.ID := i;
vDataRec.Name := 'X';
vDataList.Add(vDataRec);
end;
finally
vDataList.Free;
end;
SetLength(vArray, vMax);
for i := 0 to vMax-1 do
begin
vArray[i].ID := i;
vArray[i].Name := 'X';
end;
vArray := nil;
end;
And here are results as I see them in Task Manager in Memory (active private working set) column - I believe this is the one that can be monitored when/if Out of memory error occurs.
TList<TDataRec>:
Starting memory: 3,716 K
Filled list: 369,488 K
Consumption: 365,772 K
TArray<TDataRec>:
Starting memory: 5,548 K
Filled list: 316,588 K
Consumption: 311,040 K
TList<TDataRec> vs TArray<TDataRec> = 54,732 K = 17%
17% is not extreme, but it is significant difference.
But! If the TDataRec only has 1 integer:
TDataRec = record
ID: integer;
end;
Now the difference is unacceptable:
TList<TDataRec>:
Starting memory: 3,720 K
Filled list: 69,700 K
Consumption: 65,980 K
TArray<TDataRec>:
Starting memory: 4,208 K
Filled list: 43,272 K
Consumption: 39,064 K
TList<TDataRec> vs TArray<TDataRec> = 26,916 K = 68%
68% seems extreme difference!
Is this normal, or am I missing something very obvious?