Find php memory usage
Find php memory usage
PHP has a built in function memory_get_usage() which help us to find the memory usage by a single php script in bytes. Here we are providing you a simple php function that help web developers to find the memory usage in Bytes, Kilo Bytes[KB] and Mega Bytes[MB] of their own scripts.
The script basically give you an idea how much memory is being used before and after a php code has been executed. You can even put your own code in the commented area and find out how much memory it is using to execute the script.
function get_memory_used($val) { $mem_usage = memory_get_usage(true); if ($mem_usage < 1024) echo $mem_usage." Bytes"; elseif ($mem_usage < 1048576) echo "Memory used $val ".round($mem_usage/1024,2)." KB"; else echo "Memory used $val ".round($mem_usage/1048576,2)." MB"; echo "<br/>"; } get_memory_used("before"); //You Can Put Your Own Script Here To Test The Memory Usage $myvar = str_repeat("TESTLOAD", 20000); get_memory_used("after"); //Unset all the variables declared here unset($myvar); get_memory_used("back to normal");
Make sure you unset all the variables declared in order to get the default memory usage.
Find php memory usage,