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

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


当前回答

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.

其他回答

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

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

在Wordpress中切换到一个新主题后,我收到了同样的错误信息。PHP运行的是5.3版本,所以我切换到了7.2。这解决了问题。

我有同样的问题,在命令行运行php。最近,我更改了php.ini文件,在更改php.ini时犯了一个错误

这是针对php7.0的

到php.ini的路径:/etc/php/7.0/cli/php.ini

我设置了memory_limit = 256(这意味着256字节),而不是memory_limit = 256M(这意味着256兆字节)。

; 脚本可能消耗的最大内存(128MB) ; http://php.net/memory-limit memory_limit = 128M

一旦我纠正了它,我的过程开始正常运行。

I was also having the same problem, looked for phpinfo.ini, php.ini or .htaccess files to no avail. Finally I have looked at some php files, opened them and checked the codes inside for memory. Finally this solution was what I come out with and it worked for me. I was using wordpress, so this solution might only work for wordpress memory size limit problem. My solution, open default-constants.php file in /public_html/wp-includes folder. Open that file with code editor, and find memory settings under wp_initial_constants scope, or just Ctrl+F it to find the word "memory". There you will come over WP_MEMORY_LIMIT and WP_MAX_MEMORY_LIMIT. Just increase it, it was 64 MB in my case, I increased it to 128 MB and then to 200 MB.

// Define memory limits.
if ( ! defined( 'WP_MEMORY_LIMIT' ) ) {
    if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {
        define( 'WP_MEMORY_LIMIT', $current_limit );
    } elseif ( is_multisite() ) {
        define( 'WP_MEMORY_LIMIT', '200M' );
    } else {
        define( 'WP_MEMORY_LIMIT', '128M' );
    }
}

if ( ! defined( 'WP_MAX_MEMORY_LIMIT' ) ) {
    if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {
        define( 'WP_MAX_MEMORY_LIMIT', $current_limit );
    } elseif ( -1 === $current_limit_int || $current_limit_int > 268435456 /* = 256M */ ) {
        define( 'WP_MAX_MEMORY_LIMIT', $current_limit );
    } else {
        define( 'WP_MAX_MEMORY_LIMIT', '256M' );
    }
}

顺便说一句,请不要做下面的代码,因为这是不好的做法:

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

你可以通过执行下面的代码来增加php脚本的内存:

ini_set('memory_limit','-1'); // enabled the full memory available.

并且在脚本中取消分配不需要的变量。