Ruby 垃圾收集
您什么时候会在 Ruby 程序中使用这种垃圾收集方法?
GC.start
When would you use this garbage collection method in your Ruby program(s)?
GC.start
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您什么时候会在 Ruby 程序中使用这种垃圾收集方法?
GC.start
When would you use this garbage collection method in your Ruby program(s)?
GC.start
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
有时需要启动它,但通常它本身就可以正常工作。我曾经遇到过这样的情况:如果不加以控制,应用程序将消耗 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.
当进行基准测试时,
我正在对一些创建大量对象的代码进行基准测试,我注意到我的基准测试差异很大。我确定峰值来自基准测试期间运行的垃圾收集。
手动控制流程为我提供了更一致的基准。
也就是说,如果重复运行 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.
That said,
GC.start
will cause significant slowdowns if you run it repeatedly.我在内存受限环境 (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.除非您有特殊需要,否则通常不鼓励这样做。前任。有时在内存分析期间,强制GC以获得更好的可预测性很有用。
Usually discouraged unless you have some special need. Ex. sometimes during memory analysis it's useful to force a gc for better predictability.
当加载 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
现实生活中的示例:
当测试 JSON Streamer< /a> 我想确保内存消耗保持在较低水平。因此,我需要在测试用例之前运行 GC.start ,否则它们将变得不可预测,甚至可能产生误报。
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.