i have a large string (e.g. 20MB).
i am now parsing this string. The problem is that strings in C# are immutable; this means that once i’ve created a substring, and looked at it, the memory is wasted.
Because of all the processing, memory is getting clogged up with String objects that i no longer used, need or reference; but it takes the garbage collector too long to free them.
So the application runs out of memory.
i could use the poorly performing club approach, and sprinkle a few thousand calls to:
GC.Collect();
everywhere, but that’s not really solving the issue.
i know StringBuilder exists when creating a large string.
i know TextReader exists to read a String into a char array.
i need to somehow “reuse” a string, making it no longer immutable, so that i don’t needlessly allocate gigabytes of memory when 1k will do.
If your application is dying, that’s likely to be because you still have references to strings – not because the garbage collector is just failing to clean them up. I have seen it fail like that, but it’s pretty unlikely. Have you used a profiler to check that you really do have a lot of strings in memory at a time?
The long and the short of it is that you can’t reuse a string to store different data – it just can’t be done. You can write your own equivalent if you like – but the chances of doing that efficiently and correctly are pretty slim. Now if you could give more information about what you’re doing, we may be able to suggest alternative approaches that don’t use so much memory.