简体   繁体   English

PHP允许的内存大小

[英]PHP Allowed memory size

Hello! 你好!

I have a web-app running on a paid VPS. 我有一个在付费VPS上运行的网络应用。 No problem there. 没问题。 I'm moving this app to my own dedicated server. 我正在将此应用程序移到我自己的专用服务器上。

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

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

I deployed the PC with the same specs because "if it works okay with that it should work with the same". 我部署了具有相同规格的PC,因为“如果可以正常运行,那么它应该也可以运行”。

On a API's endpoint the current CS returns the correct json. 在API的端点上,当前CS返回正确的json。 The new Server returns: 新服务器返回:

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行的/var/www/api/Components/Database.php中 ,耗尽了536870912字节的允许的内存大小(尝试分配4294967296字节)

line 439 is: 439行是:

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

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. 有人说升级PHP版本,其中90%设置了更大的内存限制。 ** I did**. **我做到了**。 I set it to 256M, 512M, 2GB (beyond this there is no ram available), 4GB and 5GB. 我将其设置为256M,512M,2GB(除此之外,没有可用的ram),4GB和5GB。 ** Nada** **娜达**

This query works ok on the other -and production- server. 此查询在另一台和生产服务器上都可以正常工作。

New 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: CS:

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

I look at the LENGTH of the base64 being queried. 我看一下被查询的base64的长度。 They're 2 images data sent. 它们是发送的2张图像数据。 This size is returned by mysql: 这个大小是由mysql返回的:

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

That is the query. 那就是查询。 It returns 2 row. 它返回2行。 Image_base64 is LONGTEXT. Image_base64为LONGTEXT。 There are some others columns but it won't add to the issue. 还有其他一些专栏,但不会添加到问题中。

LENGTH(image_base64)
162678
131402

It is clearly not close to 4Gb 显然不是接近4Gb

I can't access php/apache conf on the CS. 我无法在CS上访问php / apache conf。 The only thing I didn't try yet is upgrading PHP from 5.4 to 5.5. 我唯一尚未尝试的就是将PHP从5.4升级到5.5。 Could be it? 可以吗 I'll try to get access to the server on weekend to try out any other ideas. 我将在周末尝试访问服务器,以尝试其他想法。

Edit #1 编辑#1

I update the PHP Version to 5.6.9. 我将PHP版本更新为5.6.9。

Same error: 同样的错误:

<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 />

Edit #2 编辑#2

Changing the column type from longtext to mediumtext seems to work as in this question 改变从LONGTEXT列类型MEDIUMTEXT似乎在工作这个问题

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. 只要是longtext col,它将给出错误。

Thanks! 谢谢!

4294967296 bytes sounds like really a lot. 4294967296字节听起来确实很多。 You surely have a memory leak somewhere. 您肯定在某处有内存泄漏。

You should read that : 您应该阅读

People, changing the memory_limit by ini_set('memory_limit', '-1'); 人们,通过ini_set('memory_limit','-1')更改memory_limit; is NOT a solution at all. 根本不是解决方案。

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. 显然,php在某处存在内存泄漏,您在告诉服务器只使用它想要的所有内存。 The problem has not been fixed at all 问题根本没有解决

UPDATE 更新

As explained in this bug report : 该错误报告中所述

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 . 这是使用libmysql(始终在5.2及更高版本中)以及使用5.3启用libmysql时ext / mysqli的已知限制。 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). 该长文本的最大长度为4G,并且ext / mysqli尝试使用最大长度进行绑定,以确保不会发生数据丢失(数据不适合C级别的绑定缓冲区)。

So to fix that, you have 4 solutions : 因此,要解决此问题,您有4个解决方案:

  • You can use a text or mediumtext instead of a longblob or longtext to use less memory 您可以使用textmediumtext ,而不是一个longbloblongtext使用较少的内存
  • You can use PDO connector instead of mysqli but I don't know if it's a easy thing to implement in SaskPhp 您可以使用PDO连接器代替mysqli,但我不知道在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. 您可以使用mysqli_stmt_store_result()在本地存储数据,这也将增加您的内存使用量,但实际上减少了,因为它与缓冲区大小共享。
  • You can upgrade your PHP with a version superior to 5.3. 您可以使用5.3之前的版本升级PHP。

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. 就我个人而言,我将选择第四篇,因为如果它没有强迫您重构代码的重要部分,那么拥有最新版本通常会为您带来更多优势。

As presented in this SO answer, you could try the following: SO答案中所示,您可以尝试以下操作:

ini_set('memory_limit', -1);

You should however attempt to find where the memory is going, it is always best fixing than forgetting! 但是,您应该尝试查找内存的去向,这总是比忘记最好的解决方法!

This is even more relevant in this case, seeing that you have a usage of over 4GB, that is one hell of a memory leak. 在这种情况下,这尤为重要,因为您已经使用了超过4GB的内存,这真是一次内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM