! 处理请求时出现意外错误:无法分配内存

请帮我解决这个错误。

我在使用ruby脚本将文本文件中的记录加载到数据库时收到此错误。

如果我使用少量记录加载到数据库中它就可以正常工作。但是如果有大量记录则会失败。

CSV.foreach(fileName) do |line| completePath = line[0] num_of_bps = line[1] completePath = cluster_path+ '/' + completePath inode = FileOrFolder.find_by_fullpath(completePath, :select=>"id") metric_instance = MetricInstance.find(:first, :conditions=>["file_or_folder_id = ? AND dataset_id = ?", inode.id, dataset_id]) add_entry(metric_instance.id, num_of_bps, num_of_bp_tests) end def self.add_entry(metaid, num_of_bps, num_of_bp_tests) entry = Bp.new entry.metric_instance_id = metaid entry.num_of_bps = num_of_bps entry.num_of_bp_tests = num_of_bp_tests entry.save return entry end 

尝试这样的事情:

 File.open(fileName) do |csv| csv.each_line do |line| CSV.parse(line) do |values| # Here you can do your manipulation end end end 

这种方式较慢,但应确保不会出现内存不足。