Ruby | binread:无法分配内存(NoMemoryError)

时间:2012-02-13 00:05:46

标签: ruby memory

运行以下代码


    Dir.foreach(FileUtils.pwd()) do |f|
        if f.end_with?('log')
            File.open(f) do |file|
                if File.size(f) > MAX_FILE_SIZE
                    puts f
                    puts file.ctime
                    puts file.mtime

                    # zipping the file
                    orig = f
                    Zlib::GzipWriter.open('arch_log.gz') do |gz|
                      gz.mtime = File.mtime(orig)
                      gz.orig_name = orig
                      gz.write IO.binread(orig)
                      puts "File has been archived"
                    end

                    #deleting the file
                    begin
                      File.delete(f)
                      puts "File has been deleted"
                    rescue Exception => e
                      puts "File #{f} can not be deleted"
                      puts "       Error #{e.message}"                
                      puts "======= Please remove file manually =========="
                    end
                end
            end
        end
    end

文件也非常重,超过1GB。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

如果您正在阅读的文件是> 1GB,你必须至少拥有那么多的无内存,因为IO.binread将会淹没这个数量。

你最好加载已知量并循环输入,直到完全读取,读取和写入块为止。

来自文档:

  IO.binread(name, [length [, offset]] )   -> string

------------------------------------------------------------------------------

Opens the file, optionally seeks to the given offset, then returns
length bytes (defaulting to the rest of the file). binread ensures
the file is closed before returning. The open mode would be "rb:ASCII-8BIT".

       IO.binread("testfile")           #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
       IO.binread("testfile", 20)       #=> "This is line one\nThi"
       IO.binread("testfile", 20, 10)   #=> "ne one\nThis is line "