8

The code illustrates better what I'm asking:

function foo(){

  $var = get_huge_amount_of_data();

  return $var[0];
}


$s = foo();

// is memory freed here for the $var variable created above?

do_other_stuff(); // need memory here lol

So I know that $var gets freed at some point, but does PHP do it efficiently? Or do I manually need to unset expensive variables?

Anna K.
  • 1,887
  • 6
  • 26
  • 38
  • *efficiently* - you can not ask that way. What you think is *efficiently* might be something different (highly likely by your style of asking BTW) what actually ***is** efficiently* in PHP. – hakre Dec 29 '12 at 15:08
  • well I mean, do it as soon as it's obvious that the variable won't be used anymore. – Anna K. Dec 29 '12 at 15:09
  • 1
    Even earlier. With the end of the function, all local *variables* are gone. It is not of importance if the data stays (temporarily) in memory or not. But to know that you need to know a little bit more about memory management which I guess is not your strength (no offence please). It would be too expensive to de-allocate memory blocks from the system right away, only because a local variable is not used any longer. – hakre Dec 29 '12 at 15:11

4 Answers4

10

Yes it does get freed.

You can check this by using:

function a() {
    $var = "Hello World";
    $content = "";
    for ($i = 0; $i < 10000; $i++) {
        $content .= $var;
    }
    print '<br>$content size:'.strlen($content);
    print '<br>memory in function:'.memory_get_usage();
    return null;
}

print '<br>memory before function:'.memory_get_usage();
a();
print '<br>memory after function:'.memory_get_usage();

output:

memory before function:273312
$content size:110000
memory in function:383520
memory after function:273352

Before the function PHP used 273312 bytes.
Before the function was finished we checked the memory usage again and it used 383520.
We checked the size of $content which is 110000 bytes.
273312 + 110000 = 383312
The remaining 208 bytes are from other variables (we only counted $content)
After the function was finished we checked the memory usage again and it was back to (almost (40 bytes difference)) the same as it was before.

The 40 bytes difference are likely to be function declarations and the for loop declaration.

Piotr Gajdowski
  • 154
  • 1
  • 6
4

You can see this example on a class, that's because you can "catch" freeing a variable in class' destructor:

class a {
  function __destruct(){
    echo "destructor<br>";
  }
}

function b(){ // test function
  $c=new a();
  echo 'exit from function b()<br>';
}

echo "before b()<br>";
b();
echo "after b()<br>";

die();

This script outputs:

before b()
exit from function b()
destructor
after b()

So it is now clear that variables are destroyed at function exit.

Voitcus
  • 4,463
  • 4
  • 24
  • 40
  • However, this does not apply to main scope. You can also set an object variable in main script and see when it is destroyed - it would be kept until `die` end, even if you explicitly call its destructor earlier! – Voitcus Mar 26 '13 at 20:07
2

Yes it is because $var is declare on stack and get clear as soon it goes out of scope

You can refer this https://stackoverflow.com/a/5971224/307157

Community
  • 1
  • 1
slier
  • 6,511
  • 6
  • 36
  • 55
  • 1
    I see absolutely nothing in that link that even hints at what you state. That page is exclusively about semantics. Any other references? -1 for now. –  Dec 29 '12 at 15:04
2

So I know that $var gets freed at some point, but does PHP do it efficiently? Or do I manually need to unset expensive variables?

Yes, PHP makes a good job. This is a question you should never need to think about. In your case I would rather think about the moment between $var = .. and return .., because that is the moment, where you cannot avoid the memory consumption. You should try to find a solution, where you don't need to fetch the whole dataset via get_huge_amount_of_data() and then select a single item, but only the data you need.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173