How to fix WordPress Fatal Error – Allowed memory size exhausted?

How to fix WordPress Fatal Error – Allowed memory size exhausted?

Memory exhausted error is a very common fatal error that WordPress users run into – If you`re also experiencing this or any similar fatal errors in WordPress, you can use this detail guide to understand and fix this error.

Why you are getting PHP Memory exhausted Error in WordPress?

Maximum Execution Time Exceeded Error

Maximum Execution Time Exceeded 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

Most of the time this error comes when PHP scripts crosses maximum value than what you (Your-Web-Host) set up PHP to handle. Shared hosting providers usually set the time limit value for PHP scripts to minimum and when it cross the maximum defined value you will get this error “maximum execution time exceeded”.

There are few more fatal errors in WordPress, you can use solutions given below to solve any of these fatal errors…

  • Fatal error: memory exhausted error
  • Fatal error: call to undefined error
  • Fatal error: cannot redeclare error

You may encounter these errors while activating plugins or installing/upgrading one of your themes. Unexpectedly, sometimes you will get this error when your blog is updated automatically to the newest WordPress Version or while updating your menus.

“Hope now you understood, what the Fatal Error is and why you are getting this, Let`s start fixing PHP Fatal Error.”

How to resolve Memory related errors in WordPress?

“We strongly recommend to make a backup of your entire site before starting any solutions. It`s also a Good way to use any sub-domain or staging platform to test and fix issues then you can deploy it later to your production site.”

There are numerous ways to fix WordPress Fatal errors, Below are few steps you should follow in order to fix Fatal Errors in WordPress. Let`s see How you can easily fix Fatal errors for WordPress:

Fixing Fatal Errors by Increasing Memory limit for PHP Processes in WordPress

The first and most effective solution is to increase memory limit for PHP. It will simply increase memory limit for all PHP processes of your site and fix this error. There are 3 different ways to increase PHP memory limit for WordPress websites.

Solution-1: How to Increase PHP Memory limit by wp-config.php file in WordPress?

“Wp-config.php is a most important file for your WordPress installation. This file is a resource for storing database connection and site base configuration details. Always make a backup before editing or updating this file, so in case if something will not work you can always upload your default file.”

How to Increase PHP Memory limit by wp-config.php file in WordPress

How to Increase PHP Memory limit by wp-config.php file in WordPress

Increase PHP Memory Limit in WordPress By using a Plugin

To Download and edit wp-config.php file, you can connect with your site using any FTP client and navigate to root WordPress directory. Now you can download wp-config.php file from root WordPress directory to your PC.

Open wp-config.php file and search for this statement that says:

/* that’s all, stop editing! Happy blogging. */

Replace above lines with this code and save the file:

define('WP_MEMORY_LIMIT', '64M');
/* That's all, stop editing! Happy blogging. */

This code will increase memory limit for all PHP processes to 64MB. Now you can upload updated wp-config.php file to your server, and check the site again to verify if everything working fine. Don`t forget to check with clearing the cache for your browser.

If above solution doesn`t work and you are using shared Hosting, you may contact your hosting provider to increase PHP memory limits.

“With Self or Managed WordPress Hosting you can increase the memory at maximum as long it does not exceed the Installed RAM memory of server”.

Solution-2: How to Increase PHP Memory limit by PHP.ini file in WordPress?

PHP.ini is an configuration file to run PHP applications, It control variables such as upload sizes, resource limits, file timeouts and other PHP settings.

How to Increase PHP Memory limit by PHP.ini file in WordPress

How to Increase PHP Memory limit by PHP.ini file in WordPress

