Interesting OutOfMemoryException with StringBuilder

There is memory but no contiguous segment that can handle the size of your string builder. You have to know that each time the buffer of the string builder is too short, its size is doubled. If you can define (in the ctor) the size of your builder, it’s better.
You MAY call GC.Collect() when you are done with a large collection of objects.

Actually, when you have an OutOfMemory, it generaly shows a bad design, you may use the hard drive (temp files) instead of memory, you shouldn’t allocate memory again and again (try to reuse objects/buffers/…).

I STRONGLY advice you to read this post “Out Of Memory” Does Not Refer to Physical Memory from Eric Lippert.

Leave a Comment