Skip to content

Instantly share code, notes, and snippets.

@korobochkin
Last active August 1, 2016 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save korobochkin/26326b787f1fa88fae2b0c9ac9c07f50 to your computer and use it in GitHub Desktop.
Save korobochkin/26326b787f1fa88fae2b0c9ac9c07f50 to your computer and use it in GitHub Desktop.
A simple example of memory leak in PHP
<?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