Skip to content

Instantly share code, notes, and snippets.

@ghacosta
Last active January 18, 2016 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghacosta/69311976b12231fbe4cc to your computer and use it in GitHub Desktop.
Save ghacosta/69311976b12231fbe4cc to your computer and use it in GitHub Desktop.
Memory limit vs usage percent
<?php
$memory_limit = (int)trim(ini_get("memory_limit"),'M');
function echo_memory_usage() {
$mem_usage = memory_get_usage(true);
if ($mem_usage < 1024)
echo $mem_usage." bytes";
elseif ($mem_usage < 1048576) {
echo "memory usage: ".round($mem_usage/1024,2)." kilobytes"."<br>";
echo "percent usage:".round((($mem_usage/1024) * 100 / ($GLOBALS['memory_limit'] * 1024)),2)."%"."<br>";
}
else {
echo "memory usage: ".round($mem_usage/1048576,2)." megabytes"."<br>";
echo "percent usage:".round((($mem_usage/1048576) * 100 / ($GLOBALS['memory_limit'])),2)."%"."<br>";
}
echo "<br/>";
}
echo "memory limit: ".$GLOBALS['memory_limit']. " megabytes"."<br>";
for($i=1; $i<=10; $i++){
//if wants to clear screen
//ob_end_clean();
echo "$i ...<br>";
echo_memory_usage();
sleep(1);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment