Php Garbage Collection

I want to ask you, in PHP, if I created a new class in a function, would memory be freed at 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 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 getUserMap I used two classes. One of them is the $ config class, the other is the $ image class, after the memory used for these two classes is freed at the end of the function?

All the best, Robert.

+2
garbage-collection php memory
Mar 14 2018-11-11T00:
source share
3 answers

Yes Yes. Local variables are located 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 be freed by the garbage collector.

+2
Mar 14 2018-11-17T00:
source share

Run a test on your specific 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

Whatever it says.

Obviously, nothing else refers to this data here, so PHP can free it.

+2
Mar 14 '11 at 17:52
source share

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

PHP stores the function in memory as a construct, if you are not executable code, then when the function is executed, depending on what the function actually performs, memory is allocated.

For example, 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 memory reference is created for the array, each time you repeat the loop, the memory increases.

Then the function ends, but if you notice that the data is not returned, this is due to the fact that the local variable is available only for this current area, there it will no longer be used, and the garbage collector will clear the memory.

So yes, it clears the allocated memory at the end of the function call.

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

you can pass data by links, so your only modification of the same allocated memory, regardless of function,

eg:

 /* Allocate the string into the memory into */ $data = str_repeat("a",5000); function minip(&$data) { $_data &= $data; $_data = ''; } 

im passes the point in $data to a new variable that just points to the old one, so you can transfer data without making the garbage collector more resources.

0
Mar 14 '11 at 18:00
source share



All Articles