Fix WordPress Memory Exhausted Error – Increase PHP Memory Limit

Encountering an error message like “Fatal error: Allowed memory size of X bytes exhausted…” in WordPress means a specific PHP script or process required more memory (RAM) to run than your server allocated for it. This is commonly known as the PHP Memory Exhausted Error.

This error can appear on the frontend, backend (wp-admin), or during specific actions like uploading media or activating a plugin. Fortunately, it’s usually straightforward to fix by increasing the memory allocated to PHP.

What Causes the Memory Exhausted Error?

WordPress and its plugins/themes run on PHP, a server-side scripting language. Your hosting server allocates a specific amount of memory for PHP processes. If a WordPress operation (often involving a poorly coded plugin, a complex theme feature, or processing large images/data) tries to use more memory than this limit, PHP triggers the fatal error.

Common Causes

  • Low Default PHP Memory Limit: Many hosting plans start with a low limit (e.g., 64M or 128M), which might be insufficient for modern WordPress sites with multiple plugins.
  • Resource-Intensive Plugins: Some plugins, especially those dealing with e-commerce, page building, backups, or complex calculations, naturally require more memory.
  • Complex Themes: Themes with many features and dynamic elements can consume more memory.
  • Processing Large Media Files: Uploading or manipulating very large images can temporarily spike memory usage.
  • Specific Actions: Running updates, imports/exports, or complex database queries can exceed the limit.

Step-by-Step Fixes: Increasing the PHP Memory Limit

WordPress itself will try to increase the limit automatically up to certain defaults (e.g., 64M for multisite), but often manual intervention is needed. Try these methods in order:

1. Edit the `wp-config.php` File

This is the most common and recommended method for WordPress.

  1. Connect to your website files using FTP or your hosting account’s File Manager.
  2. Locate the wp-config.php file in the root directory of your WordPress installation.
  3. Download a backup copy of the file before editing.
  4. Open the file for editing and find the line that says: `/* That’s all, stop editing! Happy publishing. */` (or similar).
  5. Just before this line, add the following code:
    define( 'WP_MEMORY_LIMIT', '256M' );
  6. Save the changes and upload the modified file back to your server, overwriting the original.
  7. Check if the memory error is resolved. If not, you can try a higher value like `512M`, but excessively high values might indicate an underlying issue with a plugin/theme rather than just needing more base memory.

Note: Some hosts may restrict or override settings in `wp-config.php`. If this doesn’t work, proceed to the next method.

2. Edit the `.htaccess` File

If editing `wp-config.php` doesn’t work, you can try modifying your `.htaccess` file (also in the root directory).

  1. Connect via FTP or File Manager.
  2. Locate the .htaccess file. (It might be hidden; ensure your FTP client or File Manager is set to show hidden files).
  3. Download a backup copy.
  4. Open the file for editing and add the following line, usually at the bottom:
    php_value memory_limit 256M
  5. Save the changes and upload the file.
  6. Test your site again.

Note: This method only works if your server is running Apache and allows PHP settings modification via `.htaccess`. It might cause a 500 Internal Server Error if not supported; if so, remove the line and re-upload the original `.htaccess`.

3. Edit the `php.ini` File

If you have access to your server’s `php.ini` file (common on VPS or dedicated servers, less so on shared hosting), you can modify the memory limit directly.

  1. Locate your `php.ini` file. Its location varies depending on the server setup (common paths might include `/etc/php/7.4/apache2/php.ini` or `/usr/local/lib/php.ini`, or sometimes a copy within your `public_html` or home directory). You might need to ask your host or check PHP info (`phpinfo()`) for the correct path.
  2. Download a backup copy.
  3. Open the file and search for the line `memory_limit = …`.
  4. Change the value to your desired limit (e.g., `256M`):
    memory_limit = 256M
  5. Save the file. You might need to restart the webserver (Apache, Nginx) for the changes to take effect.
  6. Test your site.

Note: On some shared hosting platforms, you might be able to create a custom `php.ini` or `.user.ini` file in your `public_html` directory with just the `memory_limit = 256M` line inside.

4. Use Your Hosting Control Panel

Many hosting providers offer a graphical interface to change PHP settings, including the memory limit.

  • Log in to your hosting account (cPanel, Plesk, SiteGround Site Tools, etc.).
  • Look for options like “PHP Settings”, “Select PHP Version”, “MultiPHP INI Editor”, or similar.
  • Find the `memory_limit` directive and change its value.
  • Save the changes. This often automatically modifies the correct configuration file for you.

5. Contact Your Hosting Provider

If none of the above methods work or you’re uncomfortable editing files, contact your hosting provider’s support team. Explain that you’re encountering the PHP memory exhausted error and ask them to increase the `memory_limit` for your account to at least 256M.

What if Increasing Memory Doesn’t Help?

If you’ve significantly increased the memory limit (e.g., to 512M or higher) and still encounter the error, it might indicate a deeper problem:

  • Plugin/Theme Bug: A specific plugin or theme might have a memory leak or inefficient code. Try the standard conflict test (deactivating plugins one by one, switching to a default theme) to identify the culprit.
  • Infinite Loop: Faulty code could be causing a script to run in an infinite loop, consuming all available memory.

In these cases, identifying the specific plugin/theme/code causing the issue is necessary.

The WordPress memory exhausted error is usually resolved by increasing the PHP memory limit. Start with the `wp-config.php` method, and proceed through the other options if necessary. While 256M is often a good starting point, remember that excessively high limits might mask underlying code issues. Always keep WordPress, themes, and plugins updated, and choose well-coded components to minimize memory usage.