← Tous les articles

PHP “Allowed memory size exhausted”: causes and fixes

Raising PHP's memory limit treats the symptom. Finding what eats the memory fixes the problem. Both approaches, in the right order.

PHP “Allowed memory size exhausted”: causes and fixes

Key points

  • The error means a script went over PHP’s memory limit.
  • You can raise the limit, but you should also find the cause.
  • Loops over large datasets are the usual suspects.

“Fatal error: Allowed memory size of 134217728 bytes exhausted”: that number is the limit in bytes (128 MB here), and the script tried to use more. Before just raising it, it is worth understanding why.

Raise the limit the right way

On WordPress, the dedicated constants go in wp-config.php:

define('WP_MEMORY_LIMIT', '256M');
define('WP_MAX_MEMORY_LIMIT', '512M'); // admin, imports

They cannot exceed the server’s memory_limit. If nothing changes, raise memory_limit in php.ini or your host’s panel.

Find what uses the memory

The error shows the file where the limit was hit, which is not always the real culprit. Query Monitor shows memory per page: compare with and without the suspected plugin.

In plain PHP, calls to memory_get_peak_usage(true) at a few points will locate the peak.

Real causes from our projects

  • A query loading every post at once: 'posts_per_page' => -1 on thousands of items. Process in batches of 100.
  • A CSV or XML import read fully into memory instead of line by line (fgetcsv, XMLReader).
  • GD resizing huge images (8,000-pixel photos).
  • A hook loop, such as a save_post that keeps firing itself.

Process in batches

$paged = 1;
do {
    $ids = get_posts(['fields' => 'ids', 'posts_per_page' => 100, 'paged' => $paged++]);
    foreach ($ids as $id) { /* work */ }
    wp_cache_flush();
} while ($ids);

FAQ

Is 256 MB too much?

For WordPress with WooCommerce or a page builder, it is common. Above 512 MB for a public page, there is almost always a code issue.

Why does it only happen in the admin?

The dashboard loads more. WP_MAX_MEMORY_LIMIT controls the limit used there.

Facing this on your site? We diagnose and fix it without putting your content at risk.

Partager LinkedIn Facebook X WhatsApp
</>

webtestudio

Agence web : sites WordPress sur mesure, thèmes premium et logiciels SaaS.

Parler de votre projet →