6 Ways to Fix the WordPress Memory Exhausted Error
You are editing a blog post in the WordPress block editor, and suddenly a white screen appears. The message reads: "Fatal error: Allowed memory size of 41943040 bytes exhausted." Your heart drops. You cannot access the dashboard, and your site is down. This is the dreaded WordPress memory exhausted error, and it happens when a script tries to use more memory than the server allows. The quickest fix is to increase the PHP memory limit by editing the wp-config.php file, but often the root cause is a faulty plugin, a bloated theme, or a corrupted database. In this guide, you will learn how to fix the error in 6 ways, from the simplest to the most advanced, and how to prevent it from returning.
Before you start, take a deep breath. This error is common and fixable. If you have FTP or cPanel access, you can restore your site within minutes. The most important step is to back up your site and database before making any changes. Use a plugin like UpdraftPlus or your hosting cPanel to create a full backup. This ensures you can revert if something goes wrong. Once the backup is complete, you can proceed with the fixes below.
Why Does the Memory Exhausted Error Happen?
The WordPress memory exhausted error occurs when a PHP script exceeds the memory limit set by your server. WordPress does not impose its own memory limit; it uses PHP's memorylimit directive. By default, many hosts set this to 40MB or 64MB, which is too low for modern WordPress sites with multiple plugins, large media files, or complex themes. Common triggers include:
- A poorly coded plugin that leaks memory or runs infinite loops
- A heavy theme with too many enqueued scripts or custom queries
- A corrupted database with stuck cron jobs or orphaned options
- A malicious script from a hacked site
- Large media files or too many image sizes
The error can appear as a white screen, a broken page, or a message in the WordPress admin area. Understanding the cause helps you choose the right fix. Let us walk through 6 proven ways to resolve it.
1. How Do I Increase the PHP Memory Limit in wp-config.php?
This is the fastest fix and works for 80% of cases. You need to edit the wp-config.php file located in the root directory of your WordPress installation. Access it via FTP (using FileZilla or your host's file manager) or through cPanel's File Manager.
Open wp-config.php and add the following line right before the line that says / That's all, stop editing! Happy publishing. /:
define('WPMEMORYLIMIT', '256M');This sets the memory limit to 256MB. If your host allows it, you can increase it to 512MB or even 1GB. After saving the file, reload your site. If the error persists, check with your hosting provider — some shared hosts override this value in a separate PHP configuration file. In that case, you need to contact support or use the next method.
Note: If you cannot access the WordPress admin area, use FTP or your hosting control panel to edit the file. The change takes effect immediately. This fix is safe and does not break anything, as long as you stay within hosting limits.
2. Can I Increase the PHP Memory Limit in php.ini or .htaccess?
If editing wp-config.php does not work, your server may have a lower priority for that setting. The next step is to create or edit the php.ini file in your site's root directory. This file overrides the default PHP settings for your site only.
Create a new file named php.ini (if it does not exist) and add this line:
memorylimit = 256MUpload the file to your site's root via FTP. If your host uses PHP-FPM, you may need to add this to a .user.ini file instead. Alternatively, you can add a rule to the .htaccess file, which is often available on Apache servers. Open .htaccess and add this line at the top:
phpvalue memorylimit 256MBoth methods achieve the same result. After saving, refresh your site. If the error is gone, you are all set. If not, move to the next step — the problem is likely a rogue plugin.
3. How Do I Fix the Error by Disabling Plugins and Themes?
When a single plugin or theme function consumes too much memory, the error appears even after increasing the limit. The fastest way to diagnose this is to disable all plugins and switch to a default theme like Twenty Twenty-Four. If you cannot access the admin area, rename the plugins folder via FTP. Navigate to /wp-content/ and rename plugins to pluginsold. This disables all plugins at once. If the site loads, rename the folder back and re-enable plugins one by one, checking the site after each activation.
Similarly, rename your current theme's folder (e.g., twentytwentythree to twentytwentythreeold) inside /wp-content/themes/. WordPress will fall back to a default theme. If the error disappears, the theme is the culprit. Replace it with a lightweight, well-coded alternative.
Common memory-hungry plugins include caching plugins with too many rules, SEO plugins with large databases, and page builders with complex modules. Always use the latest versions from reputable developers. This step is diagnostic — once you identify the offender, you can either update, replace, or contact the developer for a fix.
4. How Do I Clean Up the Database to Free Up Memory?
A bloated database with thousands of post revisions, transients, and orphaned options can strain memory. WordPress's automatic revision system can create dozens of copies of a single post. To clean this up, use a plugin like WP-Optimize or Advanced Database Cleaner. If you cannot access the admin area, use phpMyAdmin in cPanel.
In phpMyAdmin, select your WordPress database, then run the following SQL query to delete all post revisions (make sure you have a backup first):
DELETE FROM wpposts WHERE posttype = 'revision';Next, delete orphaned transients — temporary options that never expire. Use this query:
DELETE FROM wpoptions WHERE optionname LIKE '%transient%';These operations can free up megabytes of memory. After cleaning, run the OPTIMIZE TABLE command on each table or use a plugin. This step is especially effective for older sites with years of accumulated data. If you have many users or large tables, consider a dedicated database optimization service or plugin automation.
5. What If the Error Is Caused by a Corrupted .htaccess File?
A corrupted .htaccess file can cause infinite redirects or memory consumption. This is less common but easy to fix. Rename your existing .htaccess file to .htaccessold via FTP. Then, go to the WordPress admin area (if accessible) or manually create a new, default .htaccess file. To generate a default one, navigate to Settings > Permalinks in the admin panel and click "Save Changes" — WordPress will regenerate it automatically. If the admin area is inaccessible, create a file with this content using a text editor:
# BEGIN WordPressRewriteEngine OnRewriteRule .* - [E=HTTPAUTHORIZATION:%{HTTP:Authorization}]RewriteBase /RewriteRule ^index\.php$ - [L]RewriteCond %{REQUESTFILENAME} !-fRewriteCond %{REQUESTFILENAME} !-dRewriteRule . /index.php [L]# END WordPressUpload this file as .htaccess to your site's root. Reload your site. If the error resolves, the old file had a conflict, possibly from a security plugin or a bad redirect rule. This fix is quick and non-destructive.
6. How Do I Increase Server Resources via Hosting Settings?
Sometimes the memory limit is enforced by your hosting provider at the server level. Shared hosting plans often limit PHP memory to 128MB or 256MB. If you need more, contact your host and ask them to increase the memorylimit in the php.ini or PHP-FPM pool configuration. Many hosts, like SiteGround or Kinsta, allow this via a control panel setting under PHP settings.
If you are on a VPS or dedicated server, you can edit the php.ini file directly and restart the web server. For example, on Ubuntu with Apache and PHP-FPM, you would run:
sudo sed -i 's/memorylimit = 128M/memorylimit = 512M/' /etc/php/8.1/fpm/php.inisudo service php8.1-fpm restartsudo service apache2 restartThis change affects all sites on that server. If you manage multiple WordPress installations, ensure this does not conflict with individual wp-config.php settings. For cloud hosting, you may need to upgrade your plan or adjust resource limits in the provider's dashboard.
How to Prevent the Memory Exhausted Error in the Future
Once your site is back online, take these steps to prevent a recurrence. First, monitor your memory usage with a plugin like Query Monitor. It shows you how much memory each plugin and theme function uses. Replace any that exceed 40MB. Second, keep everything updated: WordPress core, plugins, and themes. Older code often has memory leaks fixed in later versions. Third, limit post revisions by adding this to wp-config.php:
define('WPPOSTREVISIONS', 5);This keeps only 5 revisions per post, saving database space. Fourth, use a caching plugin like WP Rocket or W3 Total Cache to reduce PHP execution load. Caching serves static HTML files instead of processing PHP on every request. Finally, choose a reliable hosting provider that offers at least 512MB of PHP memory per site. Many cheap hosts share memory among hundreds of sites, leading to random exhaustion. If your site grows, consider upgrading to a managed WordPress host with dedicated resources.
For content-heavy sites, automating SEO content publishing with a tool like nativeWP can reduce the number of plugins needed, lowering overall memory consumption. Once your site is healthy again, nativeWP helps you publish SEO articles on autopilot using your own API key, freeing you from managing dozens of content plugins. Learn more about nativeWP.
Frequently Asked Questions
How do I check the current PHP memory limit in WordPress?
You can check it by installing a health check plugin like WP Health or by adding a simple code snippet to a test file. Create a new PHP file in your site root called info.php with the content <?php phpinfo(); ?>. Access it in your browser, then search for memorylimit. Delete the file after checking for security reasons. Alternatively, use the Site Health tool in WordPress under Tools > Site Health.
Can I set the memory limit to unlimited?
Setting memorylimit to -1 removes the limit, but it is not recommended. A runaway script can consume all server memory, crashing the entire server and affecting other users. Instead, set a high value like 512MB or 1024MB. Most shared hosts do not allow unlimited memory anyway.
What is the difference between WPMEMORYLIMIT and WPMAXMEMORYLIMIT?
WPMEMORYLIMIT sets the memory limit for the front end of your site (public pages). WPMAXMEMORYLIMIT sets the limit for the admin area, which is often higher (default 256MB). If you only see the error in the admin dashboard, try increasing WPMAXMEMORYLIMIT separately by adding this to wp-config.php: define('WPMAXMEMORY_LIMIT', '512M');.
Why did the error appear after updating a plugin?
A plugin update can introduce new code that is not optimized for memory. The update may include larger libraries, additional queries, or background processes that exceed your current limit. Roll back to the previous version using a plugin like WP Rollback, and then contact the plugin developer. Always test updates on a staging site before pushing to production.
Is this error a sign of a hacked site?
It can be. Malicious scripts often run memory-intensive operations like spamming or brute-force attacks. If you cleaned the database, disabled all plugins, and increased memory but the error returns, scan your site with a security plugin like Wordfence or Sucuri. Check your wp-config.php and functions.php for suspicious code. A hacked site may also show unusual outbound traffic.
Key Takeaways
- The most common cause is a low PHP memory limit; increase it first via
wp-config.php. - If that does not work, disable all plugins and switch to a default theme to isolate the problem.
- Clean your database regularly to remove bloated revisions and transients.
- Always back up your site before making any changes to configuration files.
- Use a reliable hosting provider with at least 512MB of PHP memory per site.