Allowed Memory Size Exhausted

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65536 bytes)

Quick Answer

Your script has used more memory than the configured limit. This usually indicates an infinite loop, loading too much data at once, or a memory leak. Optimize your code to use less memory or increase the limit.

Why This Happens

PHP enforces a memory limit per script defined by the memory_limit directive in php.ini. When your script tries to allocate memory beyond this limit, PHP terminates it. This commonly happens when loading large datasets into memory, recursive functions without proper termination, or accumulating data in loops.

The Problem

// Loading millions of rows into memory at once
$rows = $pdo->query('SELECT * FROM logs')->fetchAll();
foreach ($rows as $row) {
    processRow($row);
}

The Fix

// Process rows one at a time using a cursor
$stmt = $pdo->query('SELECT * FROM logs');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    processRow($row);
}

Step-by-Step Fix

  1. 1

    Identify the memory hog

    Check the line number in the error. Look for large array operations, file reads, or unbounded loops that accumulate data.

  2. 2

    Optimize memory usage

    Use generators, chunked queries, streaming reads, or unset large variables after use to reduce peak memory consumption.

  3. 3

    Increase the limit if needed

    If the memory usage is genuinely necessary, increase memory_limit in php.ini or use ini_set('memory_limit', '256M') at the start of the script.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free