freeing the memory used by a hash

R

ravi

which is a best way to free the all the memory used by a hash explain
in the context of complicated data structures like hash of hashes

undef %hash;
delete %hash;
%hash =();

What is the difference between these three
 
T

Tore Aursand

which is a best way to free the all the memory used by a hash explain in
the context of complicated data structures like hash of hashes

undef %hash;
delete %hash;
%hash =();

What is the difference between these three

What happened when you read the documentation, specifically:

perldoc -f undef
perldoc -f delete

You might also want to read these:

perldoc -q memory
perldoc -q shrinks
 
J

Joe Smith

ravi said:
which is a best way to free the all the memory used by a hash explain
in the context of complicated data structures like hash of hashes

undef %hash;
delete %hash;
%hash =();

The middle one is incomplete; the delete() function is used on
individual entries in the hash. To get them all, you would need
delete $hash{$_} for keys %hash;

All of them mark the memory used by the hash as being available for
reuse. Perl will use it for new variables when it needs it.
It will not return memory back to the OS.
-Joe
 
A

Anno Siegel

Joe Smith said:
The middle one is incomplete; the delete() function is used on
individual entries in the hash. To get them all, you would need
delete $hash{$_} for keys %hash;

....or a slice:

delete @hash{ keys %hash};

Anno
 
T

Tad McClellan

ravi said:
which is a best way to free the all the memory used by a hash


Simply by letting the hash go out of scope.


{ my %hash;
# do stuff
}
# memory is reclaimed by perl here
 

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

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,880
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top