PHP

Allowed memory size exhausted (PHP)

The script tried to use more memory than PHP allows and was killed.

What it looks like

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /var/www/app/export.php on line 88

What does "Allowed memory size exhausted (PHP)" mean?

A fatal error raised when a script passes PHP’s memory_limit (134217728 bytes is 128 MB). The line it names is only where the last allocation happened — the memory was usually used up gradually, somewhere else.

What causes "Allowed memory size exhausted (PHP)"?

  • Loading a huge dataset/file fully into memory.
  • An accidental infinite loop building an array.
  • Processing big images/exports.

How do I confirm the cause?

  1. Look for code that loads everything at once: fetchAll() on a large query, file() or file_get_contents() on a big file, or large images in memory.
  2. Log memory_get_usage() at a few points in the script to see where memory grows.
  3. Check for loops that keep adding to an array or cache without ever clearing it.

How do I fix "Allowed memory size exhausted (PHP)"?

  • Process data in chunks/streams instead of all at once.
  • Fix any runaway loop.
  • Raise memory_limit in php.ini only if the usage is legitimate.

How do I stop it happening again?

  • Stream or batch large data: fetch rows one at a time, read files line by line, and paginate exports.
  • Treat raising memory_limit as a last resort for work that genuinely needs it, not as the fix.
Got a different error? Paste it into the Explain This Error tool → Identifies the family and the fix — runs locally, nothing uploaded.

Related errors