Ruby/Mechanize是否有任何方法来耗尽RAM-&燃气轮机;无法分配内存
我已经建立了一个在网站上投票给我的代码 Ruby脚本运行得很好,但几分钟后,该脚本停止运行,出现以下错误: 因此,我检查了windows任务管理器,每次循环后,ruby.exe的内存都会增长强> 以下是指控和平的准则:Ruby/Mechanize是否有任何方法来耗尽RAM-&燃气轮机;无法分配内存,ruby,variables,memory,mechanize,ram,Ruby,Variables,Memory,Mechanize,Ram,我已经建立了一个在网站上投票给我的代码 Ruby脚本运行得很好,但几分钟后,该脚本停止运行,出现以下错误: 因此,我检查了windows任务管理器,每次循环后,ruby.exe的内存都会增长 以下是指控和平的准则: class VoteWebsite def self.main agent = Mechanize.new agent.user_agent_alias = 'Windows Mozilla' while $stop!=true
class VoteWebsite
def self.main
agent = Mechanize.new
agent.user_agent_alias = 'Windows Mozilla'
while $stop!=true
page = agent.get 'http://website.com/vote.php'
reports_divs = page.search(".//div[@class='Pad1Color2']")
tds = reports_divs.search("td")
i = 3;j = 0;ouiboucle=0;voteboucle=0
while i < tds.length
result = tds[i].to_s.scan(/<font class="ColorRed"><b>(.*?) :<\/b><\/font>/)
type = result[0].to_s[2..-3]
k=i
case type
when "Type of vote"
j= i+1;i += 4
result2 = tds[j].to_s.scan(/<div id="btn(.*?)">/)
id = result2[0].to_s[2..-3]
monvote=define_vote($vote_type, tds[k].to_s, $vote_auto)
page2 = agent.get 'http://website.com/AJAX_Vote.php?id='+id+'&vote='+monvote
voteboucle+=1
.
.
.
else
.
.
.
end
end
end
end
end
VoteWebsite.main
class-VoteWebsite
def自我管理系统
agent=Mechanize.new
agent.user\u agent\u别名='Windows Mozilla'
而$stop=真的
page=agent.get'http://website.com/vote.php'
reports\u divs=page.search(“.//div[@class='Pad1Color2']”)
tds=报告和部门搜索(“td”)
i=3;j=0;ouiboucle=0;voteboucle=0
而i
我认为将方法中的所有变量声明为全局变量应该可以解决这个问题,但是代码非常大,并且这个方法中有大量的变量
那么是否有任何方法(任何Ruby指令)在每个循环结束时耗尽所有变量?您始终可以强制垃圾收集器启动:
GC.start
值得注意的是,这看起来不太像红宝石。使用将多个语句打包到一行
是一种糟糕的形式,使用$
类型变量可能是从其他类型移植过来的遗留问题
请记住,
$
-前缀变量在Ruby中是全局变量,如果不小心使用,可能会导致大量问题,应该保留在非常特定的情况下。最好的选择是一个前缀为@
的实例变量,或者如果必须的话,一个声明的常量
事实上,问题来自mechanize的历史或方法,甚至只是将属性设置为合理的值
#!/usr/bin/env ruby
require 'mechanize'
class GetContent
def initialize url
agent = Mechanize.new
agent.user_agent_alias = 'Windows Mozilla'
agent.history.max_size=0
while true
page = agent.get url
end
end
end
myPage = GetContent.new('http://www.nypost.com/')
希望有帮助 非常感谢,我会尽快把这个放进托盘。。。我知道我用ruby编程很糟糕。你不是第一个告诉我的人。非常感谢你的帮助