How to Resolve Memory Shortage Issue When EC2, Laravel, and Queue Processes Stop
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
Allowed memory size of 134217728 bytes exhausted (tried to allocate 1638400 bytes)
This article talks about what to check when such an error occurs.
Environment
EC2
Laravel
supervisor
In this case, it occurred when running heavy tasks asynchronously using Queue.
Conclusion
It is possible that you exceeded the amount of memory set in the php.ini configuration file.
This error can occur not only with queues but also with actions like composer install.
Solution
Find where php.ini is located
php --ini
By running this command.
In my case, I installed php7.4, so I can find it by running:
php74 --ini
and it will show:
Loaded Configuration File: /etc/opt/remi/php74/php.ini
This is the location of php.ini.
When you don't know the PHP version
You can use the function phpinfo()
to check various information.
If it's a development or test environment, this is the fastest way to find out (never do this in a production environment).
Write the following in web.php
Route::get('aaaaaaa', function () {
dd(phpinfo());
return redirect()->route('login');
});
Accessing http://????/aaaaaaa will show you the information.
Look for the location of the php.ini file in the information.
In this case, it was found in:
/etc/opt/remi/php74/php.ini
Edit the file
Open the file location you found earlier with vim.
sudo vim /etc/opt/remi/php74/php.ini
You'll see:
memory_limit = 128M
Change it to:
memory_limit = 256M
In vim, you can search with:
/memory
After editing, save with:
/wq
⇨Basic Vim Commands for EC2 and other uses
Restart Apache
One-line command to restart Apache.
sudo systemctl restart httpd
Don't forget to restart supervisor
Memory shortage issues in queues won't be resolved until you restart them (easy to forget).
supervisorctl restart all
The queue has been restarted.
Summary
These are the steps to resolve memory shortage issues.
I wasted hours because I forgot to restart the supervisor.
And yet, the article is just three lines long!!!
Please be careful, everyone. That's all.