- basename: documentation ( source)
- debug_backtrace: documentation ( source)
- str_repeat: documentation ( source)
- round: documentation ( source)
- memory_get_usage: documentation ( source)
- printf: documentation ( source)
<?php
function m()
{
$memory = round(memory_get_usage() / 1024 / 1024, 2);
$frame = debug_backtrace(0, 1)[0];
$file = basename($frame['file']);
$line = $frame['line'];
printf("%s:%d | %sMb\n", $file, $line, $memory);
}
function gen(): \iterator
{
$data = str_repeat('x', 10 * 1024 * 1024);
$i = 0;
while ($data) {
m();
yield $data;
m();
unset($data);
yield;
m();
++$i;
// Simulate the API return nothing
if ($i == 3) {
$data = false;
} else {
$data = str_repeat('x', 10 * 1024 * 1024);
}
m();
}
};
m();
foreach ($gen = gen() as $k => $v) {
$gen->next();
m();
unset($k, $v);
m();
}
m();