Last active
December 30, 2015 20:39
-
-
Save fvox/48eb4b5474c4a9a7a535 to your computer and use it in GitHub Desktop.
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 | |
function randStr($length) { | |
$chars = "abcdefghijklmnopqrstuvwxyz"; | |
$string = null; | |
for ($i = 0; $i < $length; $i++) | |
$string .= $chars[rand(0, strlen($chars) - 1)]; | |
return $string; | |
} | |
class A { | |
public function __destruct() { | |
echo "destroyed!\n"; | |
} | |
} | |
class B { | |
} | |
// Objeto para armazenar as refs | |
$b = new B(); | |
for(; ;) { | |
echo "memory usage 1 step: ", memory_get_usage(), "\n"; | |
// Criando uma variável dinamicamente para armazenar o objeto | |
$varName = randStr(12); | |
${$varName} = new A(); | |
// Armazenando o objeto da classe A dentro de um atributo randômico da classe B | |
$attrName = randStr(10); | |
$b->$attrName = ${$varName}; | |
// Destruindo o objeto A | |
unset(${$varName}); | |
// Tentando usar o objeto ${$varName} | |
var_dump(${$varName}); | |
// Mesmo com o unset no objeto A, o objeto ainda existe dentro do objeto B | |
// Apesar do warning, isto implica que o objeto A foi removido da symbol tables mas ainda existe em memória | |
// E então, o uso de memória não para de crescer com diferenças para o step 1 :) | |
echo "memory usage 2 step: ", memory_get_usage(), "\n\n"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment