PHP允许的内存大小 [英] PHP Allowed memory size

查看:107
本文介绍了PHP允许的内存大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我有一个运行在付费VPS上的网络应用程序.没问题.我正在将该应用程序移到我自己的专用服务器上.

I have a web-app running on a paid VPS. No problem there. I'm moving this app to my own dedicated server.

当前的云服务器-CS-: Centos 6 x86_64; 2 Gb Ram; 2个vCPU

Current Cloud Server -CS-: Centos 6 x86_64; 2 Gb Ram; 2 vCPU

在专用服务器上虚拟: Centos 7 x86_64; 2 Gb Ram; 2个vCPU

Virtual on Dedicated Server: Centos 7 x86_64; 2 Gb Ram; 2 vCPU

我以相同的规格部署了PC,因为如果可以的话,它应该可以在相同的条件下工作."

I deployed the PC with the same specs because "if it works okay with that it should work with the same".

在API的端点上,当前CS返回正确的json.新服务器返回:

On a API's endpoint the current CS returns the correct json. The new Server returns:

致命错误:在/var/www/api/Components/Database.php 中,在 439

Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 4294967296 bytes) in /var/www/api/Components/Database.php on line 439

第439行是:

call_user_func_array(array($stmt, 'bind_result'), $parameters);

我在这里和那里找到的搜索结果没有帮助.有人说升级PHP版本,其中90%设置了更大的内存限制. **我做到了**.我将其设置为256M,512M,2GB(除此之外,没有可用的ram),4GB和5GB. **娜达(Nada)**

The search results I found here and there were not helpful. Some said upgrade PHP version and 90% of them is set a larger memory limit. ** I did**. I set it to 256M, 512M, 2GB (beyond this there is no ram available), 4GB and 5GB. ** Nada**

此查询在另一台和生产服务器上都可以正常工作.

This query works ok on the other -and production- server.

新服务器:

Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.1e-fips mod_fcgid/2.3.9        

PHP/5.4.16 mod_wsgi/3.4 Python/2.7.5 
X-Powered-By: PHP/5.4.16

CS:

 Server: Apache 
 X-Powered-By: PHP/5.5.22

我查看正在查询的base64的长度.它们是发送的2张图像数据.此大小由mysql返回:

I look at the LENGTH of the base64 being queried. They're 2 images data sent. This size is returned by mysql:

select LENGTH(image_base64) from pmv where pmv.model = 1;

那是查询.它返回2行. Image_base64为LONGTEXT.还有其他一些专栏,但不会添加到问题中.

That is the query. It returns 2 row. Image_base64 is LONGTEXT. There are some others columns but it won't add to the issue.

LENGTH(image_base64)
162678
131402

显然不是接近4Gb

我无法在CS上访问php/apache conf.我唯一没有尝试过的就是将PHP从5.4升级到5.5.可以吗我将尝试在周末访问服务器,以尝试其他想法.

I can't access php/apache conf on the CS. The only thing I didn't try yet is upgrading PHP from 5.4 to 5.5. Could be it? I'll try to get access to the server on weekend to try out any other ideas.

编辑#1

我将PHP版本更新为5.6.9.

I update the PHP Version to 5.6.9.

相同错误:

<b>Fatal error</b>:  Allowed memory size of 536870912 bytes exhausted (tried to allocate 4294967296 bytes) in <b>/var/www/api/Components/Database.php</b> on line <b>439</b><br />

编辑#2

将列类型从 longtext 更改为 mediumtext 似乎可以像

Changing the column type from longtext to mediumtext seems to work as in this question

但是为什么我实际上需要更改此服务器上的列类型?到目前为止,无论该列上存储了多少信息,我现在都可以进行测试.只要是长文本列,它就会给出错误.

But why on earth I need to change the column type on this server? As far I can test now, no matter how much info is stored on that column. It will give the error as long as it's a longtext col.

谢谢!

推荐答案

4294967296字节听起来确实很多.您肯定在某处内存泄漏.

4294967296 bytes sounds like really a lot. You surely have a memory leak somewhere.

您应该阅读:

人们,通过ini_set('memory_limit','-1')更改memory_limit;根本不是解决方案.

People, changing the memory_limit by ini_set('memory_limit', '-1'); is NOT a solution at all.

请不要那样做.显然,php在某处存在内存泄漏,您在告诉服务器只使用它想要的所有内存.问题根本没有解决

Please don't do that. Obviously php has a memory leak somewhere and you are telling the server to just use all the memory that it wants. The problem has not been fixed at all

更新

此错误报告中所述:

这是使用libmysql(始终在5.2及更高版本中)以及使用5.3启用libmysql时ext/mysqli的已知限制.原因是服务器发送的列元数据不太具体.该长文本的最大长度为4G,并且ext/mysqli尝试以最大长度进行绑定,以确保不会发生数据丢失(数据不适合C级别的绑定缓冲区).

This is a known limitation of ext/mysqli when using libmysql (always in 5.2 and previous) and when libmysql is enabled with 5.3 . The reason is that the server sends not too specific metadata about the column. This longtext has a max length of 4G and ext/mysqli tries to bind with the max length, to be sure no data loss occurs (data doesn't fit in the bind buffer on C level).

要解决此问题,您有4个解决方案:

So to fix that, you have 4 solutions :

  • 您可以使用textmediumtext代替longbloblongtext来减少内存用量
  • 您可以使用PDO连接器代替mysqli,但我不知道在SaskPhp中实现它是否容易
  • 您可以使用 mysqli_stmt_store_result()来存储您本地数据,这也会增加您的内存使用量,但由于与缓冲区大小共享,因此实际使用量会减少.
  • 您可以使用5.3之前的版本升级PHP.
  • You can use a text or mediumtext instead of a longblob or longtext to use less memory
  • You can use PDO connector instead of mysqli but I don't know if it's a easy thing to implement in SaskPhp
  • You can use mysqli_stmt_store_result() to store you data locally, which will also increase your memory usage but really less since it's shared with the buffer size.
  • You can upgrade your PHP with a version superior to 5.3.

就个人而言,我会选择第四个,因为如果它没有强制您重构代码的重要部分,那么拥有最新版本通常会为您带来更多优势.

Personally, I would go for the fourth one since having an recent version generally bring you many more advantages if it doesn't oblige you to refactor significant parts of your code.

这篇关于PHP允许的内存大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