Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to free, or recycle, a string in C#?

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.

like image 387
Ian Boyd Avatar asked Sep 06 '11 19:09

Ian Boyd


3 Answers

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.

like image 157
Jon Skeet Avatar answered Oct 31 '22 16:10

Jon Skeet


This question is almost 10 years old. These days, please look at ReadOnlySpan - instantiate one from the string using AsSpan() method. Then you can apply index operators to get slices as spans without allocating any new strings.

like image 3
Mr. TA Avatar answered Oct 31 '22 17:10

Mr. TA


I would suggest, considering the fact, that you can not reuse the strings in C#, use Memory-Mapped Files. You simply save string on a disk and process it with performance/memory-consuption excelent relationship via mapped file like a stream. In this case you reuse the same file, the same stream and operate only on small possible portion of the data like a string, that you need in that precise moment, and after immediately throw it away.

This solution is strictly depends on your project requieremnts, but I think one of the solutions you may seriously consider, as especially memory consumption will go down dramatically, but you will "pay" something in terms of performance.

like image 2
Tigran Avatar answered Oct 31 '22 17:10

Tigran