Ruby 垃圾收集

发布于 2024-11-08 03:04:49 字数 69 浏览 0 评论 0原文

您什么时候会在 Ruby 程序中使用这种垃圾收集方法?

GC.start

When would you use this garbage collection method in your Ruby program(s)?

GC.start

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

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

发布评论

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

评论(6

不再让梦枯萎 2024-11-15 03:04:49

有时需要启动它,但通常它本身就可以正常工作。我曾经遇到过这样的情况:如果不加以控制,应用程序将消耗 1GB 内存,深入交换,间歇性触发 GC.start 会将内存减少到 100MB。

问题在于,调用此方法的成本非常高,如果过度使用,可能会大大减慢应用程序的速度。

There are occasions when it's necessary to kick it off, but usually it works fine by itself. I've had situations where an app will chew through 1GB of memory if left unchecked, pushing deep into swap, where triggering GC.start intermittently will cut that to 100MB.

The trouble is that calling this method is very expensive and can slow down your application considerably if used aggressively.

枫以 2024-11-15 03:04:49

当进行基准测试时,

我正在对一些创建大量对象的代码进行基准测试,我注意到我的基准测试差异很大。我确定峰值来自基准测试期间运行的垃圾收集

手动控制流程为我提供了更一致的基准。

def without_gc
  GC.start # start out clean
  GC.disable
  begin
    yield
  ensure
    GC.enable
  end
end

without_gc do
  Benchmark.measure { some_code }
end

也就是说,如果重复运行 GC.start,将会导致速度显着减慢。

When Benchmarking

I'm benchmarking some code that creates a lot of objects, and I noticed that my benchmarks vary wildly. I determined that the spikes were from garbage collection running during my benchmark.

Controlling the process manually gives me more consistent benchmarks.

def without_gc
  GC.start # start out clean
  GC.disable
  begin
    yield
  ensure
    GC.enable
  end
end

without_gc do
  Benchmark.measure { some_code }
end

That said, GC.start will cause significant slowdowns if you run it repeatedly.

残月升风 2024-11-15 03:04:49

我在内存受限环境 (Heroku) 上迭代大量项目时使用它 - 我每 100 个项目左右强制执行一次 GC.start。

I use it when iterating through large numbers of items on memory constrained environments (Heroku) - I force a GC.start every 100 items or so.

那支青花 2024-11-15 03:04:49

除非您有特殊需要,否则通常不鼓励这样做。前任。有时在内存分析期间,强制GC以获得更好的可预测性很有用。

Usually discouraged unless you have some special need. Ex. sometimes during memory analysis it's useful to force a gc for better predictability.

骑趴 2024-11-15 03:04:49

当加载 100k 行的 CSV 时,这是必须的。否则,服务器会耗尽内存并且无法加载数据。
请参阅https://stackoverflow.com/a/52722689/6594668

When loading a CSV with 100k lines it's a must. Otherwise a server runs out of memory and data does not get loaded.
See https://stackoverflow.com/a/52722689/6594668

凉宸 2024-11-15 03:04:49

Real life example:

When testing a JSON streamer I want to make sure that the memory consumption remains low. Therefore I need to run GC.start before test cases or else they become unpredictable and can even produce false positives.

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