TStringList of objects taking up tons of memory in Delphi XE

后端 未结 10 1494
遇见更好的自我
遇见更好的自我 2021-01-05 14:50

I\'m working on a simulation program.

One of the first things the program does is read in a huge file (28 mb, about 79\'000 lines,), parse each line (about 150 field

10条回答
  •  没有蜡笔的小新
    2021-01-05 15:30

    In addition to Andreas' post:

    Before Delphi 2009, a string header occupied 8 bytes. Starting with Delphi 2009, a string header takes 12 bytes. So every unique string uses 4 bytes more than before, + the fact that each character takes twice the memory.

    Also, starting with Delphi 2010 I believe, TObject started using 8 bytes instead of 4. So for each single object created by delphi, delphi now uses 4 more bytes. Those 4 bytes were added to support the TMonitor class I believe.

    If you're in desperate need to save memory, here's a little trick that could help if you have a lot of string value that repeats themselve.

    var
      uUniqueStrings : TStringList;
    
    function ReduceStringMemory(const S : String) : string;
    var idx : Integer;
    begin
      if not uUniqueStrings.Find(S, idx) then
        idx := uUniqueStrings.Add(S);
    
      Result := uUniqueStrings[idx]
    end;
    

    Note that this will help ONLY if you have a lot of string values that repeat themselves. For exemple, this code use 150mb less on my system.

    var sl : TStringList;
      I: Integer;
    begin
      sl := TStringList.Create;
      try
        for I := 0 to 5000000 do
          sl.Add(ReduceStringMemory(StringOfChar('A',5)));every
      finally
        sl.Free;
      end;
    end;
    

提交回复
热议问题