In general, you won't be able to completely undefine a hash (memory wise), you can only clear out it's data and keys (but Perl will still retain the allocated memory for future use).
Normally the rule is: you need to undef the variable before it goes out of scope. It works for arrays and strings, but not with hashes.
###############################################!/usr/bin/perl use strict; use warnings; # array reclaims mem show_size(); { my @var=(0..1000000); show_size(); undef @var; } show_size(); exit(0); sub show_size { local $/; open(my $pfh, '<', "/proc/$$/status") || die $!; my $size = <$pfh> =~ /VmSize:\s+(\d+)/ ? $1 : 'unknown'; close($pfh); print "Process size: $size\n"; }
#!/usr/bin/perl use strict; use warnings; # dosn't work well with an hash show_size(); { my %hash; for(0..1000000){ $hash{$_}= 'perlperlperl'; } show_size(); foreach(0..1000000){ $hash{$_}= ''; undef $hash{$_}; } undef %hash; } show_size(); exit(0); sub show_size { local $/; open(my $pfh, '<', "/proc/$$/status") || die $!; my $size = <$pfh> =~ /VmSize:\s+(\d+)/ ? $1 : 'unknown'; close($pfh); print "Process size: $size\n"; }
In reply to Re: How to destroy variables and hash?
by zentara
in thread How to destroy variables and hash?
by PerlPhi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |