php 中允许的内存大小已耗尽(尝试分配 43148176 字节) 33554432 字节
出现此错误消息,有什么建议吗?
允许的内存大小 33554432 字节已耗尽(尝试分配 43148176 字节)在 PHP 中
This error message is being presented, any suggestions?
Allowed memory size of 33554432 bytes exhausted (tried to allocate
43148176 bytes) in php
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
我想分享一下我在这个问题上的经验!
假设你有一个类 A 和类 B。
这将启动对象的链形成,这可能会产生此类问题!
I want to share my experience on this issue!
Suppose you have a class A and class B.
this will initiate a chain formation of objects which may be create this kind of issue!
我们遇到了类似的情况,我们尝试了答案顶部给出的
ini_set('内存限制', '-1');
一切正常,压缩图像文件大于 1MB 到 KB。
We had a similar situation and we tried out given at the top of the answers
ini_set('memory_limit', '-1');
and everything worked fine, compressed images files greater than 1MB to KBs.
写入
ini_set('memory_limit', '-1');
打开 php 标签后,在 index.php 顶部
Write
ini_set('memory_limit', '-1');
in your index.php at the top after opening of php tag
在 WordPress 中切换到新主题后,我收到了相同的错误消息。 PHP 运行的是 5.3 版本,所以我切换到 7.2。 这解决了问题。
I was receiving the same error message after switching to a new theme in Wordpress. PHP was running version 5.3 so I switched to 7.2. That fixed the issue.
如果您使用共享主机,您将无法强制执行 php 大小限制的增量。
只需转到您的 cpanel 并将您的 php 版本升级到 7.1 及更高版本即可。
If you are using a shared hosting, you will not be able to enforce the increment in the php size limit.
Just go to your cpanel and upgrade your php version to 7.1 and above then you are good to go.
我运行了以下命令
composer update
,现在正在工作。 如果它不能解决您的问题,请增加php.ini
文件中memory_limit
中的内存限制。I ran the following command
composer update
, and now are working. If its not solve your problem increase your memory limit inmemory_limit
in thephp.ini
file.WordPress 用户在 wp-settings.php 中添加行:
@ini_set('memory_limit', '-1');
,您可以在 WordPress 安装的根文件夹中找到该文件
wordpress users add line:
@ini_set('memory_limit', '-1');
in wp-settings.php which you can find in the wordpress installed root folder
我没有更新我的主机,数据库是只读的。 Joomla 需要编写会话但无法做到。
I hadn't renewed my hosting and the database was read-only. Joomla needed to write the session and couldn't do it.
我在命令行中运行 php 遇到了同样的问题。 最近,我更改了 php.ini 文件,并在更改 php.ini 时出错,
这是针对 php7.0 的
我设置了
memory_limit = 256
(这意味着256 字节
)而不是
memory_limit = 256M
(这意味着256兆字节
)。一旦我更正它,我的进程就开始正常运行。
I had the same issue which running php in command line. Recently, I had changes the php.ini file and did a mistake while changing the php.ini
This is for php7.0
I had set
memory_limit = 256
(which means256 bytes
)instead of
memory_limit = 256M
(which means256 Mega bytes
).Once I corrected it, my process started running fine.
WordPress 用户添加行:
或
在 cpanel 中将 memory_limit 设置为 -1
wordpress users add line:
or
set memory_limit to -1 in cpanel
如果您使用 Laravel,请使用以下方式:
而不是
If you are using Laravel, then use these ways:
instead of
如果您的脚本预期分配如此大的内存量,那么您可以通过将此行添加到您的 php 文件中来增加内存限制,
其中
44M
是您预期的数量被消耗。但是,大多数情况下,此错误消息意味着脚本执行了错误,增加内存限制只会导致相同的错误消息,但数字不同。
因此,您必须重写代码,而不是增加内存限制,这样它就不会分配那么多内存。 例如,以较小的块处理大量数据、取消设置保存较大值但不再需要的变量等。
If your script is expected to allocate that big amount of memory, then you can increase the memory limit by adding this line to your php file
where
44M
is the amount you expect to be consumed.However, most of time this error message means that the script is doing something wrong and increasing the memory limit will just result in the same error message with different numbers.
Therefore, instead of increasing the memory limit you must rewrite the code so it won't allocate that much memory. For example, processing large amounts of data in smaller chunks, unsetting variables that hold large values but not needed anymore, etc.
以下是增加共享主机限制的两种简单方法:
如果您有权访问 PHP.ini 文件,请更改 PHP.ini 中的行
如果您的线路显示 32M,请尝试 64M:
内存限制=64M; 脚本可能消耗的最大内存量 (64MB)
如果您无权访问 PHP.ini,请尝试将其添加到 .htaccess 文件中:
php_value memory_limit 64M
Here are two simple methods to increase the limit on shared hosting:
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)
If you don't have access to PHP.ini try adding this to an .htaccess file:
php_value memory_limit 64M
您的脚本使用了太多内存。 如果您有一个失去控制的循环,并且您在循环的每次传递中创建对象或添加到数组,则在 PHP 中经常会发生这种情况。
检查是否存在无限循环。
如果这不是问题,请尝试通过将已完成的对象设置为 null 来销毁它们来帮助 PHP。 例如。
$OldVar = null;
检查实际发生错误的代码。 您是否期望该行分配大量内存? 如果没有,请尝试找出问题所在......
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.
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;
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...
做:
从来都不是好事。 如果您想读取一个非常大的文件,最好的做法是一点一点地复制它。 尝试以下代码以获得最佳实践。
Doing :
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.
我在 php7.2 和 laravel 5.6 中遇到了同样的问题。 我只是根据应用程序的需求增加 php.ini 中变量
memory_limit = 128M
的数量。 可能是256M/512M/1048M.....现在工作正常了。I have faced same problem in php7.2 with laravel 5.6. I just increase the amount of variable
memory_limit = 128M
in php.ini as my applications demand. It might be 256M/512M/1048M.....Now it works fine.不幸的是,用 PHP 编程很容易,但消耗内存的速度比您想象的要快。 复制字符串、数组和对象而不是使用引用就可以做到这一点,尽管 PHP 5 应该比 PHP 4 更自动地执行此操作。但是与处理最小逻辑单元相比,通过几个步骤处理整个数据集也是浪费的一次。 典型的例子是处理数据库中的大型结果集:大多数程序员将整个结果集提取到数组中,然后使用
foreach()
对其进行一次或多次循环。 使用 while() 循环一次获取和处理一行的内存效率要高得多。 同样的事情也适用于处理文件。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 awhile()
loop to fetch and process one row at a time. The same thing applies to processing a file.如果你想读取大文件,你应该一点一点地读取它们,而不是一次读取它们。
这是一个简单的数学计算:如果您一次读取 1 MB 的大文件,则同时至少需要 1 MB 的内存来保存数据。
所以你应该使用
fopen
& 一点一点地阅读它们。恐惧
。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.
So you should read them bit by bit using
fopen
&fread
.您可以通过在脚本中的所有代码上方执行以下行来增加允许 php 脚本使用的内存:
并且还可以取消分配脚本中不需要的变量。
You can increase the memory allowed to php script by executing the following line above all the codes in the script:
And also de allocate the unwanted variables in the script.
我也遇到了同样的问题,寻找 phpinfo.ini、php.ini 或 .htaccess 文件但无济于事。 最后我查看了一些 php 文件,打开它们并检查里面的代码是否内存。 最后这个解决方案就是我提出的并且它对我有用。 我正在使用 WordPress,因此此解决方案可能仅适用于 WordPress 内存大小限制问题。
我的解决方案是,打开 /public_html/wp-includes 文件夹中的 default-constants.php 文件。 使用代码编辑器打开该文件,然后在
wp_initial_constants
范围下找到内存设置,或者只需按 Ctrl+F 即可找到单词“内存”。 在那里您将遇到WP_MEMORY_LIMIT
和WP_MAX_MEMORY_LIMIT
。 只需增加它即可,在我的例子中是 64 MB,我将其增加到 128 MB,然后增加到 200 MB。顺便说一句,请不要执行以下代码,因为这是不好的做法:
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 overWP_MEMORY_LIMIT
andWP_MAX_MEMORY_LIMIT
. Just increase it, it was 64 MB in my case, I increased it to 128 MB and then to 200 MB.Btw, please don't do the following code, because that's bad practice:
我注意到许多答案只是试图增加分配给有其位置的脚本的内存量,但通常这意味着由于不可预见的体积或大小而导致某些内存过于自由。 显然,如果您不是脚本的作者,除非您雄心勃勃,否则您将受到作者的摆布:) PHP 文档甚至说内存问题是由于“脚本写得不好"
需要提到的是
ini_set('memory_limit', '-1');
(无限制)可能会导致服务器不稳定,因为0 字节空闲 = 坏事
。 相反,请根据脚本尝试执行的操作和计算机上的可用内存量找到合理的平衡点。更好的方法:如果您是脚本的作者(或雄心勃勃),您可以使用 xdebug。 最新版本(2.6.0 - 2018-01-29 发布)带回了内存分析功能,可以显示哪些函数调用正在消耗大量内存。 它暴露了脚本中很难发现的问题。 通常,效率低下的情况是在一个循环中,它不期望收到的数量,但每种情况都会留给读者作为练习:)
xdebug 文档很有帮助,但它归结为 3 个步骤:
apt-get
和yum
等xdebug.profiler_enable = 1
,xdebug.profiler_output_dir = /where/ever/
I notice many answers just try to increase the amount of memory given to a script which has its place but more often than not it means that something is being too liberal with memory due to an unforseen amount of volume or size. Obviously if your not the author of a script your at the mercy of the author unless your feeling ambitious :) The PHP docs even say memory issues are due to "poorly written scripts"
It should be mentioned that
ini_set('memory_limit', '-1');
(no limit) can cause server instability as0 bytes free = bad things
. Instead, find a reasonable balance by what your script is trying to do and the amount of available memory on a machine.A better approach: If you are the author of the script (or ambitious) you can debug such memory issues with xdebug. The latest version (2.6.0 - released 2018-01-29) brought back memory profiling that shows you what function calls are consuming large amounts of memory. It exposes issues in the script that are otherwise hard to find. Usually, the inefficiencies are in a loop that isn't expecting the volume it's receiving, but each case will be left as an exercise to the reader :)
The xdebug documentation is helpful, but it boils down to 3 steps:
apt-get
andyum
etcxdebug.profiler_enable = 1
,xdebug.profiler_output_dir = /where/ever/
如果您尝试读取文件,这将占用 PHP 内存。 例如,如果您尝试打开并读取 MP3 文件(例如 $data = file("http://mydomain.com/path/sample.mp3" )它将把所有内容都放入内存中,
如果您确实需要的话,您可以努力增加最大内存限制。使用这么多内存。
If you are trying to read a file, that will take up memory in PHP. For instance, if you are trying to open up and read an MP3 file ( like, say, $data = file("http://mydomain.com/path/sample.mp3" ) it is going to pull it all into memory.
As Nelson suggests, you can work to increase your maximum memory limit if you actually need to be using this much memory.