执行相同的PHP脚本,需要不同的内存


the same php script executing , different memory required

当我运行以下php脚本在本地机器中执行一些统计信息时,它适用于init_set('memory_limit', 100M)

但是,当我在另一个 Web 服务器中执行相同的 PHP 脚本时,处理相同的文件(我确信这一点),它不起作用。

我将memory_limit更改为 1000M,它仍然不起作用,给出异常:"致命错误:允许的内存大小 1048576000 字节耗尽(尝试分配 32 字节)"。最后我改成init_set('memory_limit', 1300M),它有效!

两个mahchine中的php版本与:

PHP 5.3.6 (cli) (built: Mar 28 2012 02:18:34) 
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
    with eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, by eAccelerator

以下是主脚本:

     protected function _insertTable($logType, $conf, $logFileName) {

        $logPath = $this->_getLogDirPath($logType, $conf) . DIRECTORY_SEPARATOR . $logFileName;
        $fileContent = file_get_contents($logPath);
        if ( !fileContent ) return;
        $fields = array_keys($conf['fields']);
        $dao = new dao'StatLogDao($this->_entities[$logType]);
        $lines = explode("'n", $fileContent);
$fileContent = null;
        unset($fileContent);
        foreach ( $lines as $line ) {
            $line = trim($line);
            if ( !$line ) continue;
            $lineData = explode($conf['glue'], $line);
            if ( !$lineData[0] ) continue;
            $entity = $this->_getEntity($logType);
            foreach ($fields as $k => $f) {
                $entity->$f = $lineData[$k];
            }
            try {
              $dao->replace($entity, $fields);
            } catch (Exception $e) {
                echo $e->getMessage() . "'n";
            }
$entity = null;
            unset($entity);
$line = null;
            unset($line);
        }
$lines = null;
        unset($lines);
$dao = null;
        unset($dao);
    }

完全困惑,似乎"未设置"不会释放足够的内存,请帮助我。非常感谢!!

您可能安装了不同的模块...

还有很多事情会导致你的脚本崩溃(看起来像一个无限循环):

  • 执行脚本的 Web 服务器的不同用户权限
  • 用于访问远程服务器的不同 PHP.ini 设置(safe_mode...
  • 您要访问的文件具有不同的读取权限
  • 不同的 MySQL 服务器版本
  • 不同的MySQL用户/表/...权利

我很确定您收到的错误是因为您的脚本出错,而不是因为内存使用情况不同。您还应该检查允许的内存大小memory_limit是否真的可以通过ini_set()进行编辑。尝试设置后,请在两台计算机上使用 echo ini_get('memory_limit'); 进行检查。

为了真正确定两个服务器是"相同的",请将phpinfo();的输出转储到两个文件中,并对它们进行diff!只有服务器名称、唯一 ID 和某些日期应该不同......