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. `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 `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. `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 `stringbuilder-memory`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. `Strings are objects. Each one will be separately managed by the garbage collector.

OINHDNNIDIQIbIJIAN{NOI%O{DNOOI{IWthrough these sizes of IP sets.NOOI9These are IU lI^s of IP references.NOODIz[] sizesIy{ 0, 1, 100, 1000, 10000 };NOOI@ (IisizeIpsizes)NOO{DNOOOI{Allocate the IU of references.NOOODIP[] IUIyIqIP[size];NOOOlong b1IyGC.GetTotalMemory(true);DNOOOI{IZIP is a fixed lI^.NOOODIw(IiiIy0; i < IU.LI^; i++)NOOO{NOOOOIU[i]IyDIqIPD(D'a'D, 10);NOOO}NOOOlong b2IyGC.GetTotalMemory(true);DNOOOI{Ensure the IU is not optimized out of the program.NOOODImIU.LI^ > 0)NOOO{NOOOOIU[0]Iynull;NOOO}DNOOOI{Ia out statistics.NOOODI'HI]: {0} Memory: {1} Chars: {2}H,NOOOOsize,NOOOOb2 - b1,NOOOO10 * size);NOO}NOOI5.IuLine();NO}N}NNDResults: lI^ 10DNNI]: 0 Memory: 0 Chars: 0NI]: 1 Memory: 40 Chars: 10NI]: 100 Memory: 4000 Chars: 1000NI]: 1000 Memory: 40012 Chars: 10000NI]: 10000 Memory: 400012 Chars: 100000NNDResults: lI^ 20DNNI]: 0 Memory: 0 Chars: 0NI]: 1 Memory: 60 Chars: 20NI]: 100 Memory: 6000 Chars: 2000NI]: 1000 Memory: 60000 Chars: 20000NI]: 10000 Memory: 600012 Chars: 200000D

!@measures memory usage of strings