Perl:内存不足

时间:2013-01-23 15:37:44

标签: perl

我从数据库收到大约7000个对象,并将每个项目写入JSON文件。 大约2000个对象perl终止后出现Out of memory错误。在开始时,所有数据库对象都被接收,显然它们完全适合内存。循环期间发生错误。

我对perl很新,我不知道为什么会这样。从我的观点来看,循环中没有内存分配,每次迭代后都无法自由编辑。

这是我的代码:

use lib '../AcePerl/blib/lib','../AcePerl/blib/arch';
use Ace;
use JSON;
use Data::Structure::Util qw( unbless );
use List::MoreUtils;
use strict vars;

use constant HOST => $ENV{ACEDB_HOST} || 'mining.wormbase.org';
use constant PORT => $ENV{ACEDB_PORT} || 2005;

$|=1;

my $json = JSON->new->allow_nonref;

print "Opening the database....";
my $db = Ace->connect(-host=>HOST,-port=>PORT
              -cache => {
                         cache_root         => '/usr/tmp/acedb',
                         cache_depth        => 4,
                         default_expires_in => '1 week'
                         }
) || die "Connection failure: ",Ace->error;
print "done.\n";

#my @anatomies = $db->list('Anatomy_Term','WBbt:000547*');
my @anatomies = $db->fetch(-name    => '*',
                           -class   => 'Anatomy_Term',
                           -fill    => true,
#                           -count   => 10
                          );

#my @anatomies = $db->list('Anatomy_Term','*');

print scalar(@anatomies), " anatomy terms found!\n";

my $i = 0;
my $c;

sub wbstr {
  my $arg = $_[0];
  if(!defined $arg) {
    return undef;
  }else{
    return $arg->name;}
}

# parse anatomy terms for cells
my %anatomy_data;

open (MYFILE, '>anatomy-data2.txt');
print MYFILE '{';

my $parent;
my $first;
my $ancestor;
my $expr_ancestor;
my $is_a;

foreach $c (@anatomies) {
  if($i!=0) {
    print MYFILE ',';
  }

  $i++;
  print MYFILE $json->encode(wbstr($c)), ':{';

  print MYFILE '"term":', $json->encode(wbstr($c->Term)), ',';
  print MYFILE '"definition":', $json->encode(wbstr($c->Definition)), ',';
  print MYFILE '"synonym":', $json->encode(wbstr($c->Synonym)), ',';

  print MYFILE '"parents":[';
  $first = true;
  foreach $parent ($c->CELL_p) {
    if($first eq false) {
      print MYFILE ',';
    }
    print MYFILE $json->encode(wbstr($parent));
    $first = false;
  }
  print MYFILE '],';

  print MYFILE '"ancestors":[';
  $first = true;
  foreach $ancestor ($c->Ancestor) {
    if($first eq false) {
      print MYFILE ',';
    }
    print MYFILE $json->encode(wbstr($ancestor));
    $first = false;
  }
  print MYFILE '],';


  print MYFILE '"expr_ancestors":[';
  $first = true;
  foreach $expr_ancestor ($c->Expr_ancestor) {
    if($first eq false) {
      print MYFILE ',';
    }
    print MYFILE $json->encode(wbstr($expr_ancestor));
    $first = false;
  }
  print MYFILE '],';

  print MYFILE '"is_a":[';
  $first = true;
  foreach $is_a ($c->IS_A_p) {
    if($first eq false) { 
      print MYFILE ',';
    }
    print MYFILE $json->encode(wbstr($is_a));
    $first = false;
  }
  print MYFILE ']}';
}
print MYFILE '}';

print "Done\n";

close (MYFILE); 

1 个答案:

答案 0 :(得分:-1)

尝试刷新文件(或在打印MYFILE语句中的某个位置插入“\ n”)。 This SO question可以帮助您