Laravel 内存:'内存不足(已分配 262144)'

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

Laravel Memory: 'out of memory (allocated 262144)'

phpapacheapilaravel

提问by Drew

I'm a system administrator which handles a server that hosts an internal api created using Laravel 5. The data source is a SQL Server hosted on another server and the api is used in a IOS app for mobile phones. When the developers created the api, this error was not present and no other memory errors were found. When we went live this error has been popping up from time to time.

我是一名系统管理员,负责处理托管使用 Laravel 5 创建的内部 api 的服务器。数据源是托管在另一台服务器上的 SQL Server,该 api 用于手机的 IOS 应用程序。开发人员创建api时,没有出现这个错误,也没有发现其他内存错误。当我们上线时,这个错误不时出现。

[2015-06-01 23:01:52] production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Out of memory (allocated 262144) (tried to allocate 140189808036120 bytes)' in Unknown:0

I checked RAM, CPU, swap etc etc of the server but everything was ok. Not much of the resources are being consumed. I've checked PHP Memory limit from php.ini and is set to -1 which is the max according to PHP. I've checked it on CLI, phpinfo.php and httpd.conf I don't see anything wrong. The team tried following this link Allowed memory size of X bytes exhausted

我检查了服务器的 RAM、CPU、交换等,但一切正常。消耗的资源并不多。我已经从 php.ini 检查了 PHP 内存限制,并设置为 -1,这是根据 PHP 的最大值。我已经在 CLI、phpinfo.php 和 httpd.conf 上检查过了,我没有发现任何问题。该团队尝试按照此链接允许 X 字节的内存大小耗尽

This is a problem in the ios app because the app searches for a correct return code but when this error comes out, even if the ios app transaction is correct, it returns an error because the return code is incorrect as it is returning the code for the memory error.

这是ios应用程序中的一个问题,因为应用程序搜索正确的返回码但是当这个错误出现时,即使ios应用程序事务正确,它也会返回错误,因为返回码不正确,因为它正在返回代码内存错误。

回答by Anilkumar S.K

The first thing I could think of was to restart the Apache httpd service. This immediately solved the issue. but I knew this is not a permanent fix for the issue. When I researched further, I got to know that the error comes when certain PHP scripts require more memory than PHP was allowed by default.

我能想到的第一件事就是重新启动Apache httpd 服务。这立即解决了这个问题。但我知道这不是该问题的永久解决方案。当我进一步研究时,我知道当某些 PHP 脚本需要比默认情况下允许的 PHP 更多的内存时会出现错误。

So the solution is to increase the memory allocated for PHP. How to do that? There are 4 possible ways –

所以解决办法就是增加给PHP分配的内存。怎么做?有4种可能的方式——

  1. Try looking for the php.ini file. You might find some redundant php.ini files, so make sure you have got the one which is actually being read by PHP. o be sure, create a new php file in your root folder, say “check.php” and have phpInfo(); within the php open and close tags. Execute this file to get the information on where the php.ini is residing. Normally it will be in /usr/local/lib/php.ini
  1. 尝试查找 php.ini 文件。您可能会发现一些多余的 php.ini 文件,因此请确保您已获得 PHP 实际正在读取的文件。o 确保在你的根文件夹中创建一个新的 php 文件,说“check.php”并使用 phpInfo();在 php 打开和关闭标签中。执行此文件以获取有关 php.ini 所在位置的信息。通常它会在 /usr/local/lib/php.ini

Open the php.ini file in a text editor like TextPad (not in Notepad) and change the values for memory_limit. By default you should see memory_limit = 8M. Try changing it to 12M. If it doesn't work, increase it to 16M or even 24M and so on.

在文本编辑器(如 TextPad(不是记事本))中打开 php.ini 文件并更改 memory_limit 的值。默认情况下,您应该看到 memory_limit = 8M。尝试将其更改为 12M。如果不行,就增加到16M甚至24M等等。

  1. In case you can't find the php.ini file or do not have access to it, then open up the file which was throwing the error (admin.php in my case) and add a line below just after ini_set('memory_limit', ‘12M');

  2. You can even consider adding a line in .htaccess file which will resolve the issue. php_value memory_limit 32M

  3. Or else, Try adding this line to your php file: Increasing memory allocated to PHP ini_set(“memory_limit”,”16M“);

  1. 如果您找不到 php.ini 文件或无法访问它,请打开抛出错误的文件(在我的情况下为 admin.php)并在 ini_set('memory_limit' , '12M');

  2. 您甚至可以考虑在 .htaccess 文件中添加一行来解决问题。php_value memory_limit 32M

  3. 或者,尝试将这一行添加到您的 php 文件中:增加分配给 PHP ini_set(“memory_limit”,”16M“) 的内存;