How does StringBuilder's capacity change?
Asked Answered
N

3

5

When I have an empty StringBuilder with a capacity of 5 and I write "hello, world!" to it, does the C# standard specify the new capacity of the StringBuilder? I have a vague memory that it's twice the new string's length (to avoid changing the capacity with every new appended string).

Neogene answered 25/9, 2011 at 18:59 Comment(6)
this is not specified in any standard, its just an implementation detailGod
StringBuilder is not part of the C# language. It is part of the .net libraries.Cryptanalysis
possible duplicate of How does the StringBuilder decide how large its capacity should be?Thundercloud
What makes you curious about this implementation detail?Babblement
@Jason: The "curious" in your question is the answer.Neogene
@Vlad Vivdovitch: Fair enough.Babblement
B
16

Depends what version of .NET you're talking about. Prior to .NET 4, StringBuilder used the standard .NET strategy, doubling the capacity of the internal buffer every time it needs to be enlarged.

StringBuilder was completely rewritten for .NET 4, now using ropes. Extending the allocation is now done by adding another piece of rope of up to 8000 chars. Not quite as efficient as the earlier strategy but avoids trouble with big buffers clogging up the Large Object Heap. Source code is available from the Reference Source if you want to take a closer look.

Bithynia answered 25/9, 2011 at 19:47 Comment(2)
It messed up for us when we upgraded our product from .Net 3.5 to .Net 4.5. We were using string builder object as parameter to a native API call through interop. It resulted in buffer overrun issue causing the process to crash. Heap memory was getting corrupted when native API was returning long strings.We started to provide explicit capacity in constructor so that buffer can take care of long strings.Suprisingly it should have been breaking earlier also for long strings but never got reported.Does Interop layer does some trick in .Net 3.5 when we use string builder as buffer for native APIs?Infante
That corrupts the GC heap in either runtime version. Such corruption is not necessarily detected, you have to be lucky. Sure, you tend to get more lucky in .NET 4Bithynia
R
5

The C# standard will not specify the behavior of a BCL library class as it has nothing to do with the language specification.

As far as I know the actual behavior is not defined in any specification and is implementation specific.

AFAIK, The MS implementation will double the capacity once the current capacity has been reached.

See this and this previous SO questions.


Update:

This has been changed in .NET 4.0. as described by Hans in his answer. Now ropes are used, adding additional 8000 characters at a time.

MSDN, however is very careful to point out that the actual behavior is implementation specific:

The StringBuilder dynamically allocates more space when required and increases Capacity accordingly. For performance reasons, a StringBuilder might allocate more memory than needed. The amount of memory allocated is implementation-specific.

Raman answered 25/9, 2011 at 19:2 Comment(2)
We no longer use the double-when-full strategy.Inculcate
@Eric - Thanks for correcting me. That and the answer from Hans gave me more detail.Raman
M
0

New StringBuilder (.NET 4.5 or higher) allocates an internal buffer m_ChunkChars requested by the capacity parameter:

public StringBuilder(int capacity) { ... m_ChunkChars = new char[capacity]; ... }

So, if capacity is smaller than 40K chars it goes on the Small Object Heap. However (contrary to popular belief), StringBuilder will still allocate on the Large Object Heap if, later, we call sb.Append(...some string larger than 40K chars...); A possible fix can be found here: https://github.com/amikunov/Large-Object-Heap-Fix-For-.NET-String-Builder

Myrtie answered 18/5, 2017 at 20:46 Comment(1)
This has nothing at all to do with what the question is asking.Kruter

© 2022 - 2024 — McMap. All rights reserved.