On Wed, 2008-12-10 at 13:15 +1030, org chen wrote:
> I read a large file into a hash table. This hash table is used only
> once in the subroutine. I am very concern the memory usage, So I plan
> to realse the memory after used the hash table. The way I used is:
>  
> my %hash = ();
>  
> #read the large file into the hash table
> ...
>  
> #release memory
> %hash = ();
>  
> Do you think this way will really release memory usage?
>  

Yes.

If you declare the has inside the sub, it will be released when the sub
is exited.  That is, unless you create a reference to it and store the
reference in an outside variable.

Example:

#!/usr/bin/perl

use strict;
use warnings;

sub foo {
  my %hash1 = ( a => 1, b => 2 ); # This will be release on exit
  my %hash2 = ( x => 3.14, y => 1.76 );

  return \%hash2; # %hash2 will NOT be released until later
}



-- 
Just my 0.00000002 million dollars worth,
  Shawn

The key to success is being too stupid to realize you can fail.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to