StringBuilder for string concatenation throws OutO

2019-01-15 07:05发布

We mostly tend to following the above best practice.

Have a look at String vs StringBuilder

But StringBuilder could throw OutOfMemoryException even when there is sufficient memory available. It throws OOM exception because it needs "continuous block of memory".

Some links for reference StringBuilder OutOfMemoryException

and there are many more.....

How many of you faced this problem or aware and what did you do to resolve it?

Is there anything I am missing?

P.S: I wasn't aware of this.

I have rephrased the question.

*** The same thing worked with manual concatenation(I'll verify this and update SO). The other thing that caused me concern was that there is enough memory in the system. That's the reason I raised this question here to check whether any one faced this problem or there was something drastically wrong with the code.

8条回答
我想做一个坏孩纸
2楼-- · 2019-01-15 07:22

If StringBuilder is going to throw an OutOfMemoryException in your particular situation, then doing manual string concatenation is NOT a better solution; it's much worse. This is exactly the case (creating an extremely, extremely long string) where StringBuilder is supposed to be used. Manual concatenation of a string this large will take many times the memory that creation of a string with StringBuilder would take.

That said, on a modern computer, if your string is running the computer out of contiguous memory your design is deeply, deeply flawed. I can't imagine what you could possibly doing that would create a string that large.

查看更多
Fickle 薄情
3楼-- · 2019-01-15 07:24

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,

查看更多
做自己的国王
4楼-- · 2019-01-15 07:28

How much memory are we talking about? I'm not talking about free or total memory in the system, but how long is the string you're concatenating?

A memory overflow exception is almost always a very bad sign about your code, even if it fails long before the memory actually runs out, like you've experienced due to continous memory not being available.

At that point you should really restructure the code.

For instance, here are various ways to combat the problem:

  1. Do not keep as much data in memory at one time, put it on disk or something
  2. Break it up, keep a list of string/stringbuilders and only add to them up to a certain length before switching to a new one, keeps the "continous memory" problem at bay
  3. Restructure the algorithm to not build up gigabyte of data in memory at all
查看更多
何必那么认真
5楼-- · 2019-01-15 07:29

Well, the question actually is, why do you need to work with strings that long? If you stumble upon this problem, more than likely you should alter your concept.

This problems affects even the System.String class, so you should rather chunk your input into List< string> and process the data in parallel, which should increase overall performance if written properly.

查看更多
甜甜的少女心
6楼-- · 2019-01-15 07:33

I encountered this exception with very large strings built sucessively with different stringbuilders (which should not have caused a problem as they were declared within anonymous functions), and finally solved it by reusing a single StringBuilder, declared outside of the anonymous function.

查看更多
等我变得足够好
7楼-- · 2019-01-15 07:34

I had a very similar experience where I was appending strings but forgot to add the String.Format. Thus:

myStringBuilder.Append("1,""{0}""", someVeryLargeIntVariable)

should have been:

myStringBuilder.Append(String.Format("1,""{0}""", someVeryLargeIntVariable))

Note that this is my vb.net code that failed. I replicated a similar test in c# with:

myStringBuilder.Append('a', 1564544656);

vs.

myStringBuilder.Append(string.Format("1,\"{0}\"", 1564544656));

But in my case, vb.net got me in trouble b/c of the implicit conversions (I couldn't parallel the exact same problem in c#).

I hope that helps someone.

查看更多
登录 后发表回答