Sometimes, we need all memory to run our task, we do ini_set('memory_limit', -1 ), or maximum value we have.
To avoid stuck of server on long and memory consuming tasks, i wrote this check. This is not the same as memory_get_usage() do, but more. It shows virtual memory amount, taken by your process. In percents.
<?php
function getVirtualMemoryTaken()
{
$pid = getmypid();
$a = `ps -p $pid v | awk 'END{print $9}'`;
return $a*1;
}
?>
It works only in linux, tested in Ubuntu 14.
<?php
$a = ' ';
do { $a .= $a . $a; }
while (getVirtualMemoryTaken() < 20 );
?>