Skip to main content

Get Memory Used by PHP Script using memory_get_usage

Whenever you are writing any code then understanding the performance of the code is very important. And how much memory is being used by your code is always a key parameter of performance. While writing or reviewing any PHP code our mind get lots of alternative ways to write the same code for optimal memory usage. But if you want to know on the ground that how much memory used by php script then it is very easy. Using PHP built-in functions like memory_get_usage we can analyzed memory used by PHP Script.

PHP memory_get_usage Function

PHP memory_get_usage function returns the amount of memory allocated to PHP in bytes. Using memory_get_usage function we can easily calculate and analyze the memory used by PHP code. Also, we can write alternative code and see what is the difference in the memory used.

PHP memory_get_usage function signature:
int memory_get_usage(real_usage)

The parameter real_usage is an optional boolean parameter, if it is set to true then it returns the real size of memory allocated from the system. By default(if you do not set) the real_usages is passed as false. If it is false or not passed then the memory_get_usage function returns the memory used by emalloc().

Below is one of a basic php memory_get_usage function example:
<?php
print memory_get_usage()."\n" ;//print 354312;
$a = '';
for($i=0;$i<=100000;$i++){
$a[] = $i;
}
print memory_get_usage()."\n";//4552792;
unset($a);
print memory_get_usage()."\n";//354312;
?>
Note: output mentioned in code is output at my computer. At your computer, the result may vary.
In the above code, I have first calculated memory before entering into my code. Then written a loop and populated array and calculated the memory. Finally, I have unset the array and then calculated the memory.

So using the above approach we can easily calculate the memory used by php script.

Example of Memory Used by PHP

Let us take a real example where you have a bigger string and you want to iterate to each character. We will take PHP For Loop and in first method we will calculate string lenght and iterate. And in another we will first convert the string in array using str_split and iterate.

Before proceeding further please note that the output what I am going to show will vary for you.

First the string formation:
<?php
print memory_get_usage()."bytes \n";//print 353464 bytes
$string = str_repeat("Teststring", 100000);
print memory_get_usage()."bytes \n";//1356992bytes
unset($string);
print memory_get_usage()."bytes \n";//353472bytes
?>

Now Let us iterate with for loop and see the result:
<?php
print memory_get_usage()." bytes \n";//print 354512 bytes
$string = str_repeat("Teststring", 100000);
print memory_get_usage()." bytes \n";//1358032 bytes
$strlen = strlen($string);
for($i=0;$i<$strlen;$i++){
//do something;
}
print memory_get_usage()." bytes\n";//print 1358032 bytes
unset($string);
unset($strlen);
unset($i);
print memory_get_usage()." bytes";//print 354512
?>

In the second approach we will first do the str_split and then iterate.
<?php
print memory_get_usage()." bytes \n";//print 354896 bytes
$string = str_repeat("Teststring", 100000);
print memory_get_usage()." bytes \n";//1358416 bytes
$arr_split = str_split($string);
$count = count($arr_split);
for($i=0;$i<$count;$i++){
//do something
}
print memory_get_usage()." bytes\n";//print 66917024 bytes
unset($string);
unset($arr_split);
unset($count);
unset($i);
print memory_get_usage()." bytes";//print 354896
?>

So in the second example you can see that the usage of memory by PHP script got double because we are making copy of the string into an array.

Get Peak memory used by PHP

In the previous section, we have calculated the memory used by php on a different point in time. Now if we have a bunch of code get executed on the request and want to know what is peak memory uses at any particular time of interval then we can get using PHP function memory_ge_peak_usage.

Function Signature:
memory_get_peak_usage(real_usage)

Where real_usage is a non-mandatory parameter, if you will set real usage true then it will return size of memory allocated from the system. If you will not pass real_usage or pass false then it will return the memory used by emalloc().

Now let us consider the previous example of str_split.

<?php
print 'Memory on init is '.memory_get_usage()." bytes \n";
$string = str_repeat("Teststring", 100000);
$arr_split = str_split($string);
$count = count($arr_split);
for($i=0;$i<$count;$i++){
//do something
}
print 'Memory after operation is '.memory_get_usage()." bytes\n";
unset($string);
unset($arr_split);
unset($count);
unset($i);
print 'Memory after unset is '.memory_get_usage()." bytes \n";
print 'Peak Memory was '.memory_get_peak_usage().' bytes';//
?>

For me the output of above code is:

Memory on init is 355256 bytes 
Memory after operation is 66917384 bytes
Memory after unset is 355256 bytes 
Peak Memory was 66917488 bytes

 

Support Me by Sharing This Article

Ankur Kumar Singh

I am a PHP programmer having some knowledge about Linux. I am always interested in web development and knowledge sharing. I am full time tech evangelist part time human being. :-)

Leave a comment/Ask Question

shares