我有一堆客户端销售点 (POS) 系统,它们定期将新的销售数据发送到一个集中数据库,该数据库将数据存储到一个大数据库中以生成报告。
客户端 POS 基于 PHPPOS,我实现了一个使用标准 XML-RPC 库将销售数据发送到服务的模块。 服务器系统基于CodeIgniter构建,并使用XML-RPC和XML-RPCS库作为Web服务组件。 每当我发送大量销售数据(销售表中的少至 50 行,以及与销售中的每个项目相关的 sales_items 中的各个行)时,我都会收到以下错误:
致命错误:允许的内存大小 134217728 字节已耗尽(尝试分配 54 字节)
128M 是 php.ini 中的默认值,但我认为这是一个需要打破的巨大数字。 事实上,我什至尝试过将此值设置为 1024M,但它所做的只是需要更长的时间才能出错。
至于我采取的步骤,我尝试禁用服务器端的所有处理,并对其进行操纵以返回预设响应,无论输入如何。 但是,我认为问题在于数据的实际发送。 我什至尝试禁用 PHP 的最大脚本执行时间,但仍然出错。
I have a bunch of client point of sale (POS) systems that periodically send new sales data to one centralized database, which stores the data into one big database for report generation.
The client POS is based on PHPPOS, and I have implemented a module that uses the standard XML-RPC library to send sales data to the service. The server system is built on CodeIgniter, and uses the XML-RPC and XML-RPCS libraries for the webservice component. Whenever I send a lot of sales data (as little as 50 rows from the sales table, and individual rows from sales_items pertaining to each item within the sale) I get the following error:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 54 bytes)
128M is the default value in php.ini
, but I assume that is a huge number to break. In fact, I have even tried setting this value to 1024M, and all it does is take a longer time to error out.
As for steps I've taken, I've tried disabling all processing on the server-side, and have rigged it to return a canned response regardless of the input. However, I believe the problem lies in the actual sending of the data. I've even tried disabling the maximum script execution time for PHP, and it still errors out.
发布评论
评论(30)
PHP 脚本中很容易出现内存泄漏 - 特别是当您使用抽象(例如 ORM)时。 尝试使用 Xdebug 来分析您的脚本并找出所有内存都去了哪里。
It's very easy to get memory leaks in a PHP script - especially if you use abstraction, such as an ORM. Try using Xdebug to profile your script and find out where all that memory went.
当使用 array_push 将 2250 万条记录添加到数组中时,我在使用
4G
作为文件 php.ini 中的内存限制时,在大约 20M 记录时不断收到“内存耗尽”致命错误。 为了解决这个问题,我在文件顶部添加了该语句。 现在一切正常。 不知道PHP有没有内存泄漏。 那不是我的工作,我也不关心。 我只需要完成我的工作,这很有效。
程序非常简单:
致命错误指向第 3 行,直到我提高了内存限制,这
消除了错误。
When adding 22.5 million records into an array with array_push I kept getting "memory exhausted" fatal errors at around 20M records using
4G
as the memory limit in file php.ini. To fix this, I added the statementat the top of the file. Now everything is working fine. I do not know if PHP has a memory leak. That is not my job, nor do I care. I just have to get my job done, and this worked.
The program is very simple:
The fatal error points to line 3 until I boosted the memory limit, which
eliminated the error.
我不断收到此错误,即使在
php.ini
中设置了memory_limit
,并且使用phpinfo()
正确读取了值。通过将其从以下更改
为
:这纠正了 PHP 7 中的问题。
I kept getting this error, even with
memory_limit
set inphp.ini
, and the value reading out correctly withphpinfo()
.By changing it from this:
To this:
This rectified the problem in PHP 7.
您网站的根目录:
Your site's root directory:
您可以通过更改 fastcgi/fpm 上的
memory_limit
来正确修复此问题:更改内存,例如从 128 更改为 512,请参见
下文
You can properly fix this by changing
memory_limit
on fastcgi/fpm:Change memory, like from 128 to 512, see below
to
当您看到上述错误时 - 特别是如果
(tried to allocate __ bytes)
值较低,则可能表示无限循环,就像一个无法退出的函数一样:When you see the above error - especially if the
(tried to allocate __ bytes)
is a low value, that could be an indicator of an infinite loop, like a function that calls itself with no way out:启用这两行后,它开始工作:
After enabling these two lines, it started working:
如果代码的一部分可能使用大量内存,您可以删除
memory_limit,而不是更改
在该部分运行之前,然后在之后替换它。php.ini
文件中的memory_limit
值Rather than changing the
memory_limit
value in yourphp.ini
file, if there's a part of your code that could use a lot of memory, you could remove thememory_limit
before that section runs, and then replace it after.在 Drupal 7 中,您可以修改站点/默认文件夹中的 settings.php 文件中的内存限制。 在第 260 行左右,您将看到以下内容:
即使您的 php.ini 设置足够高,如果您的 Drupal settings.php 文件中没有设置,您也将无法消耗超过 128 MB 的内存。
In Drupal 7, you can modify the memory limit in the settings.php file located in your sites/default folder. Around line 260, you'll see this:
Even if your php.ini settings are high enough, you won't be able to consume more than 128 MB if this isn't set in your Drupal settings.php file.
更改 php.ini 文件中的内存限制并重新启动 Apache。 重新启动后,从任何 PHP 文件运行 phpinfo(); 函数以确认
memory_limit
更改。内存限制 -1 表示没有设置内存限制。 现在已经是最大了。
Change the memory limit in the php.ini file and restart Apache. After the restart, run the phpinfo(); function from any PHP file for a
memory_limit
change confirmation.Memory limit -1 means there is no memory limit set. It's now at the maximum.
只需在网页顶部添加
ini_set('memory_limit', '-1');
行即可。您可以根据需要将内存设置为 -1、
16M
等。Just add a
ini_set('memory_limit', '-1');
line at the top of your web page.And you can set your memory as per your need in the place of -1, to
16M
, etc..对于 Drupal 用户,Chris Lane 的回答是:
有效,但我们需要将其放在
站点根目录中的 index.php 文件中的开始标记之后。
For Drupal users, this Chris Lane's answer of:
works but we need to put it just after the opening
tag in the index.php file in your site's root directory.
PHP 5.3+ 允许您通过在
public_html
文件夹中放置.user.ini
文件来更改内存限制。只需创建上述文件并在其中键入以下行:
某些 cPanel 主机仅接受此方法。
PHP 5.3+ allows you to change the memory limit by placing a
.user.ini
file in thepublic_html
folder.Simply create the above file and type the following line in it:
Some cPanel hosts only accept this method.
崩溃页面?
(当 MySQL 必须查询大行时会发生这种情况。默认情况下,
memory_limit
设置为较小,这对于硬件。)您可以在增加
php.ini
之前检查系统现有内存状态:这里我按如下方式增加了它,然后执行
service httpd restart
来修复崩溃页面问题。Crash page?
(It happens when MySQL has to query large rows. By default,
memory_limit
is set to small, which was safer for the hardware.)You can check your system existing memory status, before increasing
php.ini
:Here I have increased it as in the following and then do
service httpd restart
to fix the crash page issue.对于那些绞尽脑汁想知道为什么这个小函数会导致内存泄漏的人来说,有时由于一个小错误,函数就会开始永远递归地调用自身。
例如,代理类与要代理它的对象的函数具有相同的名称。
有时你可能会忘记带上那个小小的actualObjec成员,并且因为代理实际上有那个doSomething方法,PHP不会给你任何错误,对于一个大类,它可能会被隐藏起来几分钟找出内存泄漏的原因。
For those who are scratching their heads to find out why on earth this little function should cause a memory leak, sometimes by a little mistake, a function starts recursively call itself for ever.
For example, a proxy class that has the same name for a function of the object that is going to proxy it.
Sometimes you may forget to bring that little actualObjec member and because the proxy actually has that
doSomething
method, PHP wouldn't give you any error and for a large class, it could be hidden from the eyes for a couple of minutes to find out why it is leaking the memory.在比之前工作的数据集更小的数据集上运行时,我遇到了以下错误。
当对故障的搜索将我带到这里时,我想我会提到它并不总是以前的答案中的技术解决方案,但更简单。 就我而言,它是 Firefox。 在我运行该程序之前,它已经使用了 1,157 MB。
事实证明,我在几天内一次观看了一个 50 分钟的视频,结果把事情搞砸了。 这是专家们不假思索就纠正的修复方法,但对于像我这样的人来说,这是值得牢记的。
I had the error below while running on a dataset smaller than had worked previously.
As the search for the fault brought me here, I thought I'd mention that it's not always the technical solutions in previous answers, but something more simple. In my case it was Firefox. Before I ran the program it was already using 1,157 MB.
It turns out that I'd been watching a 50 minute video a bit at a time over a period of days and that messed things up. It's the sort of fix that experts correct without even thinking about it, but for the likes of me it's worth bearing in mind.
就我的 mac(Catalina - Xampp)而言,没有加载文件,所以我必须先执行此操作。
然后更改
memory_limit = 512M
然后重新启动Apache并检查文件加载
结果是否为
Finally Check
In my case on mac (Catalina - Xampp) there was no loaded file so I had to do this first.
Then change
memory_limit = 512M
Then Restart Apache and check if file loaded
Result was
Finally Check
使用
yield
也可能是一个解决方案。 请参阅生成器语法。有时,在循环内实现
yield
可能会解决问题,而不是更改PHP.ini
文件以获得更大的内存存储。 Yield 所做的并不是一次转储所有数据,而是一项一项地读取数据,从而节省了大量的内存使用。Using
yield
might be a solution as well. See Generator syntax.Instead of changing the
PHP.ini
file for a bigger memory storage, sometimes implementing ayield
inside a loop might fix the issue. What yield does is instead of dumping all the data at once, it reads it one by one, saving a lot of memory usage.像这样运行脚本(例如 cron 情况): php5 /pathToScript/info.php 会产生相同的错误。
正确方法:
php5 -cli /pathToScript/info.php
Running the script like this (cron case for example):
php5 /pathToScript/info.php
produces the same error.The correct way:
php5 -cli /pathToScript/info.php
如果您运行的是 WHM 支持的 VPS(虚拟专用服务器),您可能会发现您没有直接编辑 PHP.INI 的权限; 系统必须这样做。 在WHM主机控制面板中,转到服务配置→PHP配置编辑器并修改
内存限制
:If you're running a WHM-powered VPS (virtual private server) you may find that you do not have permissions to edit PHP.INI directly; the system must do it. In the WHM host control panel, go to Service Configuration → PHP Configuration Editor and modify
memory_limit
:我发现在实际处理的文件中包含或要求
_dbconnection.php_
和_functions.php
时(而不是包含在标头中)很有用。 这本身就包含在内。因此,如果包含页眉和页脚,只需在包含页眉之前包含所有功能文件即可。
I find it useful when including or requiring
_dbconnection.php_
and_functions.php
in files that are actually processed, rather than including in the header. Which is included in itself.So if your header and footer is included, simply include all your functional files before the header is included.
对我来说,出现此错误消息的最常见原因是在 PHP“for”语句中省略了“++”运算符。 这会导致循环永远继续下去,无论您允许使用多少内存。 这是一个简单的语法错误,但编译器或运行时系统很难检测到。 只要我们愿意去寻找,我们就很容易改正!
但是假设您想要一个通用程序来尽早停止此类循环并报告错误? 您可以简单地检测每个循环(或至少最里面的循环),如下所述。
在某些情况下,例如异常内的递归,
set_time_limit
失败,并且浏览器不断尝试加载 PHP 输出,要么是无限循环,要么是致命错误消息(这是本问题的主题)。通过减少代码开头附近允许的分配大小,您可能能够防止致命错误,如其他答案中所述。
然后您可能会留下一个终止的程序,但仍然难以调试。
无论您的程序是否终止,都可以通过在程序中插入
BreakLoop()
调用来检测您的代码,以获得控制权并找出程序中导致问题的循环或递归。BreakLoop 的定义如下:
$LoopSite 参数可以是代码中函数的名称。 这并不是真正必要的,因为您将收到的错误消息会将您指向包含 BreakLoop() 调用的行。
The most common cause of this error message for me is omitting the "++" operator from a PHP "for" statement. This causes the loop to continue forever, no matter how much memory you allow to be used. It is a simple syntax error, yet is difficult for the compiler or runtime system to detect. It is easy for us to correct if we think to look for it!
But suppose you want a general procedure for stopping such a loop early and reporting the error? You can simply instrument each of your loops (or at least the innermost loops) as discussed below.
In some cases such as recursion inside exceptions,
set_time_limit
fails, and the browser keeps trying to load the PHP output, either with an infinite loop or with the fatal error message which is the topic of this question.By reducing the allowed allocation size near the beginning of your code you might be able to prevent the fatal error, as discussed in the other answers.
Then you may be left with a program that terminates, but is still difficult to debug.
Whether or not your program terminates, instrument your code by inserting
BreakLoop()
calls inside your program to gain control and find out what loop or recursion in your program is causing the problem.The definition of BreakLoop is as follows:
The $LoopSite argument can be the name of a function in your code. It isn't really necessary, since the error message you will get will point you to the line containing the BreakLoop() call.
出现此错误的原因是您的服务器配置的内存限制非常低。 尝试将其添加到 wp-config.php(将其放在此文件中的
请注意,此限制对于主题和主题附带的插件来说是可以的。 如果您想启用其他插件,您可能需要进一步增加限制。
The reason for this error is that your server configuration has a very low memory limit. Try adding this to wp-config.php (put it after <?php in this file):
Please note that this limit is OK for the theme and the plugins that come with the theme. If you want to enable other plugins you may need to increase the limit further.
我对
ini_set('memory_limit', 'size_you_want');
表示怀疑。相反,我将上述内容与
ini_restore('memory_limit');
结合使用。 这将恢复 PHP 配置中定义的原始内存限制。 https://www.php.net/manual/en/function。 ini-restore.phpI got skeptical of
ini_set('memory_limit', 'size_you_want');
.Rather, I use the above in conjunction with
ini_restore('memory_limit');
. This restores the original of the memory_limit has defined in your PHP config. https://www.php.net/manual/en/function.ini-restore.phpGreetings 是一个非常常见的问题,因为如果您分配给 php 的内存非常少,并且您的网站正在增长,则需要更多资源。
我发现自己所在的网站存在问题,仅修改某些产品时会出现错误 500,问题是他们在这些特定产品中使用了非常重的图像,解决方案:
1.- 增加 php.ini 中的“内存限制”
2.- 降低图像的权重。
3.- 再次将“memory_limit”调整为可接受的值“512M”,至少对我来说足够了。
现在重要的是您验证是否进行了更改,因为 php 除了在服务器上有多个版本和多种安装类型之外,也许您修改了一个版本但它不起作用,这是因为您没有修改正确的 php。 ini 文件。
如何验证您正在修改正确的文件?
在 prestashop 仪表板中,转到高级设置/信息,您可以看到“内存限制”。
永远记住,在 php.ini 文件中进行更改后,建议重新启动 apache 或 Nginx。
Ubuntu: sudo services apache2 restart
重要提示:永远不要像很多人在这里提到的那样设置“memory_limit = -1”。 问题是,如果文件或模块出现问题,您可能会陷入连续循环,消耗所有服务器的内存和处理器。 让我们举一个简单的例子:一个模块发生错误并调用一个函数,直到它不肯定为止,它会一直调用,这将创建一个无限循环,并且它永远不会停止这样做,因为 php 没有限制。
希望对遇到这个问题的同事有所帮助。
Greetings is a very common problem because if you have very little memory allocated to php and your website is growing will require more resources.
I found myself in a site that had problems that gave error 500 to modify only some products, the problem was that they had used very heavy images in those specific products, solution:
1.- Increase "memory_limit" in php.ini
2.- Lower the weight of the images.
3.- Adapt again "memory_limit" to an acceptable value "512M" at least for me more than enough.
now it is important that you verify that the changes are being made because php apart from having several versions and several types of installations on the server, maybe you modify one and it does not work and this is because you are not modifying the correct php.ini file.
How do you verify that you are modifying the correct file?
In the prestashop dashboard go to advanced settings/information there you can see "Memory limit".
always remember that after making a change in the php.ini file it is advisable to restart apache or Nginx.
Ubuntu: sudo services apache2 restart
IMPORTANT NOTE: Never set the "memory_limit = -1" as many people mention here. The problem is that if you have a problem with a file or module you could be in a continuous loop consuming all the server's memory and processor. Let's take a simple example: a module has an error and makes a call to a function and until it is not positive it keeps calling, this will create an infinite loop and it will never stop doing it because php has no limit.
I hope it helps colleagues who have this problem.
增加
内存限制
解决了问题。 但是,我在找到内存限制时遇到了问题。 我直接从实时服务器处理我的项目,所以如果您也这样做,在 cPanel 上,如果您转到“Software - MultiPHP INI Editor”,您可以找到memory_limit
并选择位置。 我把我的从256M增加到512M。 您还可以找到说明 此处。Increasing the
memory_limit
fixed the problem. However, I had problems finding the memory limit. I am working on my project directly from live server, so if you're doing the same, on cPanel you can find thememory_limit
if you go to Software - MultiPHP INI Editor and select the location. I increased mine from 256M to 512M. You can also find instructions here.通过
ini_set('memory_limit', '-1');
更改memory_limit
不是正确的解决方案。 请不要这样做。您的 PHP 代码可能在某处存在内存泄漏,并且您告诉服务器只使用它想要的所有内存。 你根本就无法解决这个问题。 如果您监视服务器,您会发现它可能正在使用大部分 RAM 并交换到磁盘。
您可能应该尝试找出代码中的违规代码并修复它。
Changing the
memory_limit
byini_set('memory_limit', '-1');
is not a proper solution. Please don't do that.Your PHP code may have a memory leak somewhere, and you are telling the server to just use all the memory that it wants. You wouldn't have fixed the problem at all. If you monitor your server, you will see that it is probably using most of the RAM and swapping to disk.
You should probably try to track down the offending code in your code and fix it.
ini_set('memory_limit', '-1');
覆盖默认的 PHP 内存限制 。ini_set('memory_limit', '-1');
overrides the default PHP memory limit.正确的方法是编辑
php.ini
文件。将
memory_limit
编辑为您想要的值。从您的问题来看,已超出
128M
(这是默认限制),因此您的代码存在严重错误,因为它不应该占用那么多。如果您知道为什么需要这么多并且您希望允许它设置
memory_limit = 512M
或更高,那么您应该很好。The correct way is to edit your
php.ini
file.Edit
memory_limit
to your desire value.As from your question,
128M
(which is the default limit) has been exceeded, so there is something seriously wrong with your code as it should not take that much.If you know why it takes that much and you want to allow it set
memory_limit = 512M
or higher and you should be good.PHP 的内存分配可以永久或临时调整。
永久
您可以通过两种方式永久更改 PHP 内存分配。
如果您有权访问
php.ini
文件,则可以将memory_limit
的值编辑为您想要的值。如果您无权访问
php.ini
文件(并且您的网站主机允许),您可以通过.htaccess
文件覆盖内存分配。 添加php_value memory_limit 128M
(或任何您想要的分配)。临时
您可以在 PHP 文件中动态调整内存分配。 您只需使用代码
ini_set('memory_limit', '128M');
(或任何您想要的分配)。 您可以通过将该值设置为“-1”来删除内存限制(尽管计算机或实例限制可能仍然适用)。The memory allocation for PHP can be adjusted permanently, or temporarily.
Permanently
You can permanently change the PHP memory allocation two ways.
If you have access to your
php.ini
file, you can edit the value formemory_limit
to your desire value.If you do not have access to your
php.ini
file (and your webhost allows it), you can override the memory allocation through your.htaccess
file. Addphp_value memory_limit 128M
(or whatever your desired allocation is).Temporary
You can adjust the memory allocation on the fly from within a PHP file. You simply have the code
ini_set('memory_limit', '128M');
(or whatever your desired allocation is). You can remove the memory limit (although machine or instance limits may still apply) by setting the value to "-1".