DEV Community

StuartCreed
StuartCreed

Posted on • Edited on

1

How to fix php memory limit errors

If you are having composer memory limit issues

COMPOSER_MEMORY_LIMIT=-1 composer install
This will temporary remove the php memory limit. -1 (infinite memory usage) is not recommended if the server is running a site/sites that is/are live as it could overload the server. In this scenario use a different memory size e.g.COMPOSER_MEMORY_LIMIT=512M. If the server is in maintenance mode then -1 should be fine to use, just be cautious. To find your current memory limit setting run: php -i | grep "memory_limit"

Memory limit issues in scripts

Put the following
ini_set('memory_limit', '64M');

Useful commands on Mac

To find your php.ini file run:
php -i | grep "Loaded Configuration File"

or:
To find all php.ini files on your machine:
sudo find / -name php.ini
kill the process after the main process has finished using Ctrl + C.

To get the memory limit:
php -i | grep "memory_limit"

Useful commands on Linux

To view locations of all of your php ini files (on linux only - To install do: sudo apt install mlocate):
locate php.ini

php -i gives a print out of the ini php which your php is using.
To find he location of this ini file run:
php -i | grep "Loaded Configuration File"

Sometimes a there are two php.ini files -> like in Laravel Forge where there is one for the CLI and one for the FPM.
To view how much space is allocated in your CLI php ini file:
php -i | grep "memory_limit"

To view how much space is used in your FPM ini file (change to php version):
grep "memory_limit" /etc/php/7.3/fpm/php.ini

to view peak usage in a script:
var_dump(memory_get_peak_usage()/1024);

Normal directory:
cd /etc/php/7.4

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay