2014-11-05 94 views
0

這裏機械化內存泄漏是一個示例腳本再現問題儘管歷史大小設置爲0和清除歷史

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 
... 

如何獲得機械化來停止內存泄漏?

+0

如果您在每次迭代中創建新代理而不是多次重複使用相同代理,行爲是否會改變? – spickermann 2014-11-05 10:02:29

+0

沒有變化,仍然看到與上述內存大小相同的增加。 – 2014-11-05 10:13:13

回答

2

這不是內存泄漏,有些東西還沒有被加密。把:

GC.start 

在循環,如果你覺得你需要它,否則它可能是安全的忽略。

+0

謝謝。將'GC.start'放在循環的末尾可以解決問題。 – 2014-11-06 08:45:37