chuckd has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
Can anyone tell me why a script would blow up my memory usage instantly after returning from a subroutine?
I have a subroutine called "InsertData(\%dafFile);"
I call this sub in my .pl and the sub is located in my .pm file.
I perform some regular expresions on the values in the %datFile I pass into the sub, and print a few things to STDOUT. Other than that there isn't a whole lot going on.
But when I step through and get to the last line in the sub and then exit out of the sub I notice my memory usage in the taskbar jumps from ~500MB to ~900+MB.
The perl.exe process instantly consumes around 400MB.
Can anyone help? Any pointers to links with info will be appreciated as well. Thanks.
-CP
  • Comment on memory blowing up when returning from a subroutine

Replies are listed 'Best First'.
Re: memory blowing up when returning from a subroutine
by mr_mischief (Monsignor) on Feb 05, 2009 at 19:24 UTC
    My first guess is that you return the data structure you just edited in place for some reason. Keep in mind that Perl return the last evaluated expression if there's no return statement. Try putting a return; as the last line in the sub. Without code to look at, there's not much more specific anyone can say. Sorry if this isn't the source of your troubles.
      hi
      I was returning a 1 "return 1;", I have changed it to "return;"
      I will see what happens. I also have tried undef'ing many of the data structures within the sub after I am done with them and before the sub returns. The funny thing is that in this sub, I am printing info to stdout and if I comment out of some of these print statements and then run, it works ok and completes, but when I print everything I need to STDOUT it dies.
        That's a peculiar sounding behavior. Is there any way you can reproduce the problem in a suitably short and nonproprietary snippet?

        Returning a one should mean it wasn't the problem I suspected, BTW. Leaving that in place instead of using a bare return should be fine. The usual reason for such a memory jump is copying some sort of large data structure, so you might want to look for other reasons that might happen.

        Another possible scenario just popped into my head, but it's pure speculation. Are you perchance calling the subroutine inside a while ( <filehandle> ) { .. } loop? If you are and it's a large binary file, it might be hundreds of megabytes before a newline appears. Again, a reproduction of the problem in a short snippet that doesn't give up any private data would be nice.

Re: memory blowing up when returning from a subroutine
by ikegami (Patriarch) on Feb 05, 2009 at 19:18 UTC
    Not much to go on in what you posted. What's the return value, how big is it, and what do you do with it?
Re: memory blowing up when returning from a subroutine
by Bloodnok (Vicar) on Feb 05, 2009 at 19:19 UTC
    Methinx further details might prove useful.

    Other than that, how big is the file from which I infer the %datFile is loaded ?

    A user level that continues to overstate my experience :-))