What is StringBuilder's RAM consumption like?

后端 未结 5 805
广开言路
广开言路 2021-01-12 15:39

We have a few operations where we are doing a large number of large string concatenations, and have recently encountered an out of memory exception. Unfortunately, debuggin

5条回答
  •  独厮守ぢ
    2021-01-12 16:24

    Each time StringBuilder runs out of space, it reallocates a new buffer twice the size of the original buffer, copies the old characters, and lets the old buffer get GC'd. It's possible that you're just using enough (call it x) such that 2x is larger than the memory you're allowed to allocate. You may want to determine a maximum length for your strings, and pass it to the constructor of StringBuilder so you preallocate, and you're not at the mercy of the doubling reallocation.

提交回复
热议问题