Skip to content

Instantly share code, notes, and snippets.

@attozk
Created November 14, 2014 18:31
Show Gist options
  • Save attozk/a63b9d115aa84c2fd1fd to your computer and use it in GitHub Desktop.
Save attozk/a63b9d115aa84c2fd1fd to your computer and use it in GitHub Desktop.
understanding php memory GC with diff var name
// comparing against https://gist.github.com/attozk/5727bad9e621dbca4615
<?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 = [];
$queue[$counter] = [];
for ($i = 0; $i < 100000; $i++) {
$queue[$counter] = rand(1,9999);
}
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: 0.29 MB
Memory True: 0.5 MB
Peak Memory: 0.38MB
Peak Memory True : 0.5MB
Counter: 11
Memory: 0.29 MB
Memory True: 0.5 MB
Peak Memory: 0.38MB
Peak Memory True : 0.5MB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment