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();
}
Even if you could solve the memory problem on the StringBuilder
, the resulting string by calling response.ToString()
would be larger than the maximum length of a string in .NET, see: https://stackoverflow.com/a/140749/3997704
So you will have to make your function store its result elsewhere, like in a file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.