in reply to Memory usage in Perl.

I will attempt to answer your question about memory usage. In general, perl is seriously memory hungry - in a quest for speed, it will burn memory. That is a very general statement.

In more specific context, there are not two variables called "name" in memory when your module is used. Perl uses garbage collection, which is (some say) simplistically implemented by a reference count. When a variable's reference count goes to 0, it is "destroyed" and the memory it used is given back to perl - not to the system - to be reused. Assuming you haven't returned a reference to your $name, that instance of the variable was destroyed when function A exited.

It may be possible perl will reuse that memory space for Function B, or it may allocate more. I do not know how aggressive perl is in its memory recycling and it still really depends on the flow of your code and how it is used.

You can also see perldoc perlobj ( search on garbage ) for a better description of the collection algorithm.

mikfire

Replies are listed 'Best First'.
RE: RE: Memory usage in Perl.
by tilly (Archbishop) on Aug 07, 2000 at 08:25 UTC
    One detail. Lexical variables do not hand their memory back. Instead perl assumes that it will need to repopulate them again and keeps the memory in use to make it faster to allocate on the next pass through. Unfortunately this can be seen in current versions of Perl in an obscure bug when you declare a my variable inside a one-line if or unless. In particular this sort of thing is seriously unwise:
    sub my_func { # Stuff my $foo = $bar unless $cond; # More stuff }
    Perl's behaviour in this case is IMO seriously broken, intentionally undocumented, and as Larry Wall put it, "Use of this feature would be erroneous." It is also far more complex than a simple test would indicate.

    I would expect it to get fixed, probably for 5.6.2. In the meantime make sure that my statements are guaranteed to be executed.

    EDIT
    If anyone wants to know more about this, here is an explanation of what exactly is going on, and the original bug report. (*ahem* Yup, that is my bug.)