Last active
August 1, 2016 16:34
-
-
Save korobochkin/26326b787f1fa88fae2b0c9ac9c07f50 to your computer and use it in GitHub Desktop.
A simple example of memory leak in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class A { | |
public $b; | |
function __destruct() { | |
//echo 'Уничтожается ', __CLASS__, PHP_EOL; | |
// Looks like no difference between "unset" and "null" method | |
//$this->b = null; | |
unset($this->b); | |
} | |
} | |
class B { | |
public $a; | |
function __destruct() { | |
//echo 'Уничтожается ', __CLASS__, PHP_EOL; | |
// Looks like no difference between "unset" and "null" method | |
//$this->a = null; | |
unset($this->a); | |
} | |
} | |
for( $i = 0; $i < 1000; $i++ ) { | |
$a = new A(); | |
$b = new B(); | |
$a->b = $b; | |
$b->a = $a; | |
// Delete objects "pointers" | |
unset($a, $b); | |
// But looks like they are exists. | |
// With this method memory freeing correctly | |
/*$a->b = null; | |
$b->a = null;*/ | |
} | |
echo memory_get_usage() / 1024, ' Kbytes used.', PHP_EOL; | |
die(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment