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

Image of Bright Data

Feed Your Models Real-Time Data – Enhance your AI with up-to-the-minute data.

Utilize our live data feeds for dynamic model training, ensuring your AI systems are always ahead.

Access Real-Time Data

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay