php 中允许的内存大小为 33554432 字节(尝试分配 43148176 字节)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/415801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:40:52  来源:igfitidea点击:

Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php

phpmemory-managementmemory-limit

提问by panidarapu

This error message is being presented, any suggestions?

正在显示此错误消息,有什么建议吗?

Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php

php 中允许的内存大小为 33554432 字节(尝试分配 43148176 字节)

回答by panidarapu

At last I found the answer:

最后我找到了答案:

Just add this below line to before line of you getting error in your file

只需将此以下行添加到您的文件中出现错误的行之前

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

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

It will take unlimited memory usage of server, it's working fine.

它将占用服务器的无限内存使用量,它工作正常。

Thanks for giving suggestion friends.

谢谢朋友给的建议。

回答by Haider Abbas

Here are two simple methods to increase the limit on shared hosting:

以下是增加共享主机限制的两种简单方法:

  1. If you have access to your PHP.ini file, change the line in PHP.ini If your line shows 32M try 64M: memory_limit = 64M ; Maximum amount of memory a script may consume (64MB)

  2. If you don't have access to PHP.ini try adding this to an .htaccess file: php_value memory_limit 64M

  1. 如果您有权访问 PHP.ini 文件,请更改 PHP.ini 中的行 如果您的行显示 32M 尝试 64M: memory_limit = 64M ; Maximum amount of memory a script may consume (64MB)

  2. 如果您无权访问 PHP.ini,请尝试将其添加到 .htaccess 文件中: php_value memory_limit 64M

回答by Rik Heywood

Your script is using too much memory. This can often happen in PHP if you have a loop that has run out of control and you are creating objects or adding to arrays on each pass of the loop.

您的脚本使用了太多内存。如果您有一个失去控制的循环,并且您在每次循环时都在创建对象或添加到数组,那么这通常会在 PHP 中发生。

Check for infinite loops.

检查无限循环。

If that isn't the problem, try and help out PHP by destroying objects that you are finished with by setting them to null. eg. $OldVar = null;

如果这不是问题,请尝试通过将已完成的对象设置为 null 来销毁对象来帮助 PHP。例如。$OldVar = null;

Check the code where the error actually happens as well. Would you expect that line to be allocating a massive amount of memory? If not, try and figure out what has gone wrong...

检查错误实际发生的代码。您是否希望该行分配大量内存?如果没有,请尝试找出发生了什么问题...

回答by assetCorp

Doing :

正在做 :

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

is never good. If you want to read a very large file, it is a best practise to copy it bit by bit. Try the following code for best practise.

从来都不好。如果你想读取一个非常大的文件,最好的做法是一点一点地复制它。尝试以下代码以获得最佳实践。

$path = 'path_to_file_.txt';

$file = fopen($path, 'r');
$len = 1024; // 1MB is reasonable for me. You can choose anything though, but do not make it too big
$output = fread( $file, $len );

while (!feof($file)) {
    $output .= fread( $file, $len );
}

fclose($file);

echo 'Output is: ' . $output;

回答by staticsan

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 中编程很容易以比您意识到的更快的方式消耗内存。复制字符串、数组和对象而不是使用引用就可以做到这一点,尽管 PHP 5 应该比 PHP 4 更自动地做到这一点。但是与处理最小的逻辑单元相比,通过多个步骤处理整个数据集也是一种浪费一次。经典示例是处理来自数据库的大型结果集:大多数程序员将整个结果集提取到一个数组中,然后使用foreach(). 使用while()循环一次获取和处理一行的内存效率要高得多。同样的事情适用于处理文件。

回答by Gumbo

If you want to read large files, you should read them bit by bit instead of reading them at once.
It's simple math: If you read a 1 MB large file at once, than at least 1 MB of memory is needed at the same time to hold the data.

如果你想读取大文件,你应该一点一点地读取它们,而不是一次读取它们。
这是一个简单的数学计算:如果您一次读取一个 1 MB 的大文件,那么同时至少需要 1 MB 的内存来保存数据。

So you should read them bit by bit using fopen& fread.

所以你应该使用fopen&一点一点地阅读它们fread

回答by Ziaur Rahman

I have faced same problem in php7.2 with laravel 5.6. I just increase the amount of variable memory_limit = 128Min php.ini as my applications demand. It might be 256M/512M/1048M.....Now it works fine.

我在使用 laravel 5.6 的 php7.2 中遇到了同样的问题。我只是memory_limit = 128M根据应用程序的需要增加php.ini 中的变量数量。它可能是 256M/512M/1048M.....现在它可以正常工作了。

回答by garakchy

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.phpfile in /public_html/wp-includesfolder. Open that file with code editor, and find memory settings under wp_initial_constantsscope, or just Ctrl+F it to find the word "memory". There you will come over WP_MEMORY_LIMITand 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.

我也遇到了同样的问题,寻找 phpinfo.ini、php.ini 或 .htaccess 文件无济于事。最后我查看了一些 php 文件,打开它们并检查内存中的代码。最后这个解决方案是我提出的,它对我有用。我使用的是 wordpress,所以这个解决方案可能只适用于 wordpress 内存大小限制问题。我的解决方案是在/public_html/wp-includes文件夹中打开default-constants.php文件。使用代码编辑器打开该文件,并在范围内找到内存设置,或者只需按 Ctrl+F 即可找到“内存”一词。在那里,你会过来和。只需增加它,就我而言是 64 MB,我将其增加到 128 MB,然后增加到 200 MB。wp_initial_constantsWP_MEMORY_LIMITWP_MAX_MEMORY_LIMIT

// 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' );
    }
}

Btw, please don't do the following code, because that's bad practice:

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

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

回答by Sanjay Kumar N S

You can increase the memory allowed to php script by executing the following line above all the codes in the script:

您可以通过在脚本中的所有代码上方执行以下行来增加允许 php 脚本的内存:

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

And also de allocate the unwanted variables in the script.

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

Check this php library : Freeing memory with PHP

检查这个 php 库:用 PHP 释放内存

回答by ??nh Nguy?n Th?

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