【问题标题】:Php5.3 magic method __call and Memoryphp5.3魔术方法__call和Memory
【发布时间】:2012-06-29 09:49:53
【问题描述】:

我有一个使用大量魔术方法__call 的脚本。 该脚本有 15 000 次迭代,并且对象更大。 每次迭代后,内存都会增长。我使用 unset 或 $val = null;但内存会继续增长。

我能做什么?

一个例子:

$data = null;
foreach ($field['method']['actions'] as $action) {
    // si l'action ne concerne pas le name space principal
    if (!array_key_exists('get', $action)) {
        continue;
    }
    if (array_key_exists('begin', $action)) {
        $data .= $action['begin'];
    }
    if (array_key_exists('action', $action)) {
        $obj = $notice->__call('get' . ucfirst($action['action']));
        $notice->clear();
        if (is_object($obj)) {
            $rsl = $obj->__call('get' . ucfirst($action['get']));
            $obj->clear();
            echo "\n" . 'get' . ucfirst($action['get']) . ' : '  . number_format(memory_get_usage());
            $data .= $rsl;
            unset($rsl);
        } else {
            $data .='';
        }
        $obj = null;
    } else {
        $data .= $notice->__call('get' . ucfirst($action['get']));
        $notice->clear();
        echo "\n" . 'get' . ucfirst($action['get']) . ' : '  . number_format(memory_get_usage());
    }
    if (array_key_exists('end', $action)) {
        $data .= $action['end'];
    }
}
//--
class Notice{
    //--

    protected $instanceObj = null;

    public function __call($name, $arguments = null) {
        $this->instanceObj = $this->$name($arguments);
        return $this->instanceObj;
    }

    public function clear(){
         $this->instanceObj = null;
     }

    //--
}

日志文件示例:

getField : 24,446,752
getField : 24,447,352
getField : 24,447,720
getField : 24,448,096
getField : 24,483,320
getField : 24,483,336
getField : 24,483,728
...
getField : 25,267,936
...
getField : 35,596,712
...

你可以看到记忆永远不会停止浏览。

【问题讨论】:

  • 如果您认为在 PHP 中发现了内存泄漏,您可以发布错误报告并获得“谢谢,但这不是错误”的回复。这就是通常与 PHP 错误有关的方式。否则,如果您可以制作一个仍然显示内存泄漏的最小示例并将其发布在这里,我们可能会看看它是否有其他问题。
  • 我的问题已完成
  • 您的代码不足以重现您的问题。您并没有将Notice::__call() 作为魔术方法调用根本(您是在显式调用它,所以不会发生任何魔术),而是在__call() 方法中调用其他Notice 方法您的示例中没有定义,并且没有输入数据($field['method']['actions'] 数组)可以运行您的代码。

标签: memory call php-5.3


【解决方案1】:

唯一的解决方案是对脚本进行多次执行。问题不是 PHP Symfony,而是生成了太多对象。所以我运行脚本 Pacquet x。否则会导致内存饱和。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-30
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 2019-12-04
    • 1970-01-01
    相关资源
    最近更新 更多