【问题标题】:Mechanize memory leak despite setting history size to 0 and clearing history尽管将历史记录大小设置为 0 并清除历史记录,但仍会机械化内存泄漏
【发布时间】:2015-01-01 11:06:09
【问题描述】:

这是重现问题的示例脚本

require 'mechanize'

agent = Mechanize.new
agent.history.max_size = 0 

5000.times do |i| 
  agent.get('http://www.yahoo.com')
  agent.history.clear

  p `ps -o rss -p #{$$}`.strip.split.last.to_i * 1024 # Prints out memory usage of the ruby process
end

我同时使用agent.history.max_sizeagent.history.clear,但似乎每个循环的内存使用量都在增加。

这是显示内存使用量增加的输出(从 48MB 开始,每个循环增加 1-2MB)。

48603136
50274304
51470336
53260288
54984704
55836672
56799232
57884672
59150336
60358656
61349888
62193664
...

如何让 Mechanize 停止泄漏内存?

【问题讨论】:

  • 如果您在每次迭代中创建一个新代理而不是多次重用同一个代理,行为会改变吗?
  • 没有变化,仍然看到与上面相同的内存大小增加。

标签: ruby memory memory-leaks mechanize mechanize-ruby


【解决方案1】:

这不是内存泄漏,只是有些东西还没有被 gc'ed。放:

GC.start

如果你觉得你需要它,就在循环中,否则忽略它可能是安全的。

【讨论】:

  • 谢谢。将GC.start 放在循环的末尾可以解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-26
相关资源
最近更新 更多