I have written following function
public void TestSB()
{
string str = "The quick brown fox jumps over the lazy dog.";
StringBuilder sb = new StringBuilder();
int j = 0;
int len = 0;
try
{
for (int i = 0; i < (10000000 * 2); i++)
{
j = i;
len = sb.Length;
sb.Append(str);
}
Console.WriteLine("Success ::" + sb.Length.ToString());
}
catch (Exception ex)
{
Console.WriteLine(
ex.Message + " :: " + j.ToString() + " :: " + len.ToString());
}
}
Now I suppose, that StringBuilder has the capacity to take over 2 billion character (2,147,483,647 to be precise).
But when I ran the above function it gave System.OutOfMemoryException
just on reaching the capacity of about 800 million.
Moreover, I am seeing widely different result on different PC having same memory and similar amount of load.
Can anyone please provide or explain me the reason for this?
StringBuilder sb = new StringBuilder(10000000 * 1);
Using a(n initial) capacity is always a good idea with big collections. – Megargee