I have this method that is expected to return a string but the string is pretty big maybe in GB. Currently this runs into out of memory exception.
I was thinking I would write to a file (random filename) then read it delete the file and send back the response. Tried MemoryStream
and StreamWriter
that too ran into out of memory exception.
Did not want to initialize StringBuilder
with a capacity since the size was not known and I think it needs a continuous block of memory.
What is the best course of action to solve this problem?
public string Result()
{
StringBuilder response = new StringBuilder();
for (int i = 1; i <= Int.Max; i++)
{
response.Append("keep adding some text {0}", i);
response.Append(Environment.NewLine);
}
return response.ToString();
}