Stability & Scaling

Recurring Magento Memory Exhaustion Patterns

Memory fatals that recur are a pattern, not an accident. Here is where the errors land, the shapes they take, and why raising memory_limit is not the fix.

Jason Schuman · March 5, 2026

Memory exhaustion is a pattern, not an accident

The fatal error "Allowed memory size of X bytes exhausted" shows up in almost every Magento store's history. Treated as a one-off, it gets fixed by raising a limit and forgotten.

Treated as a pattern, it tells you something specific about where the code loads more than it should. The same operation is exhausting memory for the same reason, on a schedule you can find.

This article covers the two memory limits people confuse, where these errors actually land, and how to read the stack trace back to the code that caused it.

The two memory limits people confuse

There are two different limits in play, and mixing them up sends people down the wrong path. The PHP memory_limit caps a single process, and the server's physical RAM caps every process running at once.

A single request hitting memory_limit throws the "allowed memory size exhausted" fatal. The whole server running out of RAM is a different failure, where the operating system kills processes to survive.

Magento operations like reindexing, imports, and compilation legitimately need a high limit, which is why the recommended CLI memory_limit is large, often 2G or unlimited. The web SAPI limit is normally much lower, and that gap matters when you diagnose.

Where the errors actually land

Not every memory fatal reaches the Magento logs. A request that dies mid-execution may only appear in the PHP-FPM error log or the web server log, never in exception.log.

CLI operations write to the terminal and often to their own logs, so a reindex or import that runs out of memory shows up where the job ran, not in the application log. Knowing to check all of these is half the battle.

When a customer reports an intermittent failure that leaves no trace in Magento's own logs, the PHP-FPM and web server logs are the next place to look.

The recurring shapes of the problem

Memory exhaustion tends to come from a short list of causes, and they repeat across stores:

  • Custom code loading a large collection all at once, with getCollection() and no pagination, on a catalog or order set that grew over time.
  • Reindex or import jobs run with a CLI memory limit that is too low for the data volume.
  • Admin reports and exports that build a huge array in memory before writing anything.
  • Long-running message-queue consumers that accumulate memory across messages and never release it.

Each has a different fix, but they share a tell: the failure scales with data or time, not with any single user's action.

Reading the stack trace back to the cause

The fatal error comes with a stack trace, and the trace names the exact line where memory ran out. That line is your starting point, though it is not always the culprit.

Read up the trace to find where the large operation began. A collection load deep in a Vendor\Module namespace points at that extension, while a trace through an export controller points at the report being generated.

The question to answer is whether the operation is genuinely large or genuinely broken. A one-time export of a million rows needs memory; a product page that exhausts memory is loading something it never should.

A quick way to confirm the limit

Before assuming the limit is wrong, confirm what it actually is. Running php -i | grep memory_limit on the CLI, and checking the same through the web SAPI, shows the real values in effect.

A surprising number of memory fatals come from a limit far lower than anyone thinks, because a server change reset it or the CLI and web configs drifted apart. Confirming the number takes seconds and rules out the simplest cause first.

Raising the limit is not the fix

The reflex is to raise memory_limit until the error stops. On a real leak or an unbounded load, that only delays the crash.

Raising memory_limit to stop a fatal does not fix the leak. It moves the crash to a higher number and a busier day, when the store is least able to absorb it.

Raising the CLI limit for a legitimately heavy job is reasonable. Raising the web limit to paper over a page that loads an unpaginated collection is not, because the real fix is to page or batch the load. The limit change buys time; the code change removes the problem.

Find the pattern, fix the cause

Memory fatals that recur are a map to the parts of the codebase that do not scale. Reading them as a pattern turns a scary error into a short list of operations to fix.

Knowing which jobs, pages, or extensions exhaust memory, and whether the cause is data volume or a genuine leak, is the difference between a store that grows smoothly and one that starts falling over as it gets busier. That distinction is exactly what a stability review is meant to draw.