如何在 C# 中释放或回收字符串?

发布于 2024-12-03 14:54:36 字数 497 浏览 0 评论 0 原文

我有一个大字符串(例如20MB)。

我现在正在解析这个字符串。问题在于 C# 中的字符串是不可变的;这意味着一旦我创建了一个子字符串并查看了它,内存就被浪费了。

由于所有的处理,内存被我不再使用、需要或引用的 String 对象堵塞;但垃圾收集器需要很长时间才能释放它们。

因此应用程序内存不足。

我可以使用性能不佳的 club 方法,并在各处进行数千次调用:

GC.Collect();

但这并不能真正解决问题。

我知道创建大字符串时存在 StringBuilder

我知道 TextReader 的存在是为了将 String 读入 char 数组。

我需要以某种方式“重用”一个字符串,使其不再是不可变的,这样我就不会在 1k 就可以的情况下不必要地分配 GB 的内存。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风向决定发型 2024-12-10 14:54:36

如果您的应用程序即将崩溃,那可能是因为您仍然引用了字符串,而不是因为垃圾收集器未能清理它们。我见过它会这样失败,但这不太可能。您是否使用分析器来检查内存中是否确实确实有很多字符串?

总而言之,您不能重复使用字符串来存储不同的数据 - 这是不可能的。如果您愿意,您可以编写自己的等效内容 - 但有效且正确地做到这一点的机会相当渺茫。现在,如果您可以提供有关您正在做什么的更多信息,我们也许可以建议不使用太多内存的替代方法。

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.

旧话新听 2024-12-10 14:54:36

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.

关于从前 2024-12-10 14:54:36

考虑到这一事实,我建议您不能重用 C# 中的字符串,请使用 内存映射文件。您只需将字符串保存在磁盘上,并通过像流一样的映射文件以性能/内存消耗良好的关系来处理它。在这种情况下,您重用相同的文件、相同的流,并且仅对您在该特定时刻需要的数据的一小部分(例如字符串)进行操作,然后立即将其丢弃。

这个解决方案严格取决于您的项目要求,但我认为您可能会认真考虑的解决方案之一,因为特别是内存消耗会急剧下降,但您将在性能方面“付出”一些代价。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文