Increase PHP Memory Limit for your computer

Published in bloggingcoding on Jul 15, 2020

featured image

If you are running PHP and at any time received an error similar to this one:

PHP Fatal error:  Allowed memory size of 1610612736 bytes exhausted 
(tried to allocate 4096 bytes)

This is a clear error message on PHP, the current PHP allocated memory is totally used (exhausted), and PHP is running out of memory, so it cannot complete the current operation.

To solve this issue, you will need to update thememory_limit configuration on the php.ini file. This is a pretty straightforward operation, but you need to locate the correct file.

The easiest way is to run this command:

php --ini

This will print all the ini configuration files that the current PHP installation on your system uses. Below is the example output from the default PHP 7.4 homebrew install.

Configuration File (php.ini) Path: /usr/local/etc/php/7.4
Loaded Configuration File:         /usr/local/etc/php/7.4/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.4/conf.d
Additional .ini files parsed:      /usr/local/etc/php/7.4/conf.d/ext-opcache.ini,

Usually, you’d go to the first php.ini file and find the configuration parameter and change it. However, in our case here, you need actually to find the php-memory-limits.ini. This is a file with 3 configuration parameters. Here is a sample:

; Max memory per instance
memory_limit = 128M

;The maximum size of an uploaded file.
upload_max_filesize = 128M
;Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be     larger than upload_max_filesize
post_max_size = 128M

The easiest way to fix this issue is to set the memory_limit = -1, which means PHP can allocate as much memory as it needs, and the OS can avail. This is not something you can do in a production environment. But in your local development environment, you can do that change. I still advise with increasing it to a specific value, 1024M for example should be enough.

The last step is to restart the PHP service. This depends greatly on how to you have installed PHP, and for homebrew, this how to restart PHP:

sudo brew services restart php

I hope that was helpful. If you have any problems or questions, feel free to reach out @abadir_.