Ruby/Mechanize “failed to allocate memory”. Erasing instantiation of 'agent.get' method?

 ̄綄美尐妖づ 提交于 2019-12-04 16:06:32

My suggestion is setting agent.max_history = 0. As mentioned in the list of linked issues.

This will keep a history entry from even being added, instead of using #clear.

Here is the modified version of the other answer

#!/usr/bin/env ruby

require 'mechanize'

$agent = Mechanize.new
$agent.user_agent_alias = 'Windows Mozilla'
$agent.max_history = 0
class GetContent
    def initialize url
        while true
            @page = $agent.get url
        end
    end
end
myPage = GetContent.new('http://www.nypost.com/')

Ok so ! (had enough reputation to answer my owns questions properly)

It seems that Mechanize::History.clear greatly solves this problem of memory leak.

here is the last Ruby code modified if you want to test before and after...

#!/usr/bin/env ruby

require 'mechanize'

$agent = Mechanize.new
$agent.user_agent_alias = 'Windows Mozilla'
class GetContent
    def initialize url
        while true
            @page = $agent.get url
            $agent.history.clear
        end
    end
end
myPage = GetContent.new('http://www.nypost.com/')
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!