
In your C# program, strings are stored with two bytes representing each character. If the strings are only ASCII, you can change them to be stored as single bytes; this reduces the memory usage by one byte per letter. This benchmark shows us how you can change string representations to be smaller.
The concept behind this benchmark is simple: it allocates an array of 10,000 strings. The memory this requires is measured. Then, another method (Compress) changes each string into a byte array. Then, the memory of this array is measured.
This C# example program represents strings with one byte per character.
Program that changes string representation [C#]
using System;
using System.IO;
using System.Text;
class Program
{
static void Main()
{
long a = GC.GetTotalMemory(true);
string[] array = Get();
long b = GC.GetTotalMemory(true);
array[0] = null;
long c = GC.GetTotalMemory(true);
byte[][] array2 = Compress(Get());
long d = GC.GetTotalMemory(true);
array2[0] = null;
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.WriteLine(d);
}
static string[] Get()
{
string[] output = new string[10000];
for (int i = 0; i < 10000; i++)
{
output[i] = Path.GetRandomFileName();
}
return output;
}
static byte[][] Compress(string[] array)
{
byte[][] output = new byte[array.Length][];
for (int i = 0; i < array.Length; i++)
{
output[i] = ASCIIEncoding.ASCII.GetBytes(array[i]);
}
return output;
}
}
Output
39128
479800
39784
320056
Results. In this program, the string[] required about 480,000 bytes. The byte[][] (a jagged array of byte arrays) required about 320,000 bytes. There was no data loss in these strings because the strings were ASCII-only.
GC.GetTotalMemory Usage Jagged Array Examples Convert String to Byte ArrayConverting back to strings. You can convert the byte arrays back into strings by calling ASCIIEncoding.ASCII.GetString. Please note this will have a performance and memory cost to create new strings.

Probably not. However, if you have a program that stores a huge number of strings that are very rarely actually needed, but must be stored in memory—and these strings are ASCII-only—this could be a useful optimization. There is an additional cost when you need to convert back into strings, however.

In this article, we looked at an optimization that can compress ASCII strings to use only one byte per character instead of two bytes. In some cases, this alternate representation could save a significant amount of memory.
Compression Tips