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

客户机POS基于PHPPOS,我实现了一个模块,该模块使用标准XML-RPC库将销售数据发送到服务。服务器系统构建在CodeIgniter上,并为webservice组件使用XML-RPC和XML-RPC库。每当我发送大量的销售数据(从销售表中只有50行,从sales_items中单独的行用于销售中的每个项目),我得到以下错误:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 54 bytes)

128M是php.ini中的默认值,但我认为这是一个巨大的数字。事实上,我甚至尝试过将这个值设置为1024M,它所做的只是花费更长的时间来出错。

至于我所采取的步骤,我已经尝试在服务器端禁用所有处理,并对其进行了修改,使其返回一个罐装响应,而不管输入是什么。但是,我认为问题出在数据的实际发送上。我甚至尝试过禁用PHP的最大脚本执行时间,但它仍然出错。


当前回答

在PHP脚本中很容易发生内存泄漏——特别是在使用抽象(如ORM)时。尝试使用Xdebug分析脚本并找出所有内存的去向。

其他回答

如果你正在运行一个whm驱动的VPS(虚拟专用服务器),你可能会发现你没有权限直接编辑PHP.INI;系统必须这样做。在WHM主机控制面板,进入“服务配置→PHP配置编辑器”,修改memory_limit:

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

; 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

在PHP脚本中很容易发生内存泄漏——特别是在使用抽象(如ORM)时。尝试使用Xdebug分析脚本并找出所有内存的去向。

你可以通过修改fastcgi/fpm的memory_limit来解决这个问题:

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

更改内存,例如从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

只需添加一个ini_set('memory_limit', '-1');在你的网页顶部的行。

并且您可以根据需要将内存设置在-1的位置,设置为16M,等等。