WP Constant “WP_MAX_MEMORY_LIMIT” Clarification Question

Home Forums BulletProof Security Pro WP Constant “WP_MAX_MEMORY_LIMIT” Clarification Question

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #42103
    Living Miracles
    Participant

    Hello,

    I’m writing because recently SiteGround recently inserted a line of code into our wp-config.php file across all our sites which increased the WP_MEMORY_LIMIT constant (in WP backend > Tools > Site Health > Info > WordPress Constants) from 40M to 256M. I initially though that setting the memory limit to 256M would represent a decrease in our memory limit, because on a number of our sites, the “Memory Limit” field in BPS Pro > PHP Options > ini_set Options is set to values higher than 256M (often 512M or higher.) However, it seems like this field may not impact the WP_MEMORY_LIMIT WordPress constant how I was thinking it would.

    Based on what I can see, it seems like the “Memory Limit” field (in BPS Pro > PHP Options > ini_set Options) is the same value as the values that appear in BPS Pro > System Info > “PHP Actual Configuration Memory Limit” and “WordPress Admin Memory Limit”. These values also appear to be identical to the WP_MAX_MEMORY_LIMIT value in our WordPress backend. Separately, we have the “WordPress Base Memory Limit” value in BPS Pro > System Info, which appears to be connected to our WP_MEMORY_LIMIT constant in our WordPress backend and it is this value that appears to be changed when Siteground edited the wp-config.php file.

    Given that the above is correct (and please let me know if it isn’t), I’m curious why this WP_MAX_MEMORY_LIMIT variable was created through BPS Pro, what the variable is actually changing about our site memory and how it is used, and how this variable is different from this other “WP_MEMORY_LIMIT” constant in the WP backend. Any context you could give on these two variables and how they impact our site would be much appreciated.

    Thanks!

    #42104
    AITpro Admin
    Keymaster

    PHP Actual Configuration Memory Limit is the php memory limit set by your host server.  BPS is retrieving that value from your server’s php config file.  PHP memory limit is allocated per site and not per hosting account.  The typical/average PHP memory limit these days is 256M per site.  This is more than enough memory to run a WordPress site.

    WP_MEMORY_LIMIT is the amount of php memory that you want to allocate to WordPress.
    WP_MAX_MEMORY_LIMIT is the maximum amount of allowed php memory that WordPress is allowed to consume.

    If a script or something else causes your php memory limit to hit the maximum allowed memory allocated to WordPress the script will not complete.  This is a good thing because a poorly written script that uses up all of your allocated memory will cause your website to crash.

    BPS does not use the WP_MAX_MEMORY_LIMIT.  So that was created by something else.  BPS has an option setting for:  Max Execution Time in PHP Options > ini_set Options.  This option setting sets the maximum time in seconds a script is allowed to run before it is terminated by the parser. This helps prevent poorly written scripts from tying up the server. The default setting is 120.

    Also they way WordPress uses the WordPress WP_MAX_MEMORY_LIMIT constant is that this constant applies and enforces whatever value you set for this constant to the WP Admin backend and not the frontend of a WP site.

    #42117
    Living Miracles
    Participant

    Hello,

    Thanks for sharing all this context about the different variables. A few follow up questions are:

    1.  If the “PHP Actual Configuration Memory Limit” value (in BPS Pro > System Info) retrieves the value from the server’s php config file, why is it that when I change the “Memory Limit” value (in BPS Pro > PHP Options > ini_set Options), the PHP Actual Configuration Memory Limit value changes to be identical to the “Memory Limit” value in BPS Pro? I would expect the PHP memory limit allocated by the server to stay constant, but that’s not what I’m observing. For exact steps, I used miraclesacademy.org, noticed that the Memory Limit value was set to 768M, and verified that the PHP Actual Configuration Memory Limit was the same. I then changed the “Memory Limit” value to 766M, and verified that the PHP Actual Configuration Memory Limit was 766M

    Is it possible that this “Memory Limit” value in BPS Pro is somehow editing the value in the server’s php config value, or is it that the PHP Actual Configuration Memory Limit represents something else?

    2.  If BPS Pro does not use the WP_MAX_MEMORY_LIMIT constant, why does this constant change to be identical to the “Memory Limit” value (in BPS Pro > PHP Options > ini_set Options)? Once again, I used miraclesacademy.org, noticed that the Memory Limit value was set to 768M, and verified that the WP_MAX_MEMORY_LIMIT constant was the same. I then changed the “Memory Limit” value to 766M, and verified that the WP_MAX_MEMORY_LIMIT value changed to 766M whereas the WP_MEMORY_LIMIT constant remained unchanged. Is the above results of changing the “Memory Limit” value in BPS Pro > PHP Options > ini_set Options not expected behavior? If it is not, what should the Memory Limit value in BPS pro correspond to?

    Thanks for your help!

    #42118
    AITpro Admin
    Keymaster

    I forgot I changed that option setting a while back to allow any input and reflect that value on the System Info page. So it used to be accurate, but due to so many people complaining about not being able to change the PHP memory limit to whatever they wanted, I just changed how the option setting works.  If you want to see your true php memory limit go to the PHP Info View tab page > click the View PHPINFO button.

    I could explain all the technical stuff, but it would not really make any more sense.  So this is the simple answer:  Yes, you can manipulate the php memory value on a website, but it is really just cosmetic.  The server controls your php memory limit period.  Whatever value is set for memory_limit on your host server in the php.ini file and whatever php memory limit your host server actually allows is the true memory limit available to each website on that host server. So most hosts allocate 256M to each website. You can change the value in BPS Pro to 500M, but that is cosmetic only since the actual php memory limit is 256M.  You can actually reduce your php memory limit in a php.ini file or using ini_get and the server will decrease your allocated memory limit, but you can never exceed the maximum allowed/allocated php memory limit set by your host server.

    #42119
    Living Miracles
    Participant

    Ok, thanks for explaining that!

    I’d like to make sure I understand a nuance of how this works. Can you actually decrease your PHP Memory Limit below the threshold set by the server using the “Memory Limit” option in in BPS Pro > PHP Options > ini_set Options, or is this field entirely cosmetic and doesn’t have any real impact on the PHP Memory Limit no matter if the “Memory Limit” value in BPS Pro is above or below the limit set by the server?

    Also, it sounds like even though the WP_MAX_MEMORY_LIMIT WordPress constant is set to be identical to the “Memory Limit” value in BPS Pro, are you saying that because BPS Pro doesn’t use this WP constant, there must be some other code on our site that is causing the “Memory Limit” value in BPS Pro to be identical to this WordPress constant?

    Thanks for your help!

    #42120
    AITpro Admin
    Keymaster

    In theory if you use these WP Constants with these values then what you are directing your php server to do is to allocate 128M of memory to frontend scripts and 256M to backend scripts.

    define( 'WP_MEMORY_LIMIT', '128M' );
    define( 'WP_MAX_MEMORY_LIMIT', '256M');

    So yes if you tell your php server you only want to allocate/allow 64M to frontend and/or backend scripts then when a script tries to consume anything above 64M the script will terminate. So the answer to your question is that any setting above the actual php server memory available is cosmetic and anything below you actual php memory limit is functional.

    Yes, since BPS Pro does not use the WP_MAX_MEMORY_LIMIT then something else is defining the constant somewhere else. Could be a plugin or theme. Or the value is coming from your server’s php.ini file.

    #42122
    Living Miracles
    Participant

    Great, that answers my previous questions about the WordPress constants. Something about your most recent response makes me think I’m thinking about the variables mentioned in this thread all wrong though. In your original response to this thread, you define the two WordPress variables we’ve been discussing like so:

    WP_MEMORY_LIMIT is the amount of php memory that you want to allocate to WordPress.
    WP_MAX_MEMORY_LIMIT is the maximum amount of allowed php memory that WordPress is allowed to consume.

    Based on that, I’ve been sort of thinking of the WP_MEMORY_LIMIT as the actual memory available for WordPress to use. If something causes this memory to go above this WP_MEMORY_LIMIT value, the site crashes. The WP_MAX_MEMORY_LIMIT “shields” the site from this possibility by limiting the amount of memory WordPress is allowed to consume. So if WP_MAX_MEMORY_LIMIT is less than WP_MEMORY_LIMIT, any problem script is not allowed to finish instead and the website does not crash.

    However, in your most recent response, you seem to be saying that:

    WP_MEMORY_LIMIT is the actual amount of memory allocated to frontend scripts.

    WP_MAX_MEMORY_LIMIT is the actual amount of memory allocated to backend scripts.

    If this is the case, it seems like if either of these variables is exceeded, the site would crash and there would be no “cushion” to stop the script from running before the site goes down.

    So I’m wondering which of the two scenarios above that I have in my mind is closer to what is actually true about these variables and how they are used on the site. Or if it’s possible that both of these ways of looking at the variables are true somehow, though I’m not seeing how that could be in this moment.

    Thanks for clarifying!

    #42124
    AITpro Admin
    Keymaster

    I did some checking about this WP constant: WP_MAX_MEMORY_LIMIT after my first reply and found that the way WP designed it to work is on the frontend only as far as I can figure out.  PHP does not have a php.ini directive setting that is the same and only has memory_limit > https://www.php.net/manual/en/ini.core.php.  So WP must be using a condition like is not or is wpadmin.  ie  is_admin or ! is_admin > https://developer.wordpress.org/reference/functions/is_admin/. Personally I recommend that you do not fiddle around with WP_MAX_MEMORY_LIMIT at all and just let your PHP server handle things.

    #42129
    Living Miracles
    Participant

    Gotchya!

    So is your current understanding that the WP_MAX_MEMORY_LIMIT variable is designed to work for allocating memory to frontend scripts as you say in your last response (on September 19th), or is it that WP_MAX_MEMORY_LIMIT is a variable to allocate memory to backend scripts and WP_MEMORY_LIMIT is a variable to allocate memory to frontend scripts as you explained in your penultimate response (on September 18th)?

    #42130
    AITpro Admin
    Keymaster

    Well I’m not 100% sure to be honest.  I tried looking around and did not find anything official on the wordpress.org site.  So the information I found was from 3rd party sites and people were saying different things.  I think WP_MAX_MEMORY_LIMIT is supposed to be used for memory limit on the frontend of site, but I am not 100% sure of that.

    #42136
    Living Miracles
    Participant

    Ok, sounds good. Thanks for looking into this and answering our questions. We appreciate it!

    Best, Living Miracles

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.