StringBuilder for string concatenation throws OutOfMemoryException

后端 未结 8 1670
南旧
南旧 2020-12-06 10:13

We mostly tend to following the above best practice.

Have a look at String vs StringBuilder

But StringBuilder could throw OutOfMemoryException even w

8条回答
  •  孤街浪徒
    2020-12-06 11:01

    If you look at how StringBuilder is implemented, you'll see that it actually uses a String to hold the data (String has internal methods, that will allow StringBuilder to modify in place).

    I.e. they both use the same amount of memory. However, since StringBuilder will automatically extend the underlying array and copy as necessary when needed (but doubling the capacity) that is most likely the cause of the out of memory error. But as others have already pointed out both of the require a continuous block of memory,

提交回复
热议问题