Skip to content

Instantly share code, notes, and snippets.

@attozk
Created November 14, 2014 18:27
Show Gist options
  • Save attozk/5727bad9e621dbca4615 to your computer and use it in GitHub Desktop.
Save attozk/5727bad9e621dbca4615 to your computer and use it in GitHub Desktop.
understanding php memory GC with same var
<?php
$loader = include __DIR__ . '/../vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$counter = 0;
$timer = $loop->addPeriodicTimer(10, function() use(&$timer, &$counter) {
$counter++;
if ($counter > 1 && $counter <= 5) {
$queue = [];
for ($i = 0; $i < 100000; $i++) {
$o = new stdClass();
$queue[] = $o;
}
unset($queue);
}
echo
'Counter: ' . $counter . "\n" .
'Memory: ' . round(memory_get_usage() / 1024 / 1024, 2) . " MB\n" .
'Memory True: ' . round(memory_get_usage(true) / 1024 / 1024, 2) . " MB\n" .
'Peak Memory: ' . round(memory_get_peak_usage() / 1024 / 1024, 2) . "MB \n" .
'Peak Memory True : ' . round(memory_get_peak_usage(true) / 1024 / 1024, 2) . "MB \n" . "\n\n";
if ($counter > 10)
die;
});
$loop->run();
/** outputs **/
Counter: 10
Memory: 8.23 MB
Memory True: 9.25 MB
Peak Memory: 26.86MB
Peak Memory True : 27.5MB
Counter: 11
Memory: 8.23 MB
Memory True: 9.25 MB
Peak Memory: 26.86MB
Peak Memory True : 27.5MB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment