How to Calculate memory usage of a php script

Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

How to Calculate memory usage of a php script

Post by Saman » Fri Jul 15, 2011 6:05 pm

Have you ever wanted to know how much memory your PHP script uses when it runs? You might want this information if your script has a memory leak or takes a long time to execute. If that's the case then there's a great built-in function called memory_get_usage, which will display the exact amount of memory being used by your script. This function is really simple to use and will show you the amount of memory, in bytes, being allocated to the script.

Code: Select all

<?php 
// Displays the amount of memory being used as soon as the script runs 
echo memory_get_usage() . "\n"; 

/* 
** Your code goes here 
*/ 

// Displays the amount of memory being used by your code 
echo memory_get_usage() . "\n"; 
?>
Post Reply

Return to “PHP & MySQL”