home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / perl / 7675 < prev    next >
Encoding:
Internet Message Format  |  1993-01-06  |  2.7 KB

  1. Path: sparky!uunet!dove!dove.nist.gov!przemek
  2. From: przemek@rrdstrad.nist.gov (Przemek Klosowski)
  3. Newsgroups: comp.lang.perl
  4. Subject: How to limit Perl memory usage?
  5. Message-ID: <PRZEMEK.93Jan5181833@rrdstrad.nist.gov>
  6. Date: 5 Jan 93 23:18:33 GMT
  7. Sender: news@dove.nist.gov
  8. Organization: U. of Maryland/NIST
  9. Lines: 87
  10.  
  11.  
  12. Hello!
  13.  
  14. I wrote a Perl script to archive data from across the network (from
  15. non-unix machines). The script uses FTP to get the remote files,
  16. calling G. Spafford's ftplib.pl library. 
  17.  
  18. The program seems to have a memory leak: it grows by 100 KB per each
  19. file transfered (and I may have several hundred files per run).
  20.  
  21. This is a core of the script (%getlist contains a list of candidates for 
  22. transfer):
  23.     
  24.     while (($key,$val) = each %getlist) {
  25.  
  26.         # <prepare source and destination names>
  27.  
  28.         if (&check_file($fullname)) {
  29.             &ftp'get($fullname, $unixpath) || do # '
  30.             {        # 
  31.             warn &ftp'error; # '
  32.             &booboo("Coudn't get file $host::$fullname"); 
  33.             &ftp'close; # 'shut down ftp 
  34.             next COMPUTER; #  give up on this computer for now
  35.             };        # 
  36.         } else {
  37.             warn "Couldn't find file $host::$fullname\n";
  38.         }
  39.         $BCKLST{$key} = $val;
  40.     }
  41.  
  42. where routine check_file checks if the remote file exists
  43.  
  44. sub check_file {        # does the unique file exist on the FTP host?
  45.     local($file)=@_[0];
  46.     local(@listnames);
  47.     (@listnames = &ftp'list($file)) || (warn &ftp'error, return undef);
  48.     return pop @listnames if( $#listnames == 0 ); # worry if more than one...
  49.     return undef;        # otherwise
  50. }
  51.  
  52. The only place that I can think of that may leak memory is the ftp'do_ftp_listing
  53. routine that I include below: it seems to allocate a global array @ftp_list
  54. and pass it as a result. I have those questions:
  55.  
  56.  - is this consistent with my observed memory growth? I mean, typically
  57.    there would be only one file returned by ftp'list(); why would the list
  58.    take 100KB? 
  59.    If not, what else do I miss? 
  60.  
  61.    If yes, 
  62.  
  63.  - is there a way of deallocating the storage created in do_ftp_listing?
  64.  
  65.    
  66.  
  67. sub list { ## Public
  68.     &do_ftp_listing("nlst", @_);
  69. }
  70. sub do_ftp_listing { ## Private
  71.     local(@lcmd) = @_;
  72.     @ftp_list = ();
  73.     $ftp_trans_flag = 0;
  74.  
  75.     return undef unless &do_open_dport;
  76.  
  77.     return undef unless &do_ftp_cmd(150, @lcmd);
  78.     do {            #  Following is grotty, but chat2 makes us do it
  79.         &chat'expect($Data_handle, 30,
  80.         "(.*)\r?\n",    'push(@ftp_list, $1)',
  81.         "EOF",     '$ftp_trans_flag = 1');
  82.     } until $ftp_trans_flag;
  83.  
  84.     &chat'close($Data_handle);
  85.     return undef unless &do_ftp_cmd(226);
  86.  
  87.     grep(y/\r\n//d, @ftp_list);
  88.     @ftp_list;
  89. }  
  90.  
  91. --
  92.             przemek klosowski (przemek@rrdstrad.nist.gov)
  93.             Reactor Division (bldg. 235), E111
  94.             National Institute of Standards and Technology
  95.             Gaithersburg, MD 20899,      USA
  96.  
  97.             (301) 975 6249
  98.