以下是再现问题的示例脚本
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
...
如何让Mechanize停止泄漏记忆?
答案 0 :(得分:3)
这不是内存泄漏,有些东西还没有被gc用过。放:
GC.start
如果您觉得自己需要,请在循环中,否则可能会被忽视。