0
0
WordpressDebug / FixBeginner · 4 min read

How to Fix Memory Exhausted Error in WordPress Quickly

The WordPress memory exhausted error happens when PHP runs out of memory. Fix it by increasing the PHP memory limit in your wp-config.php file using define('WP_MEMORY_LIMIT', '256M'); or higher.
🔍

Why This Happens

The memory exhausted error occurs because WordPress or a plugin tries to use more PHP memory than the server allows. This limit is set to prevent one site from using too many resources and crashing the server.

When your site needs more memory than allowed, PHP stops running and shows this error.

php
<?php
// Example of code that can cause memory exhausted error
function heavy_task() {
    $array = [];
    while(true) {
        $array[] = str_repeat('WordPress', 10000); // keeps adding large strings
    }
}
heavy_task();
Output
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 262144 bytes) in /path/to/file.php on line X
🔧

The Fix

To fix this, increase the PHP memory limit WordPress can use. The easiest way is to add a line in your wp-config.php file before the line that says /* That's all, stop editing! Happy blogging. */.

This tells WordPress to allow more memory, like 256 megabytes, which is enough for most sites.

php
<?php
define('WP_MEMORY_LIMIT', '256M');
/* That's all, stop editing! Happy blogging. */
Output
No memory exhausted error; WordPress runs normally with increased memory.
🛡️

Prevention

To avoid this error in the future, keep your plugins and themes updated and only use well-coded ones. Avoid running too many heavy plugins at once.

Also, monitor your site's memory usage with tools or plugins and increase memory only if needed.

Consider upgrading your hosting plan if your site grows and needs more resources.

⚠️

Related Errors

Other errors related to memory include:

  • White screen of death: Often caused by memory limits or PHP errors.
  • 500 Internal Server Error: Can be triggered by memory exhaustion or server misconfiguration.
  • PHP timeout errors: When scripts run too long due to insufficient memory.

Fixes usually involve increasing memory limits or optimizing code.

Key Takeaways

Increase PHP memory limit in wp-config.php to fix memory exhausted errors.
Add define('WP_MEMORY_LIMIT', '256M'); before the stop editing comment.
Keep plugins and themes updated and avoid heavy plugins to prevent memory issues.
Monitor memory usage and upgrade hosting if your site grows.
Related errors like white screen or 500 errors can also be caused by memory limits.