memory_limit:它是如何工作的?

Mar*_*arm 9 php memory memory-limit

我有一个运行大约2个小时的PHP脚本.这是一个cron工作.cron作业每4小时运行一次.

在脚本的末尾,我显示一些内存值.

The memory_get_usage() result is 881568 Bytes (0.840766906738M)
The memory_get_peak_usage() result is 1340304 Bytes (1.27821350098M)
The memory_get_usage(true) result is 1572864 Bytes (1.5M)
The memory_get_peak_usage(true) result is 1835008 Bytes (1.75M)
Run Code Online (Sandbox Code Playgroud)

php.ini中的memory_limit是128M并且它不起作用.我把它提升到256M,现在它可以工作了.

但由于脚本的内存峰值小于2M ....

那么memory_limit参数如何工作?

它是脚本使用的总内存吗?如果是这样,我该如何计算呢?

它是脚本的内存峰值吗?如果是的话,我算得对吗?

我正在使用php 5.3.16.

编辑

我没有任何错误消息.当限制为128M时脚本执行,但永远不会完成.

Jar*_*ipe 3

尝试使用一种使用操作系统来报告实际内存的函数,例如此函数。

function unix_get_usage() { 
      $pid = getmypid(); 
      exec("ps -o rss -p $pid", $output); 
      return $output[1] *1024; 
 }

function windows_get_usage(){
    $output = array();
    exec('tasklist /FI "PID eq '.getmypid().'" /FO LIST', $output );
    return preg_replace( '/[^0-9]/', '', $output[5] ) * 1024;
}
Run Code Online (Sandbox Code Playgroud)

你的脚本可能消耗了大量 PHP 在从 memory_get_usage() 返回时没有考虑的内存(顺便说一句,它查看堆)。调用并返回 memory_get_usage() 时,堆栈分配的变量将被清除。

您还应该尝试在执行过程中的其他时间点运行此函数和其他函数,例如在大型 MySQL 调用或文件处理之后。memory_get_peak_usage() 可能不适用于所有操作系统,特别是取决于编译选项。