为什么SetString在Delphi中使用较少的内存(使用Unicode)?

lke*_*ler 8 delphi string unicode pchar setstring

这是Delphi 2009,因此Unicode适用.

我有一些代码将缓冲区中的字符串加载到StringList中,如下所示:

      var Buffer: TBytes; RecStart, RecEnd: PChar; S: string;

      FileStream.Read(Buffer[0], Size);

      repeat
         ... find next record RecStart and RecEnd that point into the buffer;        

         SetString(S, RecStart, RecEnd - RecStart);
         MyStringList.Add(S);
      until end of buffer
Run Code Online (Sandbox Code Playgroud)

但是在一些修改过程中,我改变了逻辑,以便最终添加相同的记录,但是作为单独派生的字符串而不是通过SetString,即

      var SRecord: string;

      repeat
        SRecord := '';
        repeat
          SRecord := SRecord + ... processed line from the buffer;
        until end of record in the buffer

        MyStringList.Add(SRecord);
      until end of buffer
Run Code Online (Sandbox Code Playgroud)

我注意到的是StringList的内存使用从52 MB上升到大约70 MB.这增加了30%以上.

为了回到我的较低的内存使用量,我发现我必须使用SetString来创建字符串变量以添加到我的StringList,如下所示:

      repeat
        SRecord := '';
        repeat
          SRecord := SRecord + ... processed line from the buffer;
        until end of record in the buffer

        SetString(S, PChar(SRecord), length(SRecord));
        MyStringList.Add(S);
      until end of buffer
Run Code Online (Sandbox Code Playgroud)

检查和比较S和SRecord,它们在所有情况下完全相同.但是将SRecord添加到MyStringList比使用S添加更多的内存.

有谁知道发生了什么以及为什么SetString可以节省内存?


跟进.我不认为会,但我检查确认.

无论是:

  SetLength(SRecord, length(SRecord));
Run Code Online (Sandbox Code Playgroud)

也不

  Trim(SRecord);
Run Code Online (Sandbox Code Playgroud)

释放多余的空间.似乎需要SetString这样做.

And*_*den 15

如果连接字符串,内存管理器将分配更多内存,因为它假定您向其添加越来越多的文本并为将来的连接分配额外的空间.这样,字符串的分配大小远大于使用的大小(取决于使用的内存管理器).如果使用SetString,则新字符串的分配大小与使用的大小几乎相同.当SRecord字符串超出范围并且其ref-count变为零时,SRecord占用的内存将被释放.因此,您最终会得到字符串所需的最小分配大小.

  • 将''分配给SRecord不会释放字符串,因为它的引用计数为2(@SRecord和@TStringList.FList [].FString).它只将RefCount递减为1(@ TStringList.FList [].FString)并将SRecord变量设置为null.因此,超大的并置字符串仍然存在并由StringList使用.您的代码会在每次迭代时分配一个新的SRecord字符串.对于不符合字符串分配大小的每个串联,对于小块,字符串增长至少100%+ 31字节,对于中间块,​​字符串增长25%,对于大块(FastMM),字符串增长25%到64KB. (3认同)