Mokus
Mokus

Reputation: 10400

Php garbage collection

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.

Upvotes: 3

Views: 2047

Answers (3)

RobertPitt
RobertPitt

Reputation: 57268

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.

Upvotes: 1

Matthew
Matthew

Reputation: 48284

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.

Upvotes: 3

mario
mario

Reputation: 145482

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.

Upvotes: 3

Related Questions