Kernel.open的Ruby内存问题

时间:2014-03-25 22:40:38

标签: ruby-on-rails ruby

我在生产服务器上遇到内存问题。我在delayed_job中使用了bioruby gem(在Rails 4应用程序中)。以前一切都运行良好,它也适用于本地开发(OS X)机器。

服务器上有足够的内存。它有8Gb,几乎没有使用2GB。访问文件时它不会改变。

导致错误发生的确切代码行是使用Kernel.open(第35行):https://github.com/misshie/bioruby-ucsc-api/blob/master/lib/bio-ucsc/file/twobit.rb

def self.load(filename)
      two_bit = nil
      Kernel.open(filename, 'rb') {|f| two_bit = f.read}
      tbq = Bio::Ucsc::File::ByteQueue.new(two_bit)

它试图打开的文件包含人类基因组,并且是800MB,但这个过程在过去的9个月里一直运行良好。

1.9.3p327 :001 > Kernel.open('/home/assay/apps/assay/shared/bin/hg19/hg19.2bit', 'rb') {|f| two_bit = f.read}
NoMemoryError: failed to allocate memory
from (irb):1:in `read'
from (irb):1:in `block in irb_binding'
from (irb):1:in `open'
from (irb):1
from /home/assay/apps/assay/shared/bundle/ruby/1.9.1/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /home/assay/apps/assay/shared/bundle/ruby/1.9.1/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /home/assay/apps/assay/shared/bundle/ruby/1.9.1/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

服务器是Ubuntu 12

assay@assaypipeline:~/apps/assay/shared/bin/hg19$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 12.04 LTS
Release:    12.04
Codename:   precise

修改

在回应下面的CMoi评论时,我只尝试了一个开放,似乎没问题。不知道现在该怎么办。

1.9.3p327 :001 > Kernel.open('/home/assay/apps/assay/shared/bin/hg19/hg19.2bit', 'rb')
 => #<File:/home/assay/apps/assay/shared/bin/hg19/hg19.2bit>

1 个答案:

答案 0 :(得分:1)

如果你试过这个怎么办

tbq = Bio::Ucsc::File::ByteQueue.new(File.open('/home/assay/apps/assay/shared/bin/hg19/hg19.2bit', &:read))

tbq = Bio::Ucsc::File::ByteQueue.new(File.read('/home/assay/apps/assay/shared/bin/hg19/hg19.2bit'))

这将消除将文件读取到局部变量中的块,而是将其直接放入Bio::Ucsc::File::ByteQueue对象中。