Memory Leak in StringBuilder when using Insert() and Clear()

后端 未结 2 882
广开言路
广开言路 2020-12-11 07:51

I need to add some lines to a StringBuilder, where the line added last should be at the beginning of the string and the oldest at the end. I add a new line like this:

<
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-11 08:26

    We ran into the same issue with

    Replace()
    

    when we updated our web app from .net 2 to .net 4.

    Apparently the implementation of StringBuilder changed. See How the StringBuilder class is implemented? Does it internally create new string objects each time we append?

    The solutions we looked at:

    1. Do not reuse StringBuilder (that's what we decided to do after a big rewrite)
    2. Write our own implementation of SB
    3. Use .NET 2.0

提交回复
热议问题