TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# StringBuilder Memory

This C# article shows how much memory StringBuilder uses. It compares 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.

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();
	    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;
    }
}

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.

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


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf