如何解决stringBuilder碎片问题?

发布于 2024-11-07 16:53:26 字数 509 浏览 0 评论 0原文

我在 StringBuilders 中遇到了一个很好的 SystemOutOfMemory 异常。这不是由于缺少内存,因此我认为这是内存碎片。

我有大约 200 个 StringBuiler 对象。所有这些都会定期重用(使用 strBldr.clear())。这似乎导致我的系统内存碎片非常严重。我该如何解决这个问题?

谢谢:)

编辑:

这里有一些数据:

输入的最大记录大小& stringBuilder: 4 146 698.

Avarage 重新启动 stringBuilders/秒: >120 (可能 >>120)

输入长度@第一个错误: 16 972 (字符串)

StringBuilder 长度@第一个错误: 16

新 stringBuilder 的次数已发生@第一个错误:~32500

总内存使用量@第一个错误 637 448K

I am getting a nice SystemOutOfMemory exception in my StringBuilders. It is not due to lack of ram, thus I believe it is memory fragmentation.

I have ~200 StringBuiler objects. All of these are reused regularly (with strBldr.clear()). This seems to cause my system to fragment the memory pretty bad. How can I solve this?

Thanks :)

EDIT:

Here are some data:

Max recorded size of input& stringBuilder: 4 146 698.

Avarage re-initiated stringBuilders/second: >120 (possibly >>120)

Length of input @ first error: 16 972 (string)

StringBuilder length @ first error: 16

Count of times a new stringBuilder had been made @ first error: ~32500

Total ram usage @ first error 637 448K

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

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

发布评论

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

评论(3

梓梦 2024-11-14 16:53:26

我同意,很可能你没有耗尽内存,而是陷入碎片。

您必须熟悉碎片和大对象堆 (LOH)。

您没有提供任何详细信息,因此我只能提供一些非常广泛的建议:

  • 尝试估计您的字符串有多大,并使用容量参数进行新的 SB
  • 向上(实际上)这些大小某个数字的倍数。这促进了重复使用。
  • 仅当您希望新内容与旧内容大小几乎相同时才使用 Clear() ,增长它会杀死您。

编辑

输入的最大记录大小&字符串生成器:4 146 698。

  • 确保不需要更大尺寸的中间体,然后
  • 创建所有 StringBuilder,如 sb1 = new StringBuilder(4200000);
  • 不要尝试重复使用它们(太多/ 根本)
  • 不要让它们停留太久

I agree, most likely you are not running out of memory but into fragmentation.

You have to get acquainted with fragmentation and the Large Object heap (LOH).

You don't provide any details so I can only give some very broad advice:

  • try to estimate how large your strings will be and use the Capacity parameter for a new SB
  • round up (really) those sizes to multiples of some number. This promotes re-use.
  • only use Clear() when you expect the new contents to be almost the same size as the old, growing it is what kills you.

Edit

Max recorded size of input& stringBuilder: 4 146 698.

  • Make sure there are no intermediates needed with a greater size, then
  • Create all StringBuilders like sb1 = new StringBuilder(4200000);
  • Don't try to re-use them (too much / at all)
  • Don't keep them around too long
倚栏听风 2024-11-14 16:53:26

您不应该像这样重复使用 StringBuilder,只需根据需要创建一个新的。

当您在 StringBuilder 上调用 Clear 时,它不会释放它使用的所有内存,只会将已使用的大小重置为零。它仍然具有相同的大缓冲区,并且重复使用 StringBuilder 仅意味着缓冲区将达到所需的大小,并且永远不会缩小。

此外,保留 StringBuilder 对象以供重用意味着它们可以通过垃圾回收并转移到下一代堆。这些收集的频率较低,因此它们更有可能对内存碎片敏感。

You should not reuse a StringBuilder like that, just create a new one as needed.

When you call Clear on a StringBuilder it will not release all the memory that it used, it will only reset the used size to zero. It will still have the same large buffer, and repeated use of the StringBuilder only means that the buffer will be as large as it ever needed to be, and never shrink.

Also, keeping the StringBuilder objects for reuse means that they survice garbage collections and move on to the next generation heap. Those are collected less frequently, so they are more likely to be sensetive to memory fragmentation.

心碎的声音 2024-11-14 16:53:26

我最终做的是迁移到x64。这解决了我的问题。

有可能我准确地分配了 x86 的整个内存空间,尽管我没有全部使用它。迁移到 x64 肯定可以解决这个问题。

What I ended up doing is migrating to x64. This solved my problems.

It is possible that I accually assigned the entire memory space of x86, even though I did not use it all. Migrating to x64 certainly would solve that issue.

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