TheDeveloperBlog.com

Home | Contact Us

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

C# String Memory

This C# example program determines the memory usage of the string type. Each character requires 2 bytes.

String memory. String, a class, has some memory overhead.

This is an implementation-specific detail of the String class. Studying the memory usage of strings provides insights into the object model—and the efficiency of character data.

Note: Each string object required a constant 20 bytes for the object data. And the buffer requires 2 bytes per character.

Therefore: The memory usage estimate for a string instance is 20 + (2 * Length) bytes.

Example. We run a simulation where a varying number of strings of a certain length are allocated and placed in an array. The length of the array is changed to ensure the numbers remain constant for each individual string of a specific length.

String Array

The program is based on a 32-bit Windows operating system, and is run in Release mode outside of the debugger. The results were that each string occupies 20 bytes of overhead and then 2 bytes for each character in its buffer.

C# program that measures memory usage of strings

using System;

class Program
{
    static void Main()
    {
	// Loop through these sizes of string sets.
	// ... These are array lengths of string references.
	int[] sizes = { 0, 1, 100, 1000, 10000 };
	foreach (int size in sizes)
	{
	    // Allocate the array of references.
	    string[] array = new string[size];
	    long b1 = GC.GetTotalMemory(true);
	    // Each string is a fixed length.
	    for (int i = 0; i < array.Length; i++)
	    {
		array[i] = new string('a', 10);
	    }
	    long b2 = GC.GetTotalMemory(true);
	    // Ensure the array is not optimized out of the program.
	    if (array.Length > 0)
	    {
		array[0] = null;
	    }
	    // Write out statistics.
	    Console.WriteLine("Count: {0} Memory: {1} Chars: {2}",
		size,
		b2 - b1,
		10 * size);
	}
	Console.ReadLine();
    }
}

Results: length 10

Count: 0 Memory: 0 Chars: 0
Count: 1 Memory: 40 Chars: 10
Count: 100 Memory: 4000 Chars: 1000
Count: 1000 Memory: 40012 Chars: 10000
Count: 10000 Memory: 400012 Chars: 100000

Results: length 20

Count: 0 Memory: 0 Chars: 0
Count: 1 Memory: 60 Chars: 20
Count: 100 Memory: 6000 Chars: 2000
Count: 1000 Memory: 60000 Chars: 20000
Count: 10000 Memory: 600012 Chars: 200000

With each string having ten characters, one string will require 40 bytes, and 100 strings will require 4000 bytes. Thus, each string of ten chars is equal to 40 bytes. With each string having 20 chars, one string will require 60 bytes.

Therefore: Each character occupies two bytes and the string object data overhead itself is 20 bytes on this 32-bit Windows system.

Info: This program uses the GC.GetTotalMemory method to acquire the memory usage of the managed heap.

The program displays the number of string objects, the memory in bytes, and the total number of characters in the string objects. The program was run a second time with the length set to 20 to get more data with varying lengths.

StringBuilder. The StringBuilder type in the .NET Framework is parallel to the string type but has different allocation behavior. It can use less memory. This is because it does not store separate object data if you combine many strings.

StringBuilder Memory

Tip: You can sometimes use StringBuilder to transform many strings into a single string, allowing for fewer objects on the managed heap.

Summary. String memory can be predicted. This is in .NET 3.5 and the 32-bit Windows operating system. The string type occupied a constant 20 bytes of overhead for each object, with an additional two bytes per character in its buffer.

Also: Strings are objects. Each one will be separately managed by the garbage collector.


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