1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
$myProgam = new MyProgram();
class MyProgram
{
public function __construct()
{
for($i=0;$i<10;$i++)
{
$memory = round((memory_get_usage(true) / 1048576),5)." Mo";
$this->createTree();
echo $memory."<br>";
}
}
/**
* Théoriquement, la mémoire devrait se vider à la sortie de cette fonction,
* mais ce n'est pas le cas !
*/
public function createTree()
{
$tree = new Tree();
//correction 1/3
$tree->__destruct();
}
}
class Tree
{
public $tree;
public function __construct()
{
$root = new Node();
$this->tree = $root;
for($i=0;$i<10;$i++)
{
$node = new Node();
for($cpt=0;$cpt<1000;$cpt++)
{
$node2 = new Node();
$node2->parent = $node;
$node->children[] = $node;
}
$node->parent = $root;
$root->children[] = $node;
}
}
//correction 2/3
public function __destruct()
{
if($this->tree != NULL)
{
foreach($this->tree->children as $child)
{
$child->__destruct();
}
}
}
}
class Node
{
public $parent;
public $children = array();
public $value;
public function __construct()
{
$this->value = "------------------------------------------------------------------";
}
//correction 3/3
public function __destruct()
{
$this->children = array();
}
} |
Partager