How to Fix Fatal Error Message in WordPress?

Stuck with this message “Fatal Error: Allowed memory size of 41943040 bytes exhausted”?

If you’re seeing this fatal error message, “Allowed memory size of 41943040 bytes exhausted”, don’t be scared. Thanks to WordPress, now I can make my own website from hundreds of themes. Sure, it took time for the platform to grow into a content management system and allow to make such resourceful websites.

However, without it, about 60 million websites now couldn’t function. Even though you might think the top websites do not use such 3rd party content management systems and develop their own with raw coding and everything. As a matter of fact, 39% of the top 10 million websites have been created and is operating through WordPress.

This is due to the range of options and customization that WordPress allows, too, without any cost. With the help of themes and plugins, you can implement as you imagine on your website. The WordPress repository itself has 58,164 plugins free of any cost. Besides that, 1500+ paid plugins are heavily used by the developers.

Till now, it might sound all glorious and flexible. However, one needs a bit of tech-savviness while operating WordPress too. Like any computer or technical operations, WordPress comes with its own sets of limitations and errors. Most of us have faced an error or two. Fatal error: the allowed memory size of 41943040 bytes exhausted is a fairly common one at that.

Some Common Errors you might encounter

Syntax Error, White Screen of Death (WSOD), Internal Server Error, 404 Error, Error Establishing a Database Connection, Connection timed out Error, Unable to upload images, unable to access the admin area, Stuck in Maintenance Mode, Schedule Post Error, Auto-Update error are to name a few of the common errors in WordPress.

Among these, the syntax error, as the name suggests, is an error due to mistakes within the written code. WordPress is humble enough to let you know precisely where the error is, and you can just head over to that specific function file and solve it. Unlike the syntax error, WSOD or the White Screen of Death is a more complicated one.

You will see a full white screen upon visiting the website, and it won’t tell you where the error is! However, tweaking the plugins, themes, clearing cache, and raising the memory limit might solve the problem. If they don’t, you can directly seek help from the WordPress debug mode. That will allow you to see errors on each page.

You might find a “The server encountered an internal error” page while trying to access a sit. That may also happen if the memory limit has reached, or maybe there is a problem with the .htaccess file. Error 404 might be a common sight for most of us. If you didn’t know that already, 404 indicates the file not found.

In the case of a website, the browser not finding the requested resource results in error. The .htaccess file directs the browser to sites, so such errors mean you need to recheck the file. When the site can’t access the database, it shows the error Establishing a Database Connection.

Check that you have given the right credentials on your wp-config.php file. If you did, then take the help of the WordPress automatic database optimization tool. Error 504, connection timed out error, is another one that might happen due to the low PHP memory. Not having enough hosting bandwidth can also result in a time out.

what is PHP Memory Limit?

If you look at the common errors we described earlier, you will see that a name pops up more than the others. The Memory Limit problem?

PHP’s memory limit’s on a per-script basis, just like the speed limit of the highway is per-vehicle. Say PHP’s memory limit is set up to 1GB. Now, this doesn’t make the scripts stack together on one another to use the 1GB we allocated early on.

Before we go deep into the Memory Limit discussion,

What is a script?

So, we have said that the memory limit is a per-script based setting. Now, what is a script?

A script is simply a written command or a written computer language that says the program what and how to execute. That is, the script sets the boundaries and is also the executioner. You need not compile a program every time you want to run it. Instead, you ask the script to do so. Since scripts are written codes, they are also easily editable. Scripts are run in 2 ways. One is on the server side, while the other on the client side. Most websites now a days uses both to optimize performance and also increase dynamism and functionality.

PHP, Perl, Ruby, Python, etc., are server-side scripting languages. This means that when you access a website, it runs the script on the hosted server and then displays the result to you. There is also client-side scripting language like JavaScript. Actually, JavaScript is used on both the server side and the client side. You may ask why is the website eating your resource by running it on your computer? Well, this is also to increase efficiency.

JavaScript is used to show interstitial objects on the screen, also to play web-based games. Running the script and sending the result back and forth would eat away a lot of time. Typically, scripts are open-sourced, and so users can view them and edit them! Pro tip: Right-click on any webpage and click on Inspect. You will find the JavaScript yourself along with the HTTP codes.

The greatest thing about scripts is that you can run them on any operating system with a little tweak here and there. Also, scripts are just commands. You will need the actual programs or resources to execute them.

Not a Storage Space

It is not like MySQL’s key_buffer_size options, PHP memory_limit constriction is not a storage capacity where many PHP scripts puddle from or build within. Instead, it’s a per-script boundary. PHP’s memory_limit indicates the maximum tally of server recall a PHP script can consume. When blocked, the shown error output turns out something like this:

