3

I want to ask you, in PHP if I created a new class in a function, will the memory freed in the end of the function?

For example: Class

class config
{
    var $mapProp    =   array("x"=>4333, "y"=>3520, "w"=>128, "h"=>128);
    var $gameProp   =   array("maxLevel"=>14, "tilesSize"=>256);
    var $mapUrl     =   'map_files';
    function getMapProp()
    {
        return $this->mapProp;
    }
    function getGameProp()
    {
        return $this->gameProp;
    }
    function getMapUrl()
    {
        return $this->mapUrl;
    }
}
$config = new config();

and the function

class framework
{
    function getUserMap()
    {
        require("class/config/config.php");
        require("class/imageManipulation/image.php");       

        $mapUrl     =   $config->getMapUrl();
        $x          =   $_GET['x'];
        $y          =   $_GET['y'];
        $level      =   $_GET['level'];

        $main_img   =   $mapUrl.'/'.$level.'/'.$x.'_'.$y.'.jpg'; 
        //Create a new class
        $image = new imageManipulation();

        //Set Up variables
        $image->setProp($config->getMapProp());
        $image->setGameProp($config->getGameProp());

        return $image->getImage($main_img, $level, $x, $y);
    }       
}
$framework = new framework();

require("class/framework/framework.php");

$path       =   $_GET['path'];

switch ($path) {
    case 'usermap':
        $framework->getUserMap();
        break;
}

in the getUserMap I have used two classes. One of them is the $config class the other is the $image class, after the end of the function will the memory used for this two classes be freed?

All the best, Robert.

1
  • I think you should at least show us (describe) the the class or the function, otherwise, we might as well just guess! :) Commented Mar 14, 2011 at 17:43

3 Answers 3

3

Yes it does. Local variables are disposed at the end of the function call. And if one of these local variables was an object, it is no longer referenced. Therefore it will get freed by the garbage collector.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that "garbage collector" is perhaps not the best phrase here. PHP has a Garbage Collector, but it is used only for detecting circular references which would otherwise never be freed. In normal circumstances, memory is simply released (to the Zend Memory Manager) as soon as the variable is unset or goes out of scope.
3

Run a test on your exact case to find out:

<?php
class bar
{
  public function __construct()
  {
     $this->data = str_repeat('x', 10000000);
  }
}

function foo()
{
  $b = new bar();
  echo memory_get_usage()."\n";
}

echo memory_get_usage()."\n";
foo();
echo memory_get_usage()."\n";

?>

I get:

  • 635248
  • 10635960
  • 635312

Which would indicate it does.

Obviously here nothing is referencing that data any more, so PHP is able to free it.

2 Comments

This is a poor way to check memory usage guarantees. Read the manual rather than haphazardly making assumptions with byte-counts... you never know what other factors might affect the results. Plus you don't actually know when the garbage collector will do its work: it could be after the script terminates!
I never indicated that there is a guarantee that the memory will be released right there and then. I'm just pointing out a way to tell if the possibility exists. (You could call gc_collect_cycles() prior to the final check if you want to make sure.) If the memory is released, then you know you've got no stray references in your code.
1

Firstly, I think you should describe exactly what you mean by "at the end of the function", do you mean at the end of a function call, or ... ?

PHP stores the function in the memory as a construct if you will, un-executed code, then when the function is executed depending on what the function is actually doing, memory is allocated.

For instance, if i had the following function:

function test()
{
    $data = array();

    for($i = 0; $i < 10000; $i++)
    {
        $data[] = array('one','two','three','four','five','six',);
    }
}

the function is called and a reference to the memory is created for the array, each time you iterate the loop, the memory increases.

The function then ends's but if you notice the the data is not return, this is because the local variable is only available for that current scope, there for it would no longer be used and the garbage collector cleans out the memory.

So yes, it does clean out the allocated memory at the end of a function call.

A little tip (does not apply to objects in PHP 5).

you can pass data by references, so that your only modifying the same allocated memory regardless of the function,

for example:

/*
    Allocate the string into the memory into
*/
$data = str_repeat("a",5000);

function minip(&$data)
{
    $_data &= $data;
    $_data = '';
}

im passing the point to the $data to a new variable, which just points to the old one, this way you can pass data around without making the garbage collector use more resources.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.