How to Fix “Fatal error: Allowed memory size of X bytes exhausted” in WordPress

Posted on Updated on

WordPress is a PHP application which usually runs on a Linux environment. The PHP process has to be allocated a certain amount of memory to carry out its tasks. We can create fine lines and tell PHP which applications need more and which applications need less memory.

By default, WordPress tries its best to assign 40 MB for a single site installation, and 64 MB for a multi site installation. However, these settings can be overridden by your hosting provider. It’s not always easy to tell which is the definitive memory limit, because there are so many and some take precedence over others. For example, you can change the memory limit for WordPress in the following locations:

  1. wp-config.php
  2. php.ini (Perhaps multiple php.ini files!)
  3. cPanel

Sometimes when WordPress runs out of memory, you get the well known error that looks like this:

This error means that whatever memory allocation you currently have for WordPress, isn’t enough. Before we go about fixing this, we need to find out what the current memory allocation is.

Getting the Current PHP Memory Allocation

As mentioned above, there are many locations in a typical PHP installation which talk about memory allocation. However, if we want to find out the actual memory that PHP has allotted, we can use the following script:

function get_the_memory_limit() {
    $current_limit     = @ini_get( 'memory_limit' );
    $current_limit_int = wp_convert_hr_to_bytes( $current_limit );

    send_to_console("Current PHP Limit: " . $current_limit_int);
    send_to_console("Location of PHP.ini File: " . php_ini_loaded_file());

add_action('wp_head', 'get_the_memory_limit');

function send_to_console($debug_output) {

	$cleaned_string = '';
	if (!is_string($debug_output))
		$debug_output = print_r($debug_output,true);

  	$str_len = strlen($debug_output);
  	for($i = 0; $i < $str_len; $i++) {
    		$cleaned_string .= '\\x' . sprintf('%02x', ord(substr($debug_output, $i, 1)));
	$javascript_ouput = "<script>console.log('Debug Info: " .$cleaned_string. "');</script>";
	echo $javascript_ouput;

Paste this into your functions.php or wherever you put your custom PHP code. It consists of two functions – one to get the current PHP memory allocation, and the other to display the results in the browser console. If you want, you can also just echo the statements onto your screen. After pasting and saving this code, open your WordPress site in a browser and bring up the debug console. In Chrome, you do this using ctrl+shift+c and open the “Console” tab. You should get an output similar to the one below:

This tells me that the current PHP memory allocation is 268435456 bytes, which translates into exactly 256 MB. This is usually more than enough for WordPress to run comfortably even on large sites. In addition, I’ve used the following function to get the location of the php.ini file:


This gives us the location of the current loaded php.ini file. In my case, it’s hidden away in /etc/php.ini. If you’re on a shared hosting plan, you most likely don’t have access to this particular folder. So even if you want to change it directly, you can’t. Luckily for us, there are ways to get around this if your host allows it.

Changing the Memory Allocation

To change the PHP memory allocation with regard to WordPress, open up your wp-config.php file located in the root directory of your WordPress installation. There, we need to add one of the two following lines:

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


@ini_set( 'memory_limit', '64M' );

In the above lines, replace “64M” with a value higher than the one that’s giving you the fatal error. You can make it as high as your host permits. As mentioned above, 64M is the default for multi-site installation, but there’s nothing stopping you from making it 256M if you want. Keep in mind that none of this might be even possible if your host has configured your PHP environment in WordPress to not go above a certain value.

After making the above changes in wp-config.php, you can run the piece of code I gave earlier. This time you should see the new limits as shown here:

As you can see, it’s now 64 MB as expected. There is yet another way to change the PHP memory limit, and that’s by placing your own php.ini file in your WordPress folder with the line:

memory_limit = 96M

Or whatever memory limit you want. Once it’s in your WordPress root, it becomes the php.ini file for that particular folder and its subdirectories. Once again, running the piece of code above will reflect this:

You can see that it’s now displaying the location of the new php.ini file – this time inside the “public_html” folder which is the root directory of my test WordPress blog. In addition, the PHP limit is now 96 MB as specified!

Any of these solutions will work for you. If your host has allowed your account to change the PHP memory allocated to a particular application, then either modifications to the wp-config.php file, or a new php.ini file will get the job done.

Leave a Reply

Your email address will not be published. Required fields are marked *