PHP Allowed Memory Size Exhausted Fix

PHP memory exhaustion means one request or command exceeded memory_limit. Raising the limit may hide inefficient code, so inspect the workload.

Symptoms

  • Fatal error mentions allowed memory size exhausted.
  • Only large imports, exports or uploads fail.
  • CLI and web PHP behave differently.

Likely causes

  • Large arrays or full-table loads in memory.
  • Recursive code or unbounded loops.
  • Different memory_limit values for CLI and FPM.

Fix steps

  1. Find the file and line in the fatal error.
  2. Stream large files or query in chunks.
  3. Adjust memory_limit only when the workload genuinely needs it.

Verify the fix

  • Run the failing command with representative data.
  • Check PHP-FPM and CLI memory settings separately.
  • Watch logs after the fix.

FAQ

Is increasing memory_limit enough?

Sometimes, but large data handling should usually be streamed or chunked.

Why CLI differs from web?

CLI PHP and PHP-FPM can load different php.ini files.

Related tools and guides

Last updated: May 18, 2026