Skip to content

Troubleshooting guide

PHP "Allowed memory size exhausted": how to fix it

A PHP request tried to allocate more than `memory_limit` permits. The line in the error is where memory ran out, which may not be where it was used.

3 min read10 sectionsUpdated

On this page

Key points

  • The named line is only the last allocation.
  • CLI and FPM use separate php.ini files.
  • Loading all rows into an array is the top cause.
  • The limit is per request, so it multiplies by workers.

What it means

The message gives bytes allowed and bytes attempted. A request that tried a tiny allocation and failed has been growing for a while. One that tried to allocate a huge block points straight at the file or query responsible.

Common causes

  • Fetching all rows from a large table into memory.
  • Image processing on large uploads.
  • Plugins building huge structures in WordPress.
  • Infinite loops or recursion appending to an array.
  • Reading large files with file_get_contents.

Diagnose it

Confirm the limit that applies to your execution environment.

bash
# CLI value (may differ from the web server)
php -i | grep memory_limit

# Which ini files are loaded
php --ini

# Web value: temporary script; remove afterwards
# <?php echo ini_get('memory_limit'), ' ', php_sapi_name();

# Peak memory in your own code:
# error_log(memory_get_peak_usage(true) / 1048576 . ' MB');

sudo tail -n 40 /var/log/php*-fpm.log

Fixes

Process data in chunks, with generators or framework helpers such as Laravel's chunkById or cursor. Stream files rather than loading them. Resize images with sensible maximum dimensions. Raise memory_limit only for what needs it, for example in the CLI config for batch jobs. On WordPress, set WP_MEMORY_LIMIT. Avoid -1 (unlimited) on web requests. In Composer, COMPOSER_MEMORY_LIMIT=-1 is the usual remedy for the CLI case.

Do the worker arithmetic

Memory limits multiply. With memory_limit=512M and 50 FPM workers, the theoretical total is 25 GB. Most requests stay far below the limit, so real usage is much lower, but a burst of expensive requests can push the host into swapping or the OOM killer. Raising the limit also lets a true memory bug run longer, so that it damages more before failing. If you find you must keep raising it, profile with Xdebug or Blackfire and find out what is actually allocating.

Catch it automatically

Alert on fatal errors in log management, and track peak memory by endpoint in PHP monitoring.

Frequently asked questions

What is a good memory_limit?

256 MB is common for applications. CMS sites with plugins may need more.

Why did changing php.ini not help?

You probably edited the CLI file instead of the FPM one, or did not reload FPM.

Is memory_limit -1 safe?

Not for web requests. It is sometimes acceptable for trusted CLI jobs.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.