开发者

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

开发者 https://www.devze.com 2023-04-02 17:13 出处:网络
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 wast开

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 wast开发者_如何学Goed.

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.


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.


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.


Do you have some sample code to test whether possible solutions would work well?

In general though, any object that is bigger than 85KB is going to be allocated onto the Large Object Heap, which will probably be garbage collected less often.

Also, if you're really pushing the CPU hard, the garbage collector will likely perform its work less often, trying to stay out of your way.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号