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

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

Hosting
Antony Atkins

June 22, 2022
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.

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.

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.

What does the error "Fatal error: Allowed memory size of XXX bytes exhausted" mean?
This error indicates that the script tried to use more memory than the PHP limit set on the server. It usually signals an issue where the code is consuming too much memory.

How can I fix this error?
Instead of simply increasing the memory limit, it's better to review your code to make it more memory-efficient. For example, process large datasets in smaller chunks or unset variables that are no longer needed.

When should I increase the PHP memory limit?
If your script genuinely requires more memory to function properly (e.g., processing large amounts of data), you can increase the memory limit. However, this should be done cautiously, especially on production servers.

How can I check the current PHP memory limit?
The memory limit is included in the error message. It’s provided in bytes, but you can convert it to megabytes or gigabytes for easier understanding.

Is increasing the memory limit a good long-term solution?
No, increasing the memory limit should be a temporary fix. Once your application is running again, focus on optimizing the code to reduce memory usage and then lower the memory limit to a more appropriate level.

What are the risks of setting the memory limit too high?
Setting the memory limit too high can lead to excessive memory consumption, potentially crashing the server, especially with multiple concurrent processes. It’s important to avoid setting the memory limit to unlimited (-1) in a production environment.

[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 ~]# 
Table of contents
By clicking Submit, you agree with Privacy Policy
Keep up to date with Scalesta and join our newsletter
Related posts
By clicking Send, you agree with Privacy Policy
Let's get started!
Ready to elevate your online presence with Scalesta hosting solutions?
Transform your operations with expert DevOps services