How to Fix the WordPress Memory Exhausted Error by Increasing PHP Memory Limit
Are you seeing a "fatal error: allowed memory size exhausted" message on your WordPress site? This is one of the most common WordPress errors, but there‘s no need to panic. In most cases, you can fix it yourself by increasing your site‘s PHP memory limit.
As a WordPress beginner, encountering this error can be stressful and confusing. You may be worried that something is seriously wrong with your site.
Fortunately, this is not a critical error – it simply means your site is trying to use more memory than it has been allocated by your web hosting server. By increasing the PHP memory limit, we can give WordPress the resources it needs to run smoothly again.
In this guide, I‘ll explain what causes the memory exhausted error, show you how to easily increase the PHP memory limit in WordPress, and share some other tips for optimizing your site‘s performance. Let‘s get started!
What is the WordPress Memory Exhausted Error?
WordPress is built on PHP, a server-side programming language. Like any piece of software, PHP requires a certain amount of memory to run properly.
On your web hosting server, memory is allocated to different applications, including PHP. If your WordPress site tries to use more memory than PHP has been allocated, you‘ll see an error message like this:
"Fatal error: Allowed memory size of 41943040 bytes exhausted (tried to allocate 2348617 bytes) in /home4/xxx/public_html/wp-includes/plugin.php on line xxx"
The specific memory sizes mentioned will vary, but the overall error indicates that WordPress has hit its memory limit. Some other common variations of the message include:
"Fatal error: Out of memory (allocated xxx) (tried to allocate xxx bytes)"
"Fatal error: Allowed memory size of xxx bytes exhausted (tried to allocate xxx bytes)"
"Allowed memory size of xxxM exhausted"
So what causes WordPress to exceed its PHP memory limit? There are a few common culprits:
- Poorly coded plugins or themes that have memory leaks or consume excessive resources
- Uploading very large media files like images or videos
- Running too many plugins at once
- Receiving a spike in traffic that overwhelms your server
By default, WordPress will try to increase the PHP memory limit to 64MB if the existing limit is lower. However, many sites require more than 64MB to run optimally.
That‘s where increasing the PHP memory limit comes in. By allocating more memory to WordPress, we give it the resources it needs to handle plugins, media uploads, and traffic spikes without crashing.
How to Increase PHP Memory Limit in WordPress
There are a few different ways to increase the PHP memory limit on your WordPress site. Let‘s go through each method step-by-step.
Method 1: Edit wp-config.php File
The first option is to add a line of code to your wp-config.php file. This is one of the core WordPress files that contains important configuration settings for your site.
Step 1: Access your site‘s files via FTP or your hosting control panel‘s file manager. Look for the wp-config.php file in the root directory (usually public_html).
Step 2: Download a copy of wp-config.php to your computer as a backup.
Step 3: Open the original wp-config.php file and find the line that says "That‘s all, stop editing! Happy blogging." Just before that, add the following line of code:
define(‘WP_MEMORY_LIMIT‘, ‘256M‘);
This tells WordPress to increase the PHP memory limit to 256MB. You can adjust the number up or down depending on your needs, but 256MB is a good starting point.
Step 4: Save the file and re-upload it to your server, overwriting the original.
Step 5: Visit your WordPress site and check that the memory exhausted error has been resolved.
Editing core files like wp-config.php can feel intimidating at first, but as long as you follow the steps carefully and keep a backup on hand, it‘s a straightforward fix. Just make sure not to edit anything else in the file unless you know what you‘re doing.
Method 2: Edit .htaccess File
If editing wp-config.php doesn‘t work, you can try adding a snippet to your .htaccess file instead. This is a server configuration file that controls how your site handles redirects, caching, and other performance optimizations.
Step 1: Access your .htaccess file via FTP or file manager. It‘s located in your site‘s root directory, but since it starts with a dot, it may be hidden by default in some FTP clients or file managers. Enable "show hidden files" to find it.
Step 2: Download a copy of .htaccess to your computer as a backup.
Step 3: Open the original .htaccess file and add the following line at the very top:
php_value memory_limit 256M
Once again, this sets the PHP memory limit to 256MB. You can use a different value if needed.
Step 4: Save the file and re-upload it to your server, overwriting the original.
Step 5: Refresh your WordPress site and see if the error persists.
Editing the .htaccess file is a bit more advanced than wp-config.php, so if you‘re not comfortable doing it yourself, you may want to ask your hosting provider for assistance. Make sure to keep the original file backed up in case anything goes wrong.
Method 3: Check php.ini File
The php.ini file controls your server‘s PHP configurations, including memory limit. Unlike wp-config.php and .htaccess, it‘s not a WordPress-specific file, so your hosting provider manages it.
If the previous two methods didn‘t fix the memory exhausted error, it‘s worth checking your php.ini settings to see what the current memory limit is set to. You can usually access php.ini via your hosting control panel‘s file manager.
Look for a line that specifies the memory limit, like:
memory_limit = 64M
If you see a lower value than what you tried setting in wp-config.php or .htaccess, that could explain why the other methods didn‘t work.
However, editing php.ini is an advanced tweak that can cause compatibility issues or break your site if done incorrectly. It‘s best to contact your hosting support and ask them to increase the limit for you rather than editing it yourself.
Method 4: Ask Your Hosting Provider
If you‘re not comfortable editing core WordPress files or you simply want to make sure the job is done right, reach out to your web hosting provider‘s support team. Explain that you‘re seeing the memory exhausted error and ask them to increase your site‘s PHP memory limit.
Most WordPress hosts are familiar with this issue and will be happy to make the change for you. It‘s part of what you‘re paying them for! They can also help you troubleshoot other potential causes of the error.
Some hosts, like WP Engine, automatically handle PHP memory for you to prevent the error from occurring in the first place. If you‘re frequently hitting memory limits, consider upgrading to a managed WordPress host that takes care of server configuration and optimization.
Other Tips to Avoid WordPress Memory Exhausted Error
Increasing PHP memory is the most direct way to resolve the memory exhausted error and get your site up and running normally again. But it‘s not the only way to prevent the error and keep your WordPress site in tip-top shape.
Here are a few other recommendations:
- Use reputable, well-coded plugins and themes. Avoid anything that feels buggy or slows your site down.
- Keep your plugins and themes up-to-date, and remove any that you‘re not actively using.
- Optimize your media files before uploading. Use a tool like Smush to compress images.
- Upgrade your WordPress hosting plan if you‘re on a lower-tier or shared server. More server resources can help accommodate memory-heavy plugins and traffic spikes.
- Break up very long posts into multiple pages to reduce the strain on memory.
- Leverage website caching to serve lighter, faster-loading copies of your content.
- Use a content delivery network (CDN) to reduce the load on your origin server.
By proactive maintaining your WordPress site and being mindful of how much you‘re loading it up with plugins and large files, you can minimize the risk of hitting the dreaded memory limit.
Of course, if you have a large, high-traffic, or resource-intensive site, increasing the PHP memory limit may be a temporary bandage rather than a long-term solution. There‘s a limit to how much you can optimize a WordPress site on a cheap shared hosting plan.
If you find yourself having to increase memory frequently or you want to take your site to the next level, it may be time to upgrade to a better hosting plan. Look for a managed WordPress host that offers generous resource allocations, automatic memory management, and premium performance features.
You‘ll pay more than budget shared hosting, but you‘ll get a faster site that can grow with your business. And you likely won‘t have to troubleshoot memory errors anymore!
To Recap
If you see the "fatal error: allowed memory size exhausted" message on your WordPress site, don‘t panic. Increasing your site‘s PHP memory limit will resolve the issue in most cases.
There are three ways to increase the memory limit yourself:
- Add a line to your wp-config.php file
- Add a line to your .htaccess file
- Check your php.ini file settings
If those methods don‘t work or you need assistance, contact your hosting provider and request a memory limit increase. You can also optimize your site in other ways to minimize the risk of hitting the memory ceiling.
Remember, every WordPress site is unique and has different hosting requirements. What works for a small blog may not work for a large WooCommerce store. Focus on finding the right balance of resources and performance optimizations to keep your particular site running smoothly.
By taking the time to understand concepts like PHP memory and how to adjust it, you‘re building valuable skills that will serve you well in your WordPress journey. The memory exhausted error is a common challenge, but one you can confidently overcome.
