在并行脚本的情况下获取PHP的内存使用情况

时间:2019-04-11 10:43:24

标签: php memory

我很想获得整个应用程序使用的内存,以防万一有并行请求或某些cronjob在后面运行。 从我发现的PHP文档中:

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

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

我已经通过这种方式测试了这些功能:

我创建了两个php文件,分别包含以下脚本:

//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';

第二个:

//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>';
  

这里的要点是使app.php运行一个可以说   一段时间,然后执行app2.php以查看是否   app2.php的$ startMemory大于app.php中的$ startMemory。   但不幸的是,不是。

结果:

//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 

通常来说,这似乎是通过实际脚本获取已用内存,而不关心其他人使用的内存。

0 个答案:

没有答案