Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: How to deal with the fact that Perl is not releasing memory

by sundialsvc4 (Abbot)
on Jul 06, 2013 at 19:12 UTC ( #1042939=note: print w/ replies, xml ) Need Help??


in reply to How to deal with the fact that Perl is not releasing memory

Start by moving the my @messages = (); declaration outside of all subs.   The variable is shared among several subs.

The code in each case appears to just be pushing messages onto an array.   Maybe those “not-listed subroutines” actually contain the bug.


Comment on Re: How to deal with the fact that Perl is not releasing memory
Re^2: How to deal with the fact that Perl is not releasing memory
by Anonymous Monk on Jul 06, 2013 at 20:39 UTC
    It's building a response to an HTTP request. By moving the variable declaration outside, it would surely "leak" memory and the "responses" would soon likely be gigabytes in size...

      (Instead of blindly down-voting ...) would you please point out my error?   Seeing a negative number tells me nothing, although I am quite used to it by now.   Obviously, the my variable must be declared somewhere, and in addition, it must be set to empty-list or undef (if this is running in a persistent environment such as mod_perl).   It is genuinely my understanding that declaring the variable within one (in-lined) sub, but using it in another, is Not A Good Thing.™   Please explain the error of my ways.

      Edit:   Was the source-code revised since the first time I saw it, or is it just that my glasses are dirty?   I don’t think I saw multiple my declarations for the array.   More cof-fee... more cof-fee...

      It occurs to me also in re-reading that the my declaration is also the only place where the variable in question is set to empty-list.   Therefore, if the code repeatedly followed another path, the array would continue to grow.   (Although I’d expect that you would be able to see that happening very easily.)

      I also don’t see anything in the fragment that was posted that would account for a runaway memory leak.

      I really want to know, as any Monk would do, where I went wrong on this comment!   Thanks in advance.

        It is genuinely my understanding that declaring the variable within one (in-lined) sub, but using it in another, is Not A Good Thing.

        Each of the anonymous subs has its own my @messages = (); statement. They are not related!(*) They do not name the same memory space (though they may well re-use some or all of the same memory at different times).

        By moving the declarations outside of the subroutines, you would make them occupy the same memory and never be cleaned or re-initialised and thus they would grow unchecked and totally exacerbate the very problem your "advice" was intended to help with. (Presumably; you do not seem deliberately malicious; just <you pick the adjective>.)

        (*)Each time any of the subs gets called, they get a new instance of that array, which is then discarded -- its memory space given back to the process memory pool for reuse -- when the call to the subroutine ends. The fact that they are used for similar purposes in each the 3 subroutines, and therefore are given similar names, does not mean that they have anything whatsoever in common.

        (In fact, the initialisation is completely redundant, a clean, empty array is created each time the subroutines are called.)

        This is how lexical (my) variables work. The fact that you don't know this beggars belief. The fact that you don't know this but still feel qualified to offer others advice, beggars disbelief!


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
        The code wasn't edited when you wrote that comment, but I just did. The complete code is now available.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1042939]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (15)
As of 2014-09-30 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (379 votes), past polls