How to free or rework a string in C #?

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.

+7
source share
3 answers

If your application dies, it may be because you still have references to the strings - not because the garbage collector simply does not clear them. I have seen this fail, but it is unlikely. Did you use the profiler to verify that you actually have a lot of lines in memory at a time?

The long and short that you cannot reuse a string to store different data is simply not possible. You can write your own equivalent if you want - but the chances of doing it efficiently and correctly are pretty subtle. Now, if you can give more information about what you are doing, we can offer alternative approaches that do not use so much memory.

+11
source

Do you have sample code to check if possible solutions work well?

In general, any object that is larger than 85 KB in size will be placed on a Large pile of objects , which are likely to be garbage collected less frequently.

Also, if you really press the CPU hard, the garbage collector will likely do its job less often, trying to stay out of your way.

+1
source

I would suggest, given that you cannot reuse strings in C #, use Memory Files . You simply save the string to disk and process it with an excellent performance-memory ratio using a matching file such as a stream. In this case, you reuse the same file, the same stream, and work only on the smallest possible part of the data, such as the line that you need at this exact moment, and immediately throw it away.

This decision strictly depends on your requirements for the project, but I think that one of the solutions that you can seriously consider, as memory consumption will decrease significantly, but you will “pay” something in terms of performance.

+1
source

All Articles