Sometimes when trying to perform an action in WordPress, such as activating a plugin, you might get an error that looks like this:

Fatal error: Allowed memory size of 987654321 bytes exhausted (tried to allocate 12345 bytes)

If you’re running resource-intensive plugins or themes, there may come a time when you find yourself hitting this error. Basically, it just means that the server needed more memory to perform the task than was allocated. The error specifically relates to memory for PHP, which is the language that WordPress runs on. The solution is to increase the amount of memory allocated to PHP.

Understanding WordPress memory allocation

Before you go changing your PHP memory allocation, it’s really important to understand two key points.

Firstly, there is memory allocation for PHP, then there is an allocation of how much of that PHP memory can be used by WordPress. If your server is solely dedicated to hosting a WordPress website, then typically these values would be the same. However, you can allocate less PHP memory to WordPress specifically if you were running other PHP applications on your server. WordPress memory can only be increased while it is less than the PHP memory allocation – otherwise increasing the WordPress memory limit will do nothing.

The second thing to understand is that both the PHP and WordPress memory allocations don’t refer to total memory, but rather the amount of memory allocated per script that is run. If multiple scripts are running, then the memory allocated will be multiplied by the number of scripts. This means that specifying a larger-than-necessary memory allocation may cause your server to run out of memory. In this case, more is not necessarily better – just enough to ensure the task no longer triggers a memory exhausted error is best.

How to increase the WordPress PHP memory limit

If you’re using a hosting service, changes to these files should take effect immediately. If you’re running your own server, you’ll need to restart both PHP and the web server for changes to take effect, or you can reboot.

php.ini

Let’s look at increasing the PHP memory limit first, since WordPress can’t allocate more memory than is specified here. Depending on your host, you may or may not have access to this file, and you may or may not be able to create your own. Search your web host’s knowledge base for php.ini and you should come up with where to locate the file.

Once you’ve located the file, it’s simply a case of adding the following line (or editing the existing one if it is already specified). In this example, we’re allocating 128MB of memory.

memory_limit = 128M

.htaccess

If you don’t have access to your php.ini file, sometimes you can use your .htaccess file to set the memory limit:

php_value memory_limit 128M

wp-config.php

Now that we’ve increased the PHP memory limit, we can specify how much of that memory is allocated for use by WordPress. The default memory allocated to WordPress is 32MB but we can increase that by editing the wp-config.php file located in the root folder of your WordPress installation. If you have WooCommerce installed, they recommend a memory limit of 64MB, some complex themes will require a limit of 96MB and most websites shouldn’t need more than 128MB. The best approach is to increase the limit incrementally until the error goes away.

Locate the line of text that reads /* That’s all, stop editing! Happy publishing. */ and place the following code just above that line. In this example, we’re allocating memory of 128MB.

define('WP_MEMORY_LIMIT', '128M');

The WordPress Dashboard is more memory intensive than your website, so it may be that it’s just the WordPress administration area than needs more memory rather than your website. You can allocate memory for the WordPress administration area using the following code. This can be specified in addition to the example above and should always be a higher value. Here we’re allocating 256MB memory for WordPress administration, but remember that your PHP memory needs to be the same or higher than this value for it to work.

define('WP_MAX_MEMORY_LIMIT', '256M');

If you’ve been experimenting with getting the correct memory allocation to resolve a memory exhausted error, you may need to go back and amend the memory limit in the php.ini file. If your server is used solely for your WordPress website, then the PHP memory allocation and the WordPress memory allocation should be the same.