Script to find out PHP memory usage

Open a php file in the server and copy paste the following php code to into it. Access the php file via browser. You can see the memory usage in the web page.

<?php
// This is only an example, the numbers below will
// differ depending on your system
for ($i=1; $i<100; $i++) {
        //echo "used memory is " . memory_get_usage() . "<br />";
        $a = loadmem($i); 
        //echo "used memory after allocating ".$i."m is " . memory_get_usage() . "<br />"; // 57960
        //echo "used memory after unsetting allocated memory is " . memory_get_usage() . "<br />"; // 36744
        echo "You have allocated ". $i . "M (". memory_get_usage() . ") memory in this php script" . "<br />";
        unset($a);
}

function loadmem($howmuchmeg) {
        $a = str_repeat("0", $howmuchmeg * 1024 * 1024); // alocating 10 chars times million chars
        return $a;
}
?>


Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: