We can temporarily increase the memory limit using this function: // Temporarily increase memory limit to 256MB ini_set('memory_limit','256M'); In my case, I needed more than 128MB of memory, so I conveniently increased it to 256MB. It depends on your usage and your hardware.
The correct way is to edit your php. ini file. Edit memory_limit to your desire value. As from your question, 128M (which is the default limit) has been exceeded, so there is something seriously wrong with your code as it should not take that much.
Increasing the PHP memory limit The default memory limit is 256M and this is usually more than sufficient for most needs. If you need to raise this limit, you must create a phprc file.
This Error happens in almost all framework like Magento, Laraval etc. You just need to increase the memory allocation limit in php by php. ini file. You can do the above task.
You can try editing /etc/php5/fpm/php.ini:
; Old Limit
; memory_limit = 512M
; New Limit
memory_limit = 2048M
You may need to restart nginx
:
sudo systemctl restart nginx
You may also have an infinite loop somewhere. Can you post the code you're calling?
It is happened to me with laravel 5.1 on php-7 when I was running bunch of unitests.
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:
/etc/php/7.0/cli/php.ini
so you need a line with
memory_limit
After that you need to restart php service
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.
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.
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.
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.
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 {
//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 ***/
}
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_limit
setting. 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.
ini_set('memory_limit', '64M');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With