How to Fix Memory Exhausted Error in WordPress Quickly
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 // 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();
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 define('WP_MEMORY_LIMIT', '256M'); /* That's all, stop editing! Happy blogging. */
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.