出现这个错误消息,有什么建议吗?

允许内存大小为33554432字节已耗尽(已尝试分配 43148176字节)的PHP


当前回答

我想分享我在这个问题上的经验!

假设你有a类和B类。

class A {
    protected $userB;

    public function __construct() {
        $this->userB = new B();
    }
}

class B {
    protected $userA;

    public function __construct() {
        $this->userA = new A();
    }
}

这将启动一个对象链的形成,这可能会产生这种问题!

其他回答

ini_set('memory_limit', '-1');

你的脚本占用了太多内存。在PHP中,如果有一个超出控制的循环,并且每次循环都要创建对象或向数组中添加内容,则经常会发生这种情况。

检查有无无限循环。

如果这不是问题,尝试通过将对象设置为null来销毁它们来帮助PHP。如。$OldVar = null;

还要检查实际发生错误的代码。你认为这一行会分配大量的内存吗?如果没有,试着找出哪里出了问题。

如果您试图读取一个文件,这将占用PHP中的内存。例如,如果您试图打开并读取一个MP3文件(例如,$data = file("http://mydomain.com/path/sample.mp3")),它将把它全部拉到内存中。

正如Nelson所建议的,如果您确实需要使用这么多内存,您可以提高您的最大内存限制。

It is unfortunately easy to program in PHP in a way that consumes memory faster than you realise. Copying strings, arrays and objects instead of using references will do it, though PHP 5 is supposed to do this more automatically than in PHP 4. But dealing with your data set in entirety over several steps is also wasteful compared to processing the smallest logical unit at a time. The classic example is working with large resultsets from a database: most programmers fetch the entire resultset into an array and then loop over it one or more times with foreach(). It is much more memory efficient to use a while() loop to fetch and process one row at a time. The same thing applies to processing a file.

Wordpress用户添加一行:

@ini_set(“memory_limit”、“-1”);

在wp-settings.php中,你可以在wordpress安装的根文件夹中找到