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: