freeing memory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mad Hatter

    #1

    freeing memory

    Hi folks

    I have a script which reads the mysql database,does a lot of string
    manipulation then writes its output to a file. The manipulation is all done
    in a separate function which is called over and over again but with each
    time it's called it's not releasing the memory allocated from the last
    call. After a few Calls it's running out of memory. I'm using 'echo
    memory_get_usag e()' to check the usage and unset(variable names) to try and
    free things up. Anyone tell me where I'm going wrong please.
  • Erwin Moller

    #2
    Re: freeing memory

    Mad Hatter wrote:
    Hi folks
    >
    I have a script which reads the mysql database,does a lot of string
    manipulation then writes its output to a file. The manipulation is all done
    in a separate function which is called over and over again but with each
    time it's called it's not releasing the memory allocated from the last
    call. After a few Calls it's running out of memory. I'm using 'echo
    memory_get_usag e()' to check the usage and unset(variable names) to try and
    free things up. Anyone tell me where I'm going wrong please.
    Hard to tell based on this only.

    A few ideas:
    If your function finishes, you shouldn't unset anything used in that
    function explicitly.
    eg
    function testmem(){
    $IamOnlyHere = "blabla";
    }

    when you call testmem(), the variable $IamOnlyHere is only used during
    your function, and is gone after the call.
    Also the memory allocated is freed.
    So no need to unset() it.

    So my guess would be that you use some global variables or growing
    variables in the scope of your script, like this (stupid example):

    $IgetBig = "";
    for ($i=0;$i<100000 0;$i++){
    $IgetBig .= getMore($i);
    }
    // probably never reach here, because out-of-memory
    echo $IgetBig;

    function getMore($number ){
    $returnThis = str_repeat("bla ",$number);
    }


    In the above example $IgetBig is the variable that consumes all the
    memory after a short while. Unsetting $returnThis in function getMore()
    won't help.

    Any chance some of the variables in the normal scope of the script are
    growing too much? Or are you maybe using one of the superglobals to
    store stuff, like $_SESSION?

    If the above doesn't help you try something like this:

    echo "<pre>";
    print_r($GLOBAL S);
    echo "</pre>";

    at some smart points in your script.
    It produces all the variabels in use.
    (Mind the $GLOBALS instead of the expected $_GLOBALS, expected by me at
    least.)

    Good luck with the bughunt.

    Erwin Moller

    Comment

    • Erwin Moller

      #3
      Re: freeing memory

      correction:

      function getMore($number ){
      $returnThis = str_repeat("bla ",$number);
      }

      should be of course:
      function getMore($number ){
      $returnThis = str_repeat("bla ",$number);
      return $returnThis;
      }

      but you probably guessed. :P

      Erwin Moller

      Comment

      • Mad Hatter

        #4
        Re: freeing memory

        Hi
        echo "<pre>";
        print_r($GLOBAL S);
        echo "</pre>";
        You've pointed me in the right direction. After a lot of searching it
        turned out to be a variable outside the function that I was overlooking.

        Thanks :-)

        Comment

        Working...