加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 04:00:51 所属栏目:大数据 来源:网络整理
导读:这是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
这是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

但是在一些修改过程中,我改变了逻辑,以便最后添加相同的记录,但是作为单独派生的字符串而不是通过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

我注意到的是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

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

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

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

无论是:

SetLength(SRecord,length(SRecord));

也不

Trim(SRecord);

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

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读