下面是一个重现问题的示例脚本
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)。
48603136
50274304
51470336
53260288
54984704
55836672
56799232
57884672
59150336
60358656
61349888
62193664
...
如何让机械化停止泄漏内存?
这不是内存泄漏,有些事情还没有解决。看跌:
GC.start
在循环中,如果你觉得你需要它,否则忽略它可能是安全的。