If you see an “allowed memory size exhausted” error in WordPress, you should know that it's one of the most commonly seen WordPress errors. Not to worry, you can quickly fix it by increasing the PHP memory limit. We will give you a hand with resolving the problem.
In this post, you will find:
Further reading:
How to Fix 404 Errors in WordPress After Changing Permalinks
The Many Uses to Which You Can Put Your WordPress Website
Top 5 Security Issues with WordPress and How to Fix Them
About WordPress Memory Limit
WordPress is written in PHP, which is a server-side programming language. Every website needs a WordPress hosting server for it to function correctly. Web servers are actually like any other computer. They need to have the memory to run multiple applications efficiently at the same time. Server administrators allocate specific memory size to different applications, including PHP.
When your WordPress code requires more memory than the default allocated memory, you get this error:
Fatal error
Allowed memory size of 33554432 bytes exhausted (tried to allocate 2348617 bytes) in /home4/xxx/public_html/wp-includes/plugin.php on line xxx
By default, WordPress will automatically try to increase the PHP memory limit if it's less than 64MB. However, 64MB often is not nearly enough. With that said, let us see how to address the PHP memory limit in WordPress to avoid the memory exhausted error.
Further reading:
How to Backup and Restore the backup of a WordPress Website
How to Create Redirects in WordPress
Adjusting the PHP Memory Limit in WordPress
There are three ways to address the memory limit:
You can change the memory limit via the PHP Selector in cPanel
- With our shared hosting plans you have full control, and you can easily configure specific PHP setup for your account. This can be achieved via cPanel, so you do not need to contact the Support team for that. In order to change the
memory_limit
andupload_max_filesize
values, you need to access your cPanel account first; - Locate the Software and Services section on the page and click on Select PHP Version. At the top right corner of the screen, you will notice a button named Switch To PHP Settings. Go to that page, and you will be able to change the
memory_limit
andupload_max_filesize
values to the ones you need for your application; - When you complete the change, click on the Save button on the left bottom corner of the page and the system will do the rest. Open your website, and you will be able to use the new configuration of your account.
You can increase the memory limit of the application
- Many WordPress themes and plugins that require the memory limit of the application to be increased. How can this be achieved? It is relatively easy, and all you have to do is locate the wp-config.php file and add the following line:
define ('WP_MEMORY_LIMIT', '64M');
- Also, if you have access to your php.ini file, you can add or change this line:
memory_limit = 64M;
Notice that in both of the examples, we have used 64 MB as value for the memory limit. However, you should set the value that the theme/plugin requires. Also, those changes can be performed via the cPanel or the FTP service.
Increasing Your PHP Memory Limit in php.ini
Open your wp-admin folder, and see if a file called php.ini or php5.ini is there. It likely won’t be, but double-click it if it is download the file to your computer, and open it.
Open a text editor on your computer if the file isn’t there.
If you are editing your php.ini or php5.ini file, find the line that contains memory_limit and an M value, and change it to this:
memory_limit 512M
If you are creating your own file, add this code to your text editor:
memory_limit 512M
Save the file, and name it php.ini, like so:
If you’re new to FileZilla and FTP clients in general, you’ll notice there are four “views” or “panels” in FileZilla. Take a look at the bottom two, which are side by side. Use the one on the left to find the location on your computer where you saved the php.ini file.
Make sure your wp-admin folder is still open in the right panel. Once you’re certain it is, drag the php.ini file from the left panel over to the right panel to upload the file to your site. Overwrite the original one if you were editing an existing php.ini file.
Refresh the FTP client, as depicted in the image above. If you are increasing your memory limit to correct a specific error on your site, refresh your site to see if the error goes away. If it does not, try increasing the 512M value to something else. Alternatively, you can try renaming the php.ini file to php5.ini.
Remove the php.ini file from your site if you choose to use another solution.
Further reading:
How to Remove the "Powered by WordPress" Footer Link
How to Secure a WordPress Website
Increasing Your PHP Memory Limit in .htaccess
You can also increase your PHP memory limit with your .htaccess file. This file is located in your root directory. It’s a “dotfile,” which are typically hidden by default in some FTP clients. If you don’t see yours, click Server in FileZilla, and select Force Showing Hidden Files:
Double-click the file to download it to your computer, and open it. Add this bit of code to it, or edit the line that’s already there if you find it:
php_value memory_limit 512M
Save the file, and upload it to your root directory, overwriting the original file. Refresh the FTP client, and refresh your site to see if the error you were trying to correct goes away. Try increasing the value if it does not.
Remove the code from the file and re-upload it to your site if you choose to try another solution.
What to Set Your WordPress Memory Limit and PHP Memory Limit to
This is a tricky question, and it may require you to contact your host. There are a few things we can do before it comes down to that, however, and they are:
- Determine the max memory limit set in default-constants.php.
- Trial and error.
Many of these solutions require you to access your site’s files, so I’ll go over how to do that via FTP for those who are still new to the more technical sides of WordPress.
Further reading:
How to enable and disable pingbacks and trackbacks in WordPress
How to safely disable the WordPress automatic update feature
How to update your WordPress installation
Finding Your Site’s WordPress Memory Limit
We can see what our WordPress memory limit is currently being set to by viewing the default-constants.php file. This file contains a few lines of code that deal with memory limits in WordPress.
Open your root directory in FileZilla. This is typically called public_html.
Open your site’s wp-includes file, and find your default-constants.php file. Double-click it to download it to your computer.
Open it with a text editor on your system. You should see a few lines of code that relate your site’s memory limit toward the top of this file:
What this tells us is that the memory limit in WordPress is set to 64MB for multisite installations and 40MB for regular WordPress sites. The WP_MAX_MEMORY_LIMIT setting underneath those lines of code relate to the amount of memory you’re allowed to use on the administrative side of things.
Basically:
- WP_MEMORY_LIMIT = front end
- WP_MAX_MEMORY_LIMIT = back end
This is how we’ll change our site’s WordPress memory limit in the wp-config.php file. We’ll use the define function from the default-constants.php file in the wp-config.php file to override the limit set in the default-constants.php file.
Determining What to Set Your Site’s PHP Memory Limit to
This is where things get a little tricky. How much memory you need depends on a few different factors, mainly the number of plugins you use and how much media exists (and will exist) on your site.
Generally, though, you should choose a number larger than what you found in the default-constants.php file, but it ultimately depends on which file you choose to edit, as you’ll see below.
This is also where we get into the “setting PHP memory limit through trial and error” thing. If you are trying to correct an error by increasing your site’s memory limit, you may need to try a base number first, test your site, and raise the number if your first test does not the correct the issue.
Be sure not to raise this number too high as it could crash your server. It’s unlikely, but it’s better to be safe than sorry. It’s difficult to give a particular maximum number, but you shouldn’t increase the number to anything that’s much higher than you need.
Basically, start at a number that’s higher than the default you found in the default-constants.php file (if you’re editing the wp-config.php file), and increase the number from there as you need.
It’s not recommended, but you can create a file called phpinfo.php. Add this code to it, and upload it to your root directory:
<?php phpinfo(); ?>
The information collected in this file will include what your PHP memory limit is currently set at. However, this is not recommended for novice users as having this file on your site leaves you vulnerable to hackers.
Contact us for Assistance
In case you tried both ways, and they don't work, or if you simply don't have the time to address the issue on your own, feel free to submit a ticket via your Client Area with us. If you are not yet our client, you can check our WordPress Hosting packages so you can enjoy the improved speed, advanced security, and expert technical support services it offers.