FAQ: PHP Fatal Error "Allowed Memory Size Exhausted" or "Out of memory"

Out-of-memory errors are one of the most common and hard-to-fix problem that PHP developers run into due to PHP's relatively conservative default memory settings.

FAQ: PHP Fatal Error "Allowed Memory Size Exhausted" or "Out of memory"

Occasionally, you can face "500 errors" with messages that will look like

  • Fatal error: Allowed memory size of XXX bytes exhausted (tried to allocate XXX bytes) in FILE_NAME
  • PHP Fatal error: Out of memory (allocated XXX) (tried to allocate XXX bytes) in FILE_NAME

Let's go deep into these messages that show up PHP Fatal errors.

However, most times this error message means that the script is doing something wrong and increasing the memory limit will just result in the same error message with different numbers.

Therefore, instead of increasing the memory limit, you must rewrite the code in order it doesn't allocate that much memory. For example, processing large amounts of data in smaller chunks, unsetting variables that hold large values but not needed anymore, etc.

If your script is expected to allocate the significant amount of memory, then you can increase the memory limit.

If you aren't sure what is your PHP memory limit, this figures are helpfully included in the error message. The size is reported in bytes though, so we've done some conversions for you:

PHP Fatal Error message How much memory lacks Can be enlarged?
Allowed Memory Size of 33554432 Bytes Exhausted 32mb Yes
Allowed Memory Size of 67108864 Bytes Exhausted 64mb Yes
Allowed Memory Size of 134217728 Bytes Exhausted 128mb Yes
Allowed Memory Size of 268435456 Bytes Exhausted 256mb Yes
Allowed Memory Size of 536870912 Bytes Exhausted 512mb In some cases
Allowed Memory Size of 1073741824 Bytes Exhausted 1024mb or 1G Risky
Allowed Memory Size of 2147483648 Bytes Exhausted 2048mb or 2G Risky

The goal is to increase the memory limit to a point where we have the application working again and only then reduce the memory usage. Once you decrease the memory usage you can safely lower the memory limit to a value that's more suitable for your project. Your plan should be to use as little memory as you can provided that your application works and functions correctly in a production server based on the workload caused by your users (humans or programmatic). I usually recommend setting the memory limit to a bit higher value, like 1GB, assuming you have at least 150% of that free in RAM.

Also, never do these tests on a production server unless you're sure you have plenty of RAM and you fully understand how web server processes consume memory. You could easily bring a server to its knees if there are many concurrent processes running, each using a high amount of memory. I would never, ever recommend setting the memory limit to -1 (unlimited) in a production environment. That's a recipe for disaster. Don't make that newbie mistake.

How to increase PHP memory_limit

To round up I'd like to demonstrate a real example of what happerns if you increase the parameters "to the maximum possible" or remove them altogether after the "out of memory" message. Spoiler: Nothing good 😕

[root@REDACTED ~]# nano /etc/php.ini
-bash: fork: Cannot allocate memory
[root@REDACTED ~]# nano /etc/php.ini
nano: error while loading shared libraries: libtinfo.so.6: failed to map segment from shared object
[root@REDACTED ~]# nano /etc/php.ini
^C
[root@REDACTED ~]# systemctl restart php-fpm
systemctl: error while loading shared libraries: libgpg-error.so.0: failed to map segment from shared object
[root@REDACTED ~]# systemctl restart php-fpm
systemctl: error while loading shared libraries: libcryptsetup.so.12: failed to map segment from shared object
[root@REDACTED ~]# systemctl restart php-fpm


^C
[root@REDACTED ~]# systemctl restart php-fpm
^C
[root@REDACTED ~]# nano /etc/php.ini
nano: error while loading shared libraries: libdl.so.2: failed to map segment from shared object
[root@REDACTED ~]# nano /etc/php.ini
Segmentation fault
[root@REDACTED ~]# nano /etc/php.ini
^X^C
[root@REDACTED ~]# pkill -9 php-fpm
[root@REDACTED ~]# 

To be on a safe side I strongly recomment from time to time to make deep examinations of your current server infrastructure to find possible bottlenecks and make preventive works before they cause unavailability of your project. Especially of you have a growing website with lots of customizations, integrations and third-party add-ons.