Ruby 尽管将历史记录大小设置为0并清除历史记录,但仍会机械化内存泄漏
下面是一个复制问题的示例脚本Ruby 尽管将历史记录大小设置为0并清除历史记录,但仍会机械化内存泄漏,ruby,memory,memory-leaks,mechanize,mechanize-ruby,Ruby,Memory,Memory Leaks,Mechanize,Mechanize Ruby,下面是一个复制问题的示例脚本 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 th
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_size
和agent.history.clear
这两项工作,但似乎内存使用量随着每个循环的增加而增加
下面是显示内存使用量增加的输出(从48MB开始,每循环增加1-2MB)
如何让Mechanize停止内存泄漏?这不是内存泄漏,有些东西还没有被gc'ed。付诸表决:
GC.start
在循环中,如果您觉得需要它,则可以忽略它。如果您在每次迭代中创建一个新代理,而不是多次重复使用同一代理,行为是否会发生变化?没有变化,仍然会看到与上述相同的内存大小增加。谢谢。将
GC.start
放在循环的末尾修复了一些问题。
GC.start