php 致命错误:允许的内存大小为 134217728 字节已用尽(CodeIgniter + XML-RPC)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/561066/
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 23:05:34  来源:igfitidea点击:

Fatal Error: Allowed Memory Size of 134217728 Bytes Exhausted (CodeIgniter + XML-RPC)

phpcodeignitermemoryxml-rpcmemory-limit

提问by ArcticZero

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.

我有一堆客户销售点 (POS) 系统,它们定期将新的销售数据发送到一个集中式数据库,该数据库将数据存储到一个大数据库中以生成报告。

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:

客户端 POS 基于 PHPPOS,我已经实现了一个模块,该模块使用标准的 XML-RPC 库将销售数据发送到服务。服务器系统建立在 CodeIgniter 之上,并为 webservice 组件使用 XML-RPC 和 XML-RPCS 库。每当我发送大量销售数据(销售表中的 50 行,以及 sales_items 中与销售中的每个项目相关的单个行)时,我都会收到以下错误:

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.

128M 是 中的默认值php.ini,但我认为这是一个很大的数字。事实上,我什至尝试将这个值设置为 1024M,但它所做的只是需要更长的时间才能出错。

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.

至于我采取的步骤,我尝试禁用服务器端的所有处理,并操纵它返回一个固定的响应,而不管输入如何。但是,我认为问题在于数据的实际发送。我什至尝试禁用 PHP 的最大脚本执行时间,但它仍然出错。

回答by Jeff

Changing the memory_limitby ini_set('memory_limit', '-1');is nota proper solution. Please don't do that.

改变memory_limitini_set('memory_limit', '-1');不是一个妥善的解决办法。请不要那样做。

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 now probably using up most of the RAM and even swapping to disk.

您的 PHP 代码可能在某处存在内存泄漏,并且您告诉服务器只使用它想要的所有内存。你根本就不会解决这个问题。如果您监控您的服务器,您会发现它现在可能已经用完了大部分 RAM,甚至正在交换到磁盘。

You should probably try to track down the offending code in your code and fix it.

您可能应该尝试追踪代码中的违规代码并修复它。

回答by Chris Lane

ini_set('memory_limit', '-1');overrides the default PHP memory limit.

ini_set('memory_limit', '-1');覆盖默认的PHP 内存限制

回答by Basav

The correct way is to edit your php.inifile. Edit memory_limitto your desire value.

正确的方法是编辑您的php.ini文件。编辑memory_limit您的期望值。

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.

从您的问题来看,128M(这是默认限制)已超出,因此您的代码存在严重错误,因为它不应该花费那么多。

If you know why it takes that much and you want to allow it set memory_limit = 512Mor higher and you should be good.

如果您知道为什么需要这么多,并且您希望允许它设置memory_limit = 512M或更高,那么您应该很好。

回答by Umair Idrees

The memory allocation for PHP can be adjusted permanently, or temporarily.

PHP 的内存分配可以永久或临时调整。

Permanently

永久

You can permanently change the PHP memory allocation two ways.

您可以通过两种方式永久更改 PHP 内存分配。

If you have access to your php.inifile, you can edit the value for memory_limitto your desire value.

如果您有权访问您的php.ini文件,您可以将 的值编辑为memory_limit您想要的值。

If you do not have access to your php.inifile (and your webhost allows it), you can override the memory allocation through your .htaccessfile. Add php_value memory_limit 128M(or whatever your desired allocation is).

如果您无权访问您的php.ini文件(并且您的虚拟主机允许),您可以通过您的.htaccess文件覆盖内存分配。添加php_value memory_limit 128M(或任何您想要的分配)。

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".

您可以从 PHP 文件中动态调整内存分配。您只需拥有代码ini_set('memory_limit', '128M');(或任何您想要的分配)。您可以通过将值设置为“-1”来移除内存限制(尽管机器或实例限制可能仍然适用)。

回答by troelskn

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.

在 PHP 脚本中很容易出现内存泄漏 - 特别是如果您使用抽象,例如 ORM。尝试使用 Xdebug 来分析您的脚本并找出所有内存的去向。

回答by JamesAD-0

When adding 22.5 million records into an array with array_push I kept getting "memory exhausted" fatal errors at around 20M records using 4Gas the memory limit in file php.ini. To fix this, I added the statement

当使用 array_push 将 2250 万条记录添加到数组中时,我一直在使用4G文件 php.ini 中的内存限制在大约 20M 条记录处出现“内存耗尽”致命错误。为了解决这个问题,我添加了语句

$old = ini_set('memory_limit', '8192M');

at 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.

在文件的顶部。现在一切正常。不知道PHP有没有内存泄漏。那不是我的工作,我也不关心。我只需要完成我的工作,这很有效。

The program is very simple:

该程序非常简单:

$fh = fopen($myfile);
while (!feof($fh)) {
    array_push($file, stripslashes(fgets($fh)));
}
fclose($fh);

The fatal error points to line 3 until I boosted the memory limit, which eliminated the error.

致命错误指向第 3 行,直到我提高了内存限制,从而消除了错误。

回答by Danny Beckett

I kept getting this error, even with memory_limitset in php.ini, and the value reading out correctly with phpinfo().

我不断收到此错误,即使memory_limit设置为php.ini,并且使用phpinfo().

By changing it from this:

通过改变它:

memory_limit=4G

To this:

对此:

memory_limit=4096M

This rectified the problem in PHP 7.

这解决了 PHP 7 中的问题。

回答by Kristen Waite

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:

当您看到上述错误时——尤其是当(tried to allocate __ bytes)是一个低值时,这可能是一个无限循环的指标,就像一个调用自身而没有出路的函数:

function exhaustYourBytes()
{
    return exhaustYourBytes();
}

回答by Prem Kumar Maurya

After enabling these two lines, it started working:

启用这两行后,它开始工作:

; Determines the size of the realpath cache to be used by PHP. This value should
; be increased on systems where PHP opens many files to reflect the quantity of
; the file operations performed.
; http://php.net/realpath-cache-size
realpath_cache_size = 16k

; Duration of time, in seconds for which to cache realpath information for a given
; file or directory. For systems with rarely changing files, consider increasing this
; value.
; http://php.net/realpath-cache-ttl
realpath_cache_ttl = 120

回答by Derick Fynn

You can properly fix this by changing memory_limiton fastcgi/fpm:

您可以通过更改memory_limitfastcgi/fpm来正确解决此问题:

$vim /etc/php5/fpm/php.ini

Change memory, like from 128 to 512, see below

更改内存,比如从 128 到 512,见下文

; Maximum amount of memory a script may consume (128 MB)
; http://php.net/memory-limit
memory_limit = 128M

to

; Maximum amount of memory a script may consume (128 MB)
; http://php.net/memory-limit
memory_limit = 512M