Skip to content

How to fix "Allowed memory size exhausted" in WordPress

A page needed more memory than PHP is allowed to use, so PHP stopped it. Raising WordPress's memory limit in wp-config.php clears it on most hosts. The error message itself names the file that asked for too much, which tells you what to look at if it comes back.

By
WP Ministry
Published
Tested on
WordPress 7.1.3, PHP 8.3.35

In short

  • The limit is a safety setting, not a fault in your site. One page asked for more than it allows.
  • Adding one line to wp-config.php raises the limit on most hosts.
  • If the line changes nothing, your host sets a ceiling that only they can lift.
  • The message names a file. If it is inside a plugin's folder, that plugin is what is using the memory.

PHP, the language WordPress is written in, gives each page a fixed amount of memory to work with. When a page asks for more, PHP stops it and reports "Allowed memory size of … bytes exhausted". WordPress then shows visitors "There has been a critical error on this website" in its place.

The number in the message is the limit in bytes: 134217728 is 128 MB, and 268435456 is 256 MB.

Where it goes wrong

A page request passes through each of these in turn. This one comes from PHP, the language WordPress runs on.

  1. Browser
  2. DNS
  3. HTTPS
  4. CDN or firewall
  5. Web server
  6. PHP (this error comes from here)
  7. WordPress
  8. Database and files

What causes it

  • The memory limit is lower than the site now needs

    Common

    Sites grow. A limit that suited a new site can be too small once it has a page builder, a shop and a dozen plugins.

    Fix: Raise WordPress's memory limit

  • One plugin or theme is using far more than the rest

    Common

    A plugin that loads a whole file into memory, or that has a fault, can use up any limit however high it is set.

    Fix: Find what is using the memory

  • The host caps memory below what you set

    Sometimes

    On some hosting plans the limit in the server's own settings cannot be raised from inside WordPress.

    Fix: Ask the host to raise PHP's own limit

How to fix it

Raise WordPress's memory limit

  • Easy
  • Back up first
  • About 5 minutes
  • Steps tested on WordPress 7.1.3
  1. Step 1: Open wp-config.php

    It is in the folder WordPress is installed in, beside the wp-content folder.

  2. Step 2: Add the line above "That's all, stop editing"

    Anything added below that line is read too late to count.

    wp-config.php
    define( 'WP_MEMORY_LIMIT', '256M' );
  3. Step 3: Save the file and reload the page that failed

    If it loads, the limit was the cause. If nothing changes straight away, wait a few seconds and reload: PHP can go on using its stored copy of the file for a moment after you save.

WordPress can only raise the limit, never lower it, and only as far as your host allows. If the page still fails with the same number in the message, the host's ceiling is in the way.

To undo it: Remove the line from wp-config.php.

Find what is using the memory

  • Takes care
  • Low risk
  • About 15 minutes
  • Steps tested on WordPress 7.1.3

The error message ends with the path of the file that asked for the memory. WordPress hides that from visitors, but will write it to a log if you ask.

  1. Step 1: Turn the debug log on

    In wp-config.php, find the line define( 'WP_DEBUG', false ); and replace it with these three. If you have WP-CLI, the commands below do the same.

    wp-config.php
    define( 'WP_DEBUG', true );
    define( 'WP_DEBUG_LOG', true );
    define( 'WP_DEBUG_DISPLAY', false );
  2. Step 2: Load the page that fails

    Once is enough.

  3. Step 3: Read the log

    Open wp-content/debug.log and find the line with "Allowed memory size". The path at the end of it shows where the memory ran out. A path through wp-content/plugins/ followed by a plugin's folder name points at that plugin.

  4. Step 4: Turn the debug log off again

    Put the line back to define( 'WP_DEBUG', false );. The log can hold details you do not want left on a public server, so delete debug.log when you have read it.

bash
wp config set WP_DEBUG true --raw
wp config set WP_DEBUG_LOG true --raw
wp config set WP_DEBUG_DISPLAY false --raw

When you have what you need:

bash
wp config set WP_DEBUG false --raw
wp config set WP_DEBUG_LOG false --raw

Switch that plugin off and load the page again. If it loads, you have found the cause: update the plugin, replace it, or tell its author.

Ask the host to raise PHP's own limit

  • Easy
  • No risk
  • About 15 minutes

If the line in wp-config.php makes no difference, the limit is set in the server's PHP settings, above WordPress.

  1. Step 1: Look for a PHP settings page in your hosting panel

    Many hosts let you change memory_limit yourself, under a name such as "PHP settings" or "PHP options".

  2. Step 2: If there is none, ask support

    Ask them to set PHP's memory_limit to 256M for your site, and whether your plan allows it.

When to get help

If the limit is at 256M and the error is still there, raising it further hides the problem instead of solving it. Something is using memory it should not, and finding it means reading the log and testing with plugins switched off.

Common questions

How much memory does WordPress need?

WordPress asks for 40 MB by default and allows itself up to 256 MB in the dashboard. A site with a page builder or a shop commonly needs 256 MB for visitors as well.

Is a higher limit always better?

No. The limit is there so that one runaway page cannot take the whole server's memory. Set it to what the site needs and find out why if it needs more.

Why does it only happen sometimes?

Different pages do different work. Uploading a large image, running a backup or importing products needs far more memory than showing a post.

More on this subject

Would you rather we fixed it?

Quick Fix is $49. One issue, one site, up to about an hour. No fix, no fee. 30-day warranty. It starts with a free diagnosis.