I have a large string (e.g. 20 MB).
Now I am parsing this line. The problem is that strings in C # are immutable; this means that as soon as I created a substring and looked at it, the memory was gone.
Because of all the processing, the memory is clogged with String objects that I no longer use, need or reference; but the garbage collector is too long to free them.
Thus, the application runs out of memory.
I could use a badly acting club approach and sprinkle several thousand calls:
GC.Collect();
but this does not solve the problem.
I know StringBuilder exists when creating a large string.
I know that TextReader exists to read a String into a char array.
I need to somehow "reuse" the string, which makes it more unchanged, so I do not unnecessarily allocate gigabytes of memory when 1k is executed.
Ian boyd
source share