Most probably you will not see this file if you are using shared hosting for your WordPress site, if you have access to your php.ini file in your root folder you can download this file and start following below steps: (If you can`t find this file in root directory, See Below)

“If you’re running on a Linux server, your php.ini file should be located in etc/php.ini”

  • Download and open php.ini file with notepad or Dreamweaver
  • Now search and find following lines in the php.ini file
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
  • And replace it with a higher value like below
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
  • Save the changes and upload php.ini file to server.
  • If you get any issues, you can change 64M to 10M and check again.
  • If you still have issues, open wp-admin directory and search for the same file php.ini and make same changes like above.

When you Don`t have a php.ini file in your directory?

If you do not see any php.ini file under root or wp-admin directory, then just create a new php.ini file and define above higher values.

[You can create a new notepad file and rename it from “New Text Document.txt” to “php.ini” to easily make this file.]

Also, you can contact your Host to create this file for you, you can also generate php.ini file using your hosting control panel. Below are steps listed to generate php.ini file by some of reliable Hosting Providers.

If you are still unable to get php.ini file from anywhere (or want to check the demo file), You can Download this php.ini file. This file is already updated for upload_max_filesize and the post_max_size to 64M. You can download this file to your pc and upload to your root and wp-admin directory.

In case if you get this error “Your PHP installation appears to be missing the MySQL extension which is required by WordPress.” You can try to restart your php from Hosting Control panel or use the below method to increase PHP memory with .htacess

Solution-3: How to Increase PHP Memory limit by .Htaccess file in WordPress?

.Htaccess or Hypertext Access file is also a configuration file which is using by Apache-based web servers to control behavior of any site or any specific directory of your website.

How to Increase PHP Memory limit by .Htaccess file in WordPress?

How to Increase PHP Memory limit by .Htaccess file in WordPress?

If above solutions doesn`t do the trick, you can open your .htacess file and add the following code at the bottom:

php_value upload_max_filesize 64M
php_value post_max_size 640M
php_value memory_limit 64M
php_value max_execution_time 64m
php_value max_input_time 64M

You can change or increase above values to whatever limits you require according your server configuration or number of sites you are running on that server. Once you have updated the code you can save and upload this .htacess file to your server root directory via FTP.

Save the file and clear your browser’s cache to see changes.

How to Increase PHP memory limit for specific function in WordPress?

If you are familiar with PHP coding and don`t want to increase memory for all PHP processes. You can give extra memory to specific functions which require extra memory so when the code gets called it will use increased memory rather than increasing memory for all PHP processes.

Below is a code snippet to achieve this function:

<?php
function get_usage_in_kb(){
    echo memory_get_usage()/1024.0 . " kb \n";
}

class Profiler
{
    protected $startTime = 0;
    public function timerStart(){
        $this->startTime = microtime(true);
    }

    function timerEnd(){
        echo number_format( (microtime(true) - $this->startTime)*1000, 2 ), ' ms';
    }
}

class ProfilerHolder {
    protected $profiler;

    public function __construct( Profiler $profiler ){
        $this->profiler = $profiler;
    }
}

ini_set('memory_limit', '1M');

$profiler = new Profiler();

$profiler->timerStart();

get_usage_in_kb();

$holder = new ProfilerHolder( $profiler );

$b = null;

/**
 * @return string
 */
function make_dummy_data()
{
    return str_repeat( "Hello|", 114242 );
}

$a = make_dummy_data();

$data = explode( '|', $a ); // comment this after narrowing down that a lot of memory is being used here
//$data = array(); // uncomment this and memory usage will drop

$buffer = array();

foreach( $data as $key => $item ){
    $buffer[] = $item;
}

get_usage_in_kb();

unset($a);

get_usage_in_kb();

echo memory_get_peak_usage(TRUE)/1024, ' kb', "\n";

$profiler->timerEnd();

unset( $profiler );
xdebug_debug_zval( 'profiler' );

//var_dump( $holder );

Source

What is WordPress Admin Hardcoded Memory Limit Patch?

In WordPress there is a memory limit of 256mb for WordPress Core. These memory settings cannot be configured with WordPress as they are hardcoded in backend.

“Implement Patch 15966 (recommended)”

You can configure memory limit in wp-config.php but the hard coded values prevent it. If you want to increase the limit of your WordPress admin section you can try this Patch to Modify the WordPress memory properly.

How to Monitor your WordPress Memory?

How to Monitor your WordPress Memory?

How to Monitor your WordPress Memory?

If you are regularly working with WordPress or installing new plugins everyday, you`ll probably face memory limit errors again and again. To monitor your server and WordPress site there are some free plugins which you can use to monitor everything.

WP System Health
TPC! Memory Usage
WP System Health

With all above tips and solutions you can fix memory exhaustion errors. If you are still getting issues please let us know in comments, so we can help you to fix any issues. Feel free to share your experience in the comments below.