Allowed memory size exhausted 🐘 PHP

PHP Fatal error: Allowed memory size of X bytes exhausted

The script tried to use more memory than PHP’s memory_limit allows.

Seen on: PHP WordPress

Meaning

PHP caps memory per request. The number in the message is the limit (134217728 bytes = 128 MB). The trigger is either legitimately heavy work (big imports, image processing) or a bug that keeps allocating (infinite loop, loading a whole table into an array).

Common causes

  • Loading large result sets or files fully into memory
  • Infinite loop/recursion building arrays
  • Image processing on large images (GD/Imagick)
  • Composer install/update on low-memory servers
  • WordPress plugins with heavy queries

⚡ Quick fix

  1. Raise memory_limit (e.g. 256M) if the workload is legitimate
  2. Process data in chunks/generators instead of loading everything
  3. For Composer: COMPOSER_MEMORY_LIMIT=-1 composer install
  4. Profile with memory_get_peak_usage() to find the hotspot

Detailed fix by platform

PHP

  1. Stream rows instead of fetchAll():
    php
    $stmt = $pdo->query('SELECT * FROM big_table');
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {   // one row at a time
        process($row);
    }
    // For MySQL, also consider: $pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
  2. Set memory_limit = 256M in php.ini, .user.ini, or ini_set('memory_limit', '256M').

WordPress

  1. define('WP_MEMORY_LIMIT', '256M'); and define('WP_MAX_MEMORY_LIMIT', '512M'); in wp-config.php.

How to diagnose

  1. Limit — What is memory_limit (from the message)?
  2. Location — Which file/line allocates?
  3. Data size — How much data is loaded at once?
  4. Loops — Any unbounded growth?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.