WordPress Memory Tuning


Revision as of 09:37, 25 August 2023 by Wikiadmin (talk | contribs)

WordPress is an extensible system and will occasionally require tuning to run efficiently.  You may also have to tune WordPress if you have a high number of concurrent users, a large number of Plugins or a Plugin or Theme that requires a lot of resources to run. At Bravenet there are two different methods which control the memory settings that can be tuned.  There are the PHP environment settings and there are the standard WordPress settings.

 

Further Details

For the WordPress environment, it is also necessary to distinguish between WordPress frontend and backend functions.  

WordPress Memory Tuning Settings - WP_MEMORY_LIMIT & WP_MAX_MEMORY_LIMIT

Frontend WordPress functions, run to serve your website to your end users and can be tuned with the WP_MEMORY_LIMIT located  in the wp-config.php file.   If for example, you add a plugin whose purpose is to interact with the users on your site and the plugin requires more memory to run efficiently or to prevent errors from occurring this is the WordPress frontend.   WooComerce is an example plugin that is often known to benefit from increasing WP_MEMORY_LIMIT.

Backend WordPress functions, run from within WordPress’ administration dashboard and can be tuned with the WP_MAX_MEMORY_LIMIT located in the wp-config.php file.   If you are running a task from the WordPress Admin Console (manual or scheduled) and are receiving errors it is possible that you need to tune WP_MAX_MEMORY_LIMIT. You would use the methods outlined by the WordPress team to tune both of these settings.

PHP Memory Tuning Settings - memory_limit

The second tunable method that can be used at Bravenet is the method to tune the PHP environment settings. The PHP tunable parameters are configured by editing a .htaccess file in the root of your website.  The most important PHP tunable parameter related to WordPress memory tuning is 'memory limit'.    For example if you placed the following in your .htaccess file you would set the PHP environment's memory limit to 256 MB:

php_value memory_limit 256M

Relationship Between PHP and WordPress Settings

There are important considerations that need to be taken when changing any of these values which can be simply summarized as:

WP_MEMORY_LIMIT <= WP_MAX_MEMORY_LIMIT <= PHP memory_limit


What the above is trying to convey is you should always ensure that:

  1. PHP memory_limit is greater than or equal to WP_MAX_MEMORY_LIMIT
  2. WP_MAX_MEMORY_LIMIT is greater than or equal to WP_MEMORY_LIMIT


Note: Before performing any major changes to your WordPress website, it is always good practice to backup your website and database.

Things to consider:

  1. Whenever you change one or more of these values please ensure that you review the logic above for the other values
  2. WP_MEMORY_LIMIT should be set as low as possible for maximum efficiency of your WordPress frontend (default 40 MB, Max 768 M)
  3. WP_MAX_MEMORY_LIMIT (set in wp-config.php) greater than or equal to WP_MEMORY_LIMIT (set in wp-config.php)  but less than or equal to PHP memory_limit (set in .htaccess). This setting is for WordPress backend processes (Anything running as WordPress Administrator) as WordPress administration tasks can take more memory (default to 256 M Max 768 M)
  4. Bravenet allows a maximum of 1024 M for all websites in an account

Default WordPress Memory Values

If you are trying to tune your WordPress memory parameters a good approach is to look at what the default values for WordPress at Bravenet are and modify them accordingly:

PHP 8.1 and WordPress 6.1.1
Parameter Value
PHP memory_limit 256 M
WP_MAX_MEMORY_LIMIT 256 M
WP_MEMORY_LIMIT 40 M

Tuning Recommendations

Scenario 1: Increasing WordPress Frontend Performance (Most commonly required)

If you are wanting to increase the website response time to your website users because you have a high number of concurrent users and you are using resource intensive plugins like WooCommerce you may want to increase the value of WP_MEMORY_LIMIT to a higher value to allow your site to run more efficiently. Choosing a value of '256 M' would be a good starting value. You could monitor the memory usage over a time with a memory diagnostic plugin like 'Memory Usage, Memory Limit, PHP and Server Memory Health Check and Fix Plugin' or equivalent.   In the default Bravenet environment you could increase WP_MEMORY_LIMIT up to '256 M' without changing any of the other parameters in this article. Increasing the value of any of these limits too much can actually decrease the performance/efficiency of your site so understanding these values and setting them correctly is important.

Initial WordPress Frontend Tuning Values
Parameter Value
PHP memory_limit 256 M
WP_MAX_MEMORY_LIMIT 256 M
WP_MEMORY_LIMIT 256 M

Scenario 2: Increasing WordPress Backend Performance

If you have a plugin that you run from the admin console that causes PHP errors or just simply a highly memory intensive operation you may want to increase  the value of WP_MAX_MEMORY_LIMIT (and if you do you will also need to update the PHP memory_limit to be greater than or equal to the same value. We would recommend the following increase in values and monitoring your site before making further changes.

Initial WordPress backend Tuning Values
Parameter Value
PHP memory_limit 384 M
WP_MAX_MEMORY_LIMIT 384 M
WP_MEMORY_LIMIT No Change

Other Notes

Maximum Total memory per account

Maximum per website settings

References

Reference 1 https://make.wordpress.org/hosting/2022/08/18/proposal-updating-wp_memory_limit/

Reference 2 phpinfo.php (Do not leave this enabled on your server as this could be used for malicious purposes)

<html>

<head> <title>PHP Variable and Configuration</title> </head> <body> <?php phpinfo(); ?> </body>

</html>

Reference 3 Current WordPress Memory Limit Defaults

WordPress Admin > Site Health > Info > WordPress Constants > WP_MEMORY_LIMIT/ WP_MAX_MEMORY_LIMIT