[wp-trac] [WordPress Trac] #56390: Updating WP_MEMORY_LIMIT

WordPress Trac noreply at wordpress.org
Thu Aug 18 10:08:27 UTC 2022


#56390: Updating WP_MEMORY_LIMIT
---------------------------+------------------------------
 Reporter:  JavierCasares  |       Owner:  (none)
     Type:  enhancement    |      Status:  new
 Priority:  normal         |   Milestone:  Awaiting Review
Component:  General        |     Version:
 Severity:  normal         |  Resolution:
 Keywords:                 |     Focuses:  performance
---------------------------+------------------------------

Comment (by JanR):

 Replying to [comment:7 dd32]:
 > Replying to [ticket:56390 JavierCasares]:
 > > During the [https://europe.wordcamp.org/2022/contributor-day/
 Contributor Day at the WordCamp Europe 2022], the Hosting Team found that
 {{{WP_MEMORY_LIMIT}}} is set as 40 MB (single site) and 64 MB (multisite).
 Furthermore, the {{{WP_MAX_MEMORY_LIMIT}}} is set as 256 MB.
 > >

 I hope this is not new information for the Hosting Team, these memory
 limits are known, documented and in-place for years. However, this is not
 really always the case. Read on.

 > > Why change the WP_MEMORY_LIMIT value?
 > >

 [...]
 > > If the hoster has some kind of limitation, misconfiguration, an
 incorrect value or does not allow changing it, the value used is the
 lesser, of 40 MB, which usually produces memory errors, when it should use
 the PHP default value [...]
 >
 > If I'm understanding this correctly, the real concern here is not that
 the value of 40/64M is too low for WordPress, but rather, that under
 certain systems WordPress may accidentally lower the memory limit from
 something much higher to 40M, correct?
 >

 That is correct. Or at least, in some circumstances. I've seen a lot of
 plugins and themes [https://www.saotn.org/increase-wordpress-memory-limit-
 wp-config-php/ "doing it wrong"] in the past.

 But look at the code (bear with my, I hope I read it correctly) and
 imagine PHP's `memory_limit` is set to 134 MB.

 https://github.com/WordPress/WordPress/blob/master/wp-includes/default-
 constants.php#L46
 https://github.com/WordPress/WordPress/blob/master/wp-includes/default-
 constants.php#L68

 Here
 {{{
 ini_set( 'memory_limit', WP_MEMORY_LIMIT );
 }}}

 is only called if `$wp_limit_int > $current_limit_int` (I can't imagine
 situations when there is no current limit set in php.ini, other than some
 development environments perhaps). So nothing changes if the current PHP
 limit is higher than the new limit.

 However, remember that  PHP `memory_limit` can/may be changed anywhere, so
 `wp_is_ini_value_changeable( 'memory_limit' )` returns always true. This
 makes [https://github.com/WordPress/WordPress/blob/master/wp-includes
 /default-constants.php#L47 this check] obsolete in my opinion.

 For reference:
 {{{
 $ grep -i _limit wp-includes/default-constants.php
         $current_limit     = ini_get( 'memory_limit' );
         $current_limit_int = wp_convert_hr_to_bytes( $current_limit );
         if ( ! defined( 'WP_MEMORY_LIMIT' ) ) {
                 if ( false === wp_is_ini_value_changeable( 'memory_limit'
 ) ) {
                         define( 'WP_MEMORY_LIMIT', $current_limit );
                         define( 'WP_MEMORY_LIMIT', '64M' );
                         define( 'WP_MEMORY_LIMIT', '40M' );
         if ( ! defined( 'WP_MAX_MEMORY_LIMIT' ) ) {
                 if ( false === wp_is_ini_value_changeable( 'memory_limit'
 ) ) {
                         define( 'WP_MAX_MEMORY_LIMIT', $current_limit );
                 } elseif ( -1 === $current_limit_int || $current_limit_int
 > 268435456 /* = 256M */ ) {
                         define( 'WP_MAX_MEMORY_LIMIT', $current_limit );
                         define( 'WP_MAX_MEMORY_LIMIT', '256M' );
         $wp_limit_int = wp_convert_hr_to_bytes( WP_MEMORY_LIMIT );
         if ( -1 !== $current_limit_int && ( -1 === $wp_limit_int ||
 $wp_limit_int > $current_limit_int ) ) {
                 ini_set( 'memory_limit', WP_MEMORY_LIMIT );
 }}}

 One can argue hosting companies should (must) set reasonable limits, but
 don't forget they're set for a reason. In my opinion, PHP memory_limit
 should be at least > 128 MB, preferably 256 MB but plugins that require
 more must be killed :)

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/56390#comment:9>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list