Quantcast
Channel: InstaWP
Viewing all articles
Browse latest Browse all 998

How to Fix PHP Memory Issues in WordPress the Right Way

$
0
0

You don’t need to panic when encountering a PHP-allowed memory-size exhausted error. It is a common error, and various ways exist to resolve it efficiently.

This guide will help you understand the PHP-allowed memory exhaustion error from the causes and how to identify and walk you through various ways to resolve it.

Note: If you have a site dealing with customer data, try fixing this error in staging and then deploy the changes to the live site. Here’s how to create staging of your live site quickly →

What is a PHP Memory Exhausted Error?

PHP memory exhausted error is an error message that appears when a PHP script in your WordPress site consumes more memory than the default limit set by your hosting server. The error helps the server maintain stability by cutting off your site’s resource access.

Usually, when you buy a hosting plan for your site, your hosting provider allocates a certain amount of server memory for PHP scripts to utilize on your site. However, the more content and plugins you add to your site, the higher the PHP scripts and demands on memory. When you exceed the memory allocated or attempt to install more plugins or themes that will cause your site to exceed the memory limit, WordPress will display the PHP allowed memory size exhausted or PHP memory limit error.

With this basic understanding, let’s discuss how to check your WordPress site’s PHP memory limit and how to resolve it anytime you encounter it.

Causes of PHP Memory Exhausted Error in WordPress

The common culprits responsible for triggering the php allowed memory size exhausted error include:

Inadequate Hosting Resources

You will likely encounter this error if you use shared or cheap hosting for your WordPress site. This is because your site will run on servers with limited resources.

Resource-Intensive Plugins and Themes

Bloated or heavy plugins and themes that send many requests to your server can consume a large volume of available server resources, leading to a PHP-allowed memory size exhausted error.

Too Large Database

Just like resource-intensive themes and plugins, a large or bloated database can also exhaust the default PHP memory size as many requests are made simultaneously to retrieve various data from the database.

Website Growth

As your WordPress site grows, more content is added to the database and storage space, requiring more server resources. This usage can easily exceed the allowed memory size limit, triggering the error.

Bad Coding Practices

Some developers write codes that send a lot of unnecessary calls and requests to the server, straining the server’s resources and exhausting the PHP memory faster.

How to Identify PHP Memory Exhausted Errors in WordPress

Identifying the PHP-allowed memory size exhausted error in WordPress doesn’t require rocket science. You can resolve the error and return your site to normalcy.

Reviewing Error Logs and Debugging

Every WordPress site has an error log file that logs every error encountered. By reviewing the error logs, you can identify the root cause of the error and have it fixed to resolve the issue.

Using Performance Monitoring Tools

Query Monitor, wp_debug, GT Metrix, Pingdom, and Google Page Speed Insight Tool are performance monitoring tools that can identify plugins, themes, site activities, call requests, or configurations straining your server’s resources and provide suggestions on how to optimize them.

How to Check PHP Memory Limit in WordPress

When you encounter an exhausted memory size limit error in WordPress, you might want to check and confirm your current memory limit to know how much to increase it.

You can check your WordPress PHP memory limit directly on your WordPress site.

Log into your WordPress site. From the side Admin menu, go to Tools > Site Health. Click on the Info tab and click on the Server accordion. You should be able to locate the PHP memory limit from the list of information. 

Checking PHP memory limit in WordPress

Now that you know your site’s present PHP memory limit, you can increase it.

Solutions for Fixing PHP Memory Exhausted Error

Before proceeding, you must be aware that some of the options we will explore in this guide will require you to edit important WordPress files. If done wrong, this can break your site or compromise its smooth running, making it difficult to resolve the arising issues.

So, you must create a backup of your site or any file you will be editing before making any changes so that you have something to fall back on if things go wrong.

You can back up your live WordPress site using any WordPress backup plugin, such as Duplicator, Updraft Backup, etc., or by pushing it to a staging environment like InstaWP before you proceed with any modifications.

So, let’s explore how you can fix the dreaded PHP memory exhaustion error.

Increasing Memory Limit via wp-config.php File

You can resolve the error by editing your wp-config.php file in WordPress to increase your site memory limit. To do this, open the wp-config.php file with an FTP client or the code editor in your cPanel. You can find the file inside the public_html folder. Then, find the following code and increase the value there, but be careful not to exceed the limit allocated by your hosting provider.

define(‘WP_MEMORY_LIMIT,’ ‘256M’);

If you can’t find the code in the file, insert it just above “That’s all; stop editing! Happy blogging.”

Adjusting PHP.ini Settings

The PHP.ini file is a server-side PHP configuration file that defines how PHP code should behave on a site, including file uploads, error handling, memory limit, etc. You can, therefore, modify the memory limit in the PHP.ini settings. To do this, locate the PHP.ini file in your site’s root directory, i.e., public_html. If it doesn’t already exist, you can create one with the following code and upload it to the root folder:

memory_limit=256M

When you are done editing the PHP.ini file, you can save the file and check again if your memory limit has been increased.

Edit  .htaccess File

Another way you can increase your memory limit is by editing your WordPress site’s .htaccess file. You can add the following code after the # End WordPress line to do this.

php_value memory_limit 256M

Upgrade Your Hosting Plan

Suppose your hosting provider is flagging your site for using more than your allocated or permitted server’s resources. In that case, upgrading to a higher plan with more resources and memory can increase the memory limit benchmark.

Smartest Solution

The above methods involve editing sensitive files, which, when edited wrongly, can lead to more complications and errors. To avoid this, you can use InstaWP to change the memory limit. If you manage your WordPress sites using InstaWP, you can just go to configurations and change the PHP memory limit in a few clicks. This can be done for all your sites.

How to Increase PHP Memory Limit in WordPress using InstaWP

You can either use InstaWP to easily change your WordPress site’s PHP memory limit without coding or use the InstaWP code editor to modify the PHP.ini file on your server.

Updating PHP Memory Size

First, create a staging site using the InstaWP Connect plugin on your live site to use this option. Then, ensure that Start Listening for Changes is turned on from the Sync (Beta) tab on both the staging and live sites.  

Enabling sync listening in InstaWP Connect

Also, go to the Settings tab, and under Sync Settings, ensure WP Options (Beta) is tuned on on both the staging and live sites.

Enabling WP options in InstaWP Connect

Then, return to your InstaWP dashboard and click on Staging. Click the three-dotted more actions button next to the appropriate staging site, and select PHP Config from the dropdown list.

PHP config in InstaWP

Locate the Memory_limit (mb) field, increase the memory limit, and click the Save button. That’s it. The PHP fixed the memory size exhaustion error, and your site should be working again.

increasing PHP limit in InstaWP

Return to your staging site to effect this change, open the Sync (Beta) tab from the InstaWP Connect interface, and sync the changes.

This is the best no-code approach to resolving the PHP-allowed memory-exhausted error in WordPress sites.

Adjusting PHP.ini on Your Server using Code Editor

Alternatively, you can use the InstaWP code editor to edit your site’s PHP.ini file to adjust the memory size limit without using cPanel or an FTP client. This option also requires the creation of a staging site. It doesn’t matter if it’s a full, quick, or custom staging site, but a quick staging will suffice since you’re interested in the PHP.ini file and not your site’s content.

Click the three-dotted more actions button next to the corresponding staging site, and select Tools > Code Editor from the dropdown list.

opening code editor in InstaWP

Select Open in Web Editor from the popup to use the free code editor. 

InstaWP visual and web editor

Then, using the InstaWP code editor, locate and modify the PHP.ini file from the root directory. If you can’t find it, go to Files > New File from the left panel to create one.

InstaWP code editor interface

When you are done, you can return to IstaWP on your staging site and sync the changes to your live site to implement the memory increment.

How InstaWP Helps in Resolving PHP Memory Issues

InstaWP is a must-have toolbox for WordPress developers and agencies. It can help create staging environments for testing memory limit settings, debugging issues, and managing multiple WordPress sites from one place.

Testing Memory Settings on Staging Environments

With InstaWP, you can create sandbox sites to test various PHP memory limits and identify the optimal memory limit needed for a smooth experience.

Using Staging Sites for Debugging

You can also use staging sites created using InstaWP to debug allowed memory size exhausted errors and resolve the issues without compromising the live site.

Managing your Sites with InstaWP

InstaWP provides a centralized dashboard for monitoring and managing multiple WordPress sites. It also comes with a built-in performance monitoring tool that can help you proactively address potential memory-related issues.

Backup and Restore

If you encounter a memory limit exceeded error immediately after a site update or significant change, you can use the InstaWP backup and restore feature to revert your site to a previous state without the error.

Preventative Measures to Avoid PHP Memory-Exhausted Errors

Let’s discuss various means of preventing the recurrence of the allowed memory size being exhausted for a smooth, uninterrupted user experience.

Optimizing Website Resources

When you reduce the demand for your server resources, you keep memory usage at a minimum. 

To optimize your site resources, you should minimize the sizes of images, scripts, and stylesheets. You can use image optimization plugins for image compression and minification tools like Autoptimize to reduce the sizes of CSS and JavaScript files. 

You can also use plugins like Assets CleanUp to restrict styles, scripts, and plugin assets by page so that they only load on pages requiring them.

Regular Updates and Maintenance

Regularly updating your WordPress core, plugins, and themes will help your site benefit from improved and optimized coding, performance, and security patches, which enhances the site’s efficiency and reduces memory usage.

Enable Caching

Enabling caching will significantly reduce call requests from PHP scripts and stylesheets by serving static pages and utilizing users’ browsers to store and preload some of the site’s resources. Providers like InstaWP Live offer 1-click caching facility.

Regular Monitoring

Using tools like Query Monitor to closely monitor your site’s resource usage regularly can help you identify scripts or plugins that consume a high amount of your server’s resources and optimize them.

Remove Unwanted Plugins

Remove unused, bloated, and resource-consuming plugins from your site to free up memory.

Use Efficient Coding Practices

Reevaluate and refine your code, eliminating unnecessary functions, loops, variables, call requests, and scripts. Instead of creating custom ones, take advantage of built-in functions and hooks. These practices will reduce resource and memory usage. 

Upgrade Your Hosting

If your hosting provider doesn’t allow you to increase your PHP memory limit or if your current hosting plan can’t accommodate your site’s current need, then upgrading your hosting to a better plan or a better hosting provider is best. 

You should also consider using a cloud-based hosting server over a shared or VPS hosting server, as cloud-based hosting utilizes the power and resources of combined servers on multiple locations compared to shared and VPS hosting, where they just share a single server’s resources. 

If you wish to migrate to a more reliable and cloud-based hosting provider, we recommend InstaWP Live, a managed WordPress hosting provider with better rates and features.

Conclusion

You can quickly resolve the PHP allowed memory size exhausted error by increasing your PHP memory limit or upgrading your hosting. We recommend using InstaWP to manage your WordPress site and swiftly resolve the memory limit error. We also recommend InstaWP Live managed WordPress hosting for a better hosting experience.


Viewing all articles
Browse latest Browse all 998

Trending Articles