Fatal error: Allowed memory size of x bytes exhausted (tried to allocate x bytes) in /path/to/PHP/script

or maybe: PHP Fatal error: Out of memory (allocated x) (tried to allocate x bytes) in /path/to/PHP/script

So, for instance, if two or further scripts are asked for simultaneously, each is without a string independent of another. They don’t distribute the memory_limit boundary. Remember, PHP isn’t developed for and doesn’t have options for multithreading. Thus, if you run seven (7) PHP scripts at the same time, with each using 100MB, that would hit up 700MB of PHP memory usage. A Memory limit of 128M is far away from being hit at this rate.

Having said that, for scripts that depend on other PHP script commands using functions like require, include, or include_once, then all the related scripts that the parent script is dependent on eats up the memory. Honestly, with so many functionalities and the need for dynamism, most scripts are related to others nowadays.

Why Keep this limit?

You might ask why to keep such limits after all! It’s not like the server has only that little RAM or memory! Why not straight up fix it to the extent of the memory the servers have. You must not forget that the website’s objective is to allow visitors a seamless experience.

If your code is too bulky, even the visitors will also face issues. They will have a hard time loading it on their pc, which will also affect their performance. So, WordPress typically sets the limit to 32M. Then, if that memory catches up, WordPress automatically increases the limit to 40MB if you have one site or 64MB if you have more than one. If you didn’t know, 41943040 bytes is equal to almost 40MB. That is why you face wordpress fatal error allowed memory size of 41943040 bytes exhausted and not an erro for 32 MB.

If you face memory limit issues, it’s a warning that your codes might not be that efficient and eat up more memories than usual. So, your first instinct should be to recheck your code and find the areas where you can improve.

An interesting fact is that less than 5% of the web hosting servers use the latest PHP version. Instead, they use an older one, which doesn’t even receive the new updates, and they do not bother to upgrade to the latest releases. So, check the version and match it with the latest found.

If the host is still living in the old days, give them a knock and ask them to install the latest version. The PHP 7.3 version has improved drastically that it can handle 3 times the requests of scripts in every second than the PHP 5.6.

Checking the PHP Memory Limit

How to Increase PHP Memory Limit? - BetterStudio

PHP has made drastic improvements with its new releases. While the PHP 7.3 is 3 times more efficient than 5.6, the options to view the Memory limit has haven there from the old version 5.2. Simply head over to the WordPress dashboard. Then from the “Tools” menu, find “Site Health.” From thereon, scroll down to the Info section. There you will find the “PHP Memory Limit” line. Next to the line is the numerical limit.

How to Fix it

Option 1: Increase through the PHP.ini file

You can straightforwardly increase the PHP Memory Limit when you are given access to edit the PHP.ini file. Most modest Shared hosting servers don’t usually give you such entry to the PHP.ini file. However, some servers let you build a copy of PHP.ini in your own site Directories. Then, the data/values will take the place of the default PHP.ini values.

For this, simply create a “php.ini” file in your directory where the WordPress site is. In your created script, just add the line, “memory_limit = 128M,” increasing the limit to 128MB. Now, do not worry if you fear that the main file will lose all the data. The newly created one can only change values that are in the original one. It can’t affect the command lines.

Option 2: Change the Limit through wp-config.php

This is another easy method and a risk-free at that. It is understandable if you do not have access to edit the .ini file or if you do not want to edit such sensitive files. In such cases, you can increase the limit by editing the wp-config.php file. Add the line ‘WP_MEMORY_LIMIT,’ ‘128M’, and thus, your limit is 128 MB.

Option 3: Modifying the .htaccess file

You usually won’t find the .htaccess file in WordPress, even though it is a must for raw coded sites. However, if you have activated the .htaccess file for a reason, maybe for a “301 Redirection,” you can use it to increase the memory limit. Just edit the file and add the command line “php_value memory_limit 128M” in the .htaccess file to increase the limit to 128 MB.

Option 5: Talk to the Hosting Service Provider

There is a good chance that your web host has limited the memory usage to a point, and no matter how you change the limit from WordPress, the sites can’t just access the needed memory. So, you should contact the service provider and ask them if they have done so. Work out a deal that might benefit you with the increased memory, or look for a better web host and opt for a private server.


Conclusion

With all the high-level language and frameworks in work all around us, it is reasonably easy to understand where the codes, the commands in, even the auto-generated scripts. Even without any formal training, or certification one can quickly figure out the solution to errors if shown the right direction. Hopefully, with our article, you now know about the fatal error: allowed memory size of 41943040 bytes exhausted and how to fix it. Before you tweak any documents, it is a safe approach to backup all the data using an FTP tool or a plugin.