.NET Array Dictionary List String 2D Async DataTable Dates DateTime Enum File For Foreach Format IEnumerable If IndexOf Lambda LINQ Parse Path Process Property Regex Replace Sort Split Static StringBuilder Substring Switch Tuple

C#: StringBuilder
Letters of the alphabet: ABC

StringBuilder stores character data in memory. It can reduce the complexity of your program's object model. Instead of storing strings separately, we can store them together. This improves performance.

Memory usage results for StringBuilder

List:          4.7 MB
StringBuilder: 4.0 MB


Example. First, this program compares a List that stores many instances of strings to a StringBuilder that contains that same string data. The List retains the object model and keeps all strings on the managed heap throughout its existence.

The StringBuilder combines the strings into a single object, reducing the workload of the garbage collector. This shows the memory savings that can be attained by changing the representation of your data models.

C# program that tests StringBuilder memory usage

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

class Program
    static StringBuilder _builder;
    static List<string> _list;

    static void Main()
	// Get start memory.
	long bytes1 = GC.GetTotalMemory(true);
	// Allocate a list or StringBuilder of many strings.
	// ... Comment out one of these lines to one method.
	_list = GetList();
	_builder = GetBuilder();
	// Compute memory usage.
	long bytes2 = GC.GetTotalMemory(true);
	Console.WriteLine(bytes2 - bytes1);

    static List<string> GetList() // Allocate list of strings
	List<string> list = new List<string>();
	for (int i = 0; i < 100000; i++)
	    string value = Path.GetRandomFileName();
	return list;

    static StringBuilder GetBuilder() // Allocate StringBuilder of strings
	StringBuilder builder = new StringBuilder();
	for (int i = 0; i < 100000; i++)
	    string value = Path.GetRandomFileName();
	return builder;
Main method

The program defines two important methods. GetList generates a List containing one hundred thousand random strings. And GetBuilder generates a StringBuilder containing one hundred thousand random strings concatenated.

Tip: The program should be modified to only run one of the two methods. This will improve its results.


GetList generates a large list of string instances. All of the strings in the List will be allocated separately on the managed heap. The garbage collector must keep track of the 100,000 strings as well as the List's internal buffer.

Next: The GetBuilder method allocates a StringBuilder instance and appends 100,000 strings to a single buffer inside the StringBuilder.

Internal layout. Because of the StringBuilder's internal representation as an array, each of the string instances will not need to exist. They will have no roots accessible in the managed heap. The garbage collector will free them.

Therefore: The StringBuilder version of the program saves the trouble of managing 100,000 strings.

Framework: NET

Memory. We note the results of executing this program on the .NET Framework and a 32-bit operating system kernel. When the program uses the GetList method only, it allocates 4,924,452 bytes of memory on the managed heap.

And: When the program uses the GetStringBuilder method only, it allocates 4,194,456 bytes on the heap.

Thus: The StringBuilder representation of the data saves 729,996 bytes or 713 KB.

Programming tip

Capacity. The capacity value can be passed as the integer argument to the StringBuilder constructor. ToString will be forced to copy the buffer again if it determines the buffer is much too large. Extra-large capacities will reduce performance.

StringBuilder Capacity Test

Summary. We saw an example of the StringBuilder type and its allocation of memory. The efficient representation of data in a StringBuilder can greatly reduce memory usage for certain programs. It reduces the complexity of the object model.