I'm rying to get the memory used by the whole application in case there were parallels requests or some cronjob running behind . From the PHP doc i found :

memory_get_usage ([ bool $real_usage = FALSE ] ) : int

memory_get_peak_usage ([ bool $real_usage = FALSE ] ) : int

I have been tested those functions in this way :

I created two php file that contains the scripts bellow respectively :

//app.php
<?php

$startMemory = memory_get_peak_usage()/1024;
echo "memory before script : ".$startMemory. "bytes <br>";

$array = new SplFixedArray(10000000);
for ($i = 0; $i < 10000000; ++$i) {
    $array[$i] = $i;
}

$endMemory = (memory_get_peak_usage()/1024);
$scriptMemory = $endMemory - $startMemory;

echo  " memory after script : ". $endMemory. ' bytes <br>';
echo  " script memory  : ". $scriptMemory. ' bytes';

And the second one :

//app2.php
<?php

$startMemory = memory_get_peak_usage()/1024;
echo "memory before script : ".$startMemory. "bytes <br>";

$array = new SplFixedArray(100000);
for ($i = 0; $i < 100000; ++$i) {
    $array[$i] = $i;
}

$endMemory = (memory_get_peak_usage()/1024);
$scriptMemory = $endMemory - $startMemory;

echo  " memory after script : ". $endMemory. ' bytes <br>';
echo  " script memory  : ". $scriptMemory. ' bytes <br>';

The point here is to make the app.php run a script that can talk a while of time , and then execute the app2.php to see if the $startMemory of app2.php is greater than the $startMemory in app.php . but unfortunately it's not .

The results :

//app.php
memory before script : 387.9921875bytes 
memory after script : 156640.234375 bytes 
script memory : 156252.2421875 bytes


app2.php
memory before script : 386.4453125bytes 
memory after script : 1950.6328125 bytes 
script memory : 1564.1875 bytes 

Generally speaking , this seems like is getting the used memory by actual script ,and not care about the memory used in others.

0 Answers