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
# 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.logFixes
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.
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.
Related reading
- JavaScript heap out of memory: how to fix it in Node.js: Fix FATAL ERROR: JavaScript heap out of memory. Set --max-old-space-size correctly, handle build-time failures and find runtime leaks with heap snapshots.
- 405 Method Not Allowed: why it happens and how to fix it: Fix 405 Method Not Allowed: POST sent to a static file, redirects that turn POST into GET, CORS preflights and routing mismatches, with curl checks.
- Cloudflare error 1016 "Origin DNS error": how to fix it: Fix Cloudflare error 1016 Origin DNS error: CNAME records pointing to names that do not resolve, deleted origins and load balancer pools, with checks.
- 429 Too Many Requests: what it means and how to fix it: 429 Too Many Requests explained: rate limits, the Retry-After header, backoff with jitter for clients, and how to set fair rate limits on your own API.
- MySQL server has gone away: causes and how to fix it: Fix MySQL error 2006 server has gone away: idle timeouts, oversized packets, server restarts and connection pools holding connections that have died.
- 400 Bad Request: what it means and how to fix it: Fix a 400 Bad Request error: oversized cookies, malformed URLs, invalid JSON bodies and proxy rules, with commands to find which layer rejected it.
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.