The StringBuilder Object Memory Usage – Capacity

The StringBuilder class provides good performance for string operations with default attributes. And you can accomplish your string operations with in best memory performance adjusting StringBuilder Capcity property.

When you instantiate a new StringBuilder object, it’s capacity becomes 16 characters.

StringBuilder() sb= new StringBuilder();

And when you add some characters more then 16, it’s capacity raises 16, automatically. (32, 48 etc.)

If you are using some text with low amount of characters, you can instantiate a StringBuilder object with a capacity parameter.

StringBuilder() sb= new StringBuilder(6);

So the capacity of the object will become 6, 12, 18 etc. And the memory usage of the object becomes optimum for the case.

One thought on “The StringBuilder Object Memory Usage – Capacity

  1. Hi,

    I just wanted to add a note on the way .NET increases the memory backing the StringBuilder clause. It doesn’t add another multiple of 16. I mean: 16, 32, 48, 64, 80,…

    It doubles the amount of memory each time you exceed a StringBuilder’s capacity. This can have serious consequences as your file grows into the megabyte range.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s