dumb question about whether hashes get cleared from memory automatically

J

Jack

Hello,

I am creating several hashes over different large amounts of data as
part of a loop, and capturing some values from a given hash with each
iteration. I dont want to keep them around in memory once I get the
results I am after and then go to the next iteration - I need to be
concerned about memory usage since I am dealing with a large volume -
do I need to actually "clear" the hash like once normally does with an
array or variable or are hashes automatically cleared with the next one
?? If hashes need to be cleared, what is the syntax ?

Thank you,

Jack
 
D

David Squire

Jack said:
Hello,

I am creating several hashes over different large amounts of data as
part of a loop, and capturing some values from a given hash with each
iteration. I dont want to keep them around in memory once I get the
results I am after and then go to the next iteration - I need to be
concerned about memory usage since I am dealing with a large volume -
do I need to actually "clear" the hash like once normally does with an
array or variable or are hashes automatically cleared with the next one
?? If hashes need to be cleared, what is the syntax ?

perldoc -q memory
 
D

David Squire

David said:
perldoc -q memory

Sorry, that's not the FAQ question I was thinking of. No doubt someone
else will give it. Basically, once Perl has got memory from the OS for
you, it won't give it back. It might reuse it once the Perl garbage
collector is done with it - which should happen once you have got rid of
all references to it.


DS
 
D

David Squire

David said:
Sorry, that's not the FAQ question I was thinking of. No doubt someone
else will give it.

It's perldoc -q free, 2nd question: "How can I free an array or hash so
my program shrinks?"
 
Z

zentara

Hello,

I am creating several hashes over different large amounts of data as
part of a loop, and capturing some values from a given hash with each
iteration. I dont want to keep them around in memory once I get the
results I am after and then go to the next iteration - I need to be
concerned about memory usage since I am dealing with a large volume -
do I need to actually "clear" the hash like once normally does with an
array or variable or are hashes automatically cleared with the next one
?? If hashes need to be cleared, what is the syntax ?

Thank you,
Jack

Well there is no "foolproof method" to be absolutely sure you are
clearing out the hash.
The problem is with refcounts. If a hash value contains a reference to
another object, you have to be careful to remove the object first,
before trying to undef the hash key, or else memory will creep up.

There has been much discussion about this, but the best "rule of thumb"
to follow, is to reuse your hashes as much as possible. Don't undef an
hash then create a new one, but loop thru the hash, and set each key's
value to undef (or ''), then undef the key. Then fill it again with new
keys and values. That will almost always work, by reusing the memory
that the previous hash used, so your program memory will act like a
"peak meter", rising to the value of the biggest hash filled.

Some useful links:
http://perlmonks.org?node_id=336822
http://perlmonks.org?node_id=419754
http://perlmonks.org?node_id=349553


I've only seen 1 example where memory was actually returned to the
system from Perl, it follows below. Generally, peak memory is held
by Perl for reuse.

#!/usr/bin/perl -w
use strict;
$| = 1;

{
my $string;
for ( 1 .. 100000 ) {
$string .= ( 'x' x 1000 );
}
print "press enter to release";
<>;

# note that memory does not get released

undef $string;
print "undefined but still in scope of sub, hit enter\n";
<>;

# if the variable only goes out of scope.
# you *need* to undef it!
}

print "ok,out of scope, press enter to exit";
<>;
__END__
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top