C# StringBuilder Memory

Determine the memory usage of the StringBuilder class. Compare StringBuilder to List.

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.StringBuilderOptimization

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.List
Info: The StringBuilder combines the strings into a single object, reducing the workload of the garbage collector.
Tip: This shows the memory savings that can be attained by changing the representation of your data models.
GetList: This method generates a List containing 100,000 random strings. All of the strings in the List will be allocated separately on the managed heap.
GetBuilder: This generates a StringBuilder containing 100,000 random strings concatenated.
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(); list.Add(value); } return list; } static StringBuilder GetBuilder() // Allocate StringBuilder of strings { StringBuilder builder = new StringBuilder(); for (int i = 0; i < 100000; i++) { string value = Path.GetRandomFileName(); builder.Append(value); } return builder; } } Output List: 4.7 MB StringBuilder: 4.0 MB

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.

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.

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

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.

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.