Laravel 中允许的内存大小为 536870912 字节

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

Allowed memory size of 536870912 bytes exhausted in Laravel

laravel

提问by Juliatzin

In the same system, I can make call to db, and there is no problem, but in some case ( with the biggest table ), I get

在同一个系统中,我可以调用db,没有问题,但在某些情况下(最大的表),我得到

"PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 32 bytes) in /home/forge/sximo.sp-marketing.com/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 311

“PHP 致命错误:在 /home/forge/sximo.sp-marketing.com/vendor/laravel/framework/src/Illuminate/Database/Connection.php 中,允许的内存大小为 536870912 字节耗尽(试图分配 32 字节) 311

I debugged the code and the problem is a basic query:

我调试了代码,问题是一个基本查询:

"  SELECT partidascapturainfo.* FROM partidascapturainfo    WHERE partidascapturainfo.partidascapturainfoid IS NOT NULL       ORDER BY partidascapturainfoid asc   LIMIT  0 , 10 "

When I run the query in a Mysql Client, query runs in 0.17s

当我在 Mysql 客户端中运行查询时,查询运行时间为 0.17 秒

I've already set memory_limit to 2048, restart nginx and my query only return 10 rows...

我已经将 memory_limit 设置为 2048,重新启动 nginx 并且我的查询只返回 10 行...

Here are my 10 rows:

这是我的 10 行:

123044,42016,249,3762,2,,0
123045,42016,249,3761,2,,0
123046,42016,249,3764,1,,0
123047,42016,249,3765,,,0
123048,42016,249,3775,,,0
123049,42016,249,3771,3,,0
123050,42016,249,3772,3,,0
123051,42016,250,3844,HAY,,0
123052,42016,255,3852,,,0
123053,42017,249,3761,1,,0

Any Idea what's going on???

知道发生了什么吗???

回答by Deciple

You can try editing /etc/php5/fpm/php.ini:

您可以尝试编辑/etc/php5/fpm/php.ini:

; Old Limit
; memory_limit = 512M

; New Limit
memory_limit = 2048M

You may need to restart nginx

您可能需要重新启动 nginx

You may also have an infinite loop somewhere. Can you post the code you're calling?

您也可能在某处有一个无限循环。你能发布你正在调用的代码吗?

回答by Yevgeniy Afanasyev

It is happened to me with laravel 5.1 on php-7 when I was running bunch of unitests.

当我运行一堆 unitest 时,我在 php-7 上使用 laravel 5.1 发生了这种情况。

The solution was - to change memory_limit in php.ini but it should be correct one. So you need one responsible for server, located there:

解决方案是 - 在 php.ini 中更改 memory_limit 但它应该是正确的。所以你需要一个负责服务器,位于那里:

/etc/php/7.0/cli/php.ini

so you need a line with

所以你需要一条线

 memory_limit

After that you need to restart php service

之后你需要重启php服务

sudo service php7.0-fpm restart

to check if it was changed successfully I used command line to run this:

要检查它是否已成功更改,我使用命令行来运行它:

 php -i

the report contained following line

该报告包含以下行

memory_limit => 2048M => 2048M

Now test cases are fine.

现在测试用例没问题。

回答by fatemeh sadeghi

This problem occurred to me when using nested try- catch and using the $ex->getPrevious() function for logging exception .mabye your code has endless loop. So you first need to check the code and increase the size of the memory if necessary

我在使用嵌套 try-catch 并使用 $ex->getPrevious() 函数记录异常时发生了这个问题。mabye 您的代码有无限循环。所以你首先需要检查代码并在必要时增加内存的大小

 try {
        //get latest product data and latest stock from api
        $latestStocksInfo = Product::getLatestProductWithStockFromApi();
    } catch (\Exception $error) {
        try {
            $latestStocksInfo = Product::getLatestProductWithStockFromDb();
        } catch (\Exception $ex) {
            /*log exception */
            Log::channel('report')->error(['message'=>$ex->getMessage(),'file'=>$ex->getFile(),'line'=>$ex->getLine(),'Previous'=>$ex->getPrevious()]);///------------->>>>>>>> this problem when use 
            Log::channel('report')->error(['message'=>$ex->getMessage(),'file'=>$ex->getFile(),'line'=>$ex->getLine()]);///------------->>>>>>>> this code is ok 
        }
        Log::channel('report')->error(['message'=>$error->getMessage(),'file'=>$error->getFile(),'line'=>$error->getLine()]);

        /***log exception ***/
    }

回答by StevenHill

I realize there is an accepted answer, and apparently it was either the size of memory chosen or the infinite loop suggestion that solved the issue for the OP.

我意识到有一个可接受的答案,显然是选择的内存大小或无限循环建议解决了 OP 的问题。

For me, I added an array to the config file earlier and made some other changes prior to running artisan and getting the out of memory error and no amount of increasing memory helped. What it turned out to be was a missing comma after the array I added to the config file.

对我来说,我之前在配置文件中添加了一个数组,并在运行 artisan 之前进行了一些其他更改,并解决了内存不足错误,并且没有增加内存的帮助。结果是在我添加到配置文件的数组之后丢失了一个逗号。

I am adding this answer in hopes that it helps someone else figure out what might be causing out of memory error. I am using laravel 5.4 under MAMP.

我正在添加这个答案,希望它可以帮助其他人找出可能导致内存不足错误的原因。我在 MAMP 下使用 laravel 5.4。

回答by Dylan Glockler

I got this error when I restored a database and didn't add the user account and privileges back in. Another site gave me an authentication error, so I didn't think to check that, but as soon as I added the user account back everything worked again!

当我恢复数据库并且没有重新添加用户帐户和权限时,我遇到了这个错误。另一个站点给了我一个身份验证错误,所以我没想过要检查,但是一旦我重新添加了用户帐户一切正常!

回答by Alec Walczak

Share the lines of code executed when you make this request. There might be an error in your code.

共享发出此请求时执行的代码行。您的代码中可能存在错误。

Also, you can change the memory limit in your php.ini file via the memory_limitsetting. Try doubling your memory to 64M. If this doesn't work you can try doubling it again, but I'd bet the problem is in your code.

此外,您可以通过memory_limit设置更改 php.ini 文件中的内存限制。尝试将内存加倍到 64M。如果这不起作用,您可以尝试再次加倍,但我敢打赌问题出在您的代码中。

ini_set('memory_limit', '64M');

回答by GarryOne

I had the same problem. No matter how much I was increasing memory_limit (even tried 4GB) I was getting the same error, until I figured out it was because of wrong database credentials setted up in .envfile

我有同样的问题。无论我增加多少 memory_limit(甚至尝试了 4GB),我都会遇到同样的错误,直到我发现这是因为.env文件中设置了错误的数据库凭据

回答by Keith Turkowski

I had this problem when trying to resize a CMYK jpeg using the Intervention / gd library. I had to increase the memory_limit.

我在尝试使用 Intervention / gd 库调整 CMYK jpeg 的大小时遇到​​了这个问题。我不得不增加memory_limit。

回答by CHARITRA SHRESTHA

I had also been through that problem. in my case, I was adding the data to the array and passing the array to the same array which brings the problem of memory limits. Some of the things you need to consider:

我也遇到过这个问题。就我而言,我将数据添加到数组并将数组传递给同一个数组,这带来了内存限制问题。您需要考虑的一些事项:

  1. Review our code, look if any loop is running infinity.

  2. Reduce the unwanted column if you are retrieving the data from the database.

  3. Maybe you can increase the memory limits in our XAMPP other any other software you are running.

  1. 查看我们的代码,看看是否有任何循环无限运行。

  2. 如果您从数据库中检索数据,请减少不需要的列。

  3. 也许您可以在我们的 XAMPP 其他您正在运行的任何其他软件中增加内存限制。

回答by Mitch M

You can also get this error if you fail to include .htaccess or have a problem in it. The file should be something like this

如果您未能包含 .htaccess 或其中有问题,您也会收到此错误。该文件应该是这样的

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews -Indexes
</IfModule>

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]