[wp-trac] [WordPress Trac] #56390: Updating WP_MEMORY_LIMIT
WordPress Trac
noreply at wordpress.org
Tue Aug 16 09:04:13 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 | Keywords:
Focuses: |
---------------------------+-----------------------------
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.
{{{WP_MEMORY_LIMIT}}} is the value for the WordPress Memory Limit, usually
referred to the frontend memory, and {{{WP_MAX_MEMORY_LIMIT}}} is the
value for the PHP Memory Limit, usually referred to the backend memory.
== History ==
Around September 2013, the {{{WP_MEMORY_LIMIT}}} value changed from 32 MB
to 40 MB (32+8). Some tests done by the Hosting Team suggest that the
memory used on WordPress is around 16 MB.
The PHP {{{memory_limit}}} sets the maximum amount of memory in bytes that
a script is allowed to allocate. This helps prevent poorly written scripts
for eating up all available memory on a server. Note that to have no
memory limit, set this directive to {{{-1}}}. Check the
[https://www.php.net/manual/en/ini.core.php#ini.memory-limit PHP page for
more information].
== Actual code ==
Checking the [https://github.com/WordPress/wordpress-
develop/blob/trunk/src/wp-includes/default-constants.php default-
constants.php], the code for {{{WP_MEMORY_LIMIT}}} is:
{{{
#!php
$current_limit = ini_get( 'memory_limit' );
$current_limit_int = wp_convert_hr_to_bytes( $current_limit );
// Define memory limits.
if ( ! defined( 'WP_MEMORY_LIMIT' ) ) {
if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {
define( 'WP_MEMORY_LIMIT', $current_limit );
} elseif ( is_multisite() ) {
define( 'WP_MEMORY_LIMIT', '64M' );
} else {
define( 'WP_MEMORY_LIMIT', '40M' );
}
}
// Set memory limits.
$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 );
}
}}}
''NOTE: this code is an extrapolation of some parts to understand the
values.''
For {{{WP_MAX_MEMORY_LIMIT}}}, is:
{{{
#!php
$current_limit = ini_get( 'memory_limit' );
$current_limit_int = wp_convert_hr_to_bytes( $current_limit );
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 );
} else {
define( 'WP_MAX_MEMORY_LIMIT', '256M' );
}
}
}}}
''NOTE: this code is an extrapolation of some parts to understand the
values.''
== PHP values ==
The first part gets the value from PHP, and if not exists, sets a default
value. Thereafter, if the value is incorrect (in bytes) sets the memory
limit from the constant.
This PHP value has evolved:
- PHP <5.1.0 (2005-11-24): valued at 8 MB (view
[https://web.archive.org/web/20051127013935/https://www.php.net/manual/en/ini.core.php
archive.org page])
- PHP =5.2.0: valued at 16 MB
- PHP >5.2.0 (2008-12-12): valued at 128 MB (view
[https://web.archive.org/web/20081217050654/https://www.php.net/manual/en/ini.core.php#ini
.memory-limit archive.org page])
== Some questions ==
**Why change the WP_MEMORY_LIMIT value?**
To level it to the PHP standard.
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, which is generally acceptable in new installations. At this
time, 20 latest versions of WordPress (since WordPress 4.1 / 2014-12-17)
can use PHP 5.6.40+ so it would meet the minimums set by PHP.
**Can, actually, the value be greater than the PHP value?**
Yes. That's why there is an intent to include something like:
{{{WP_MEMORY_LIMIT <= WP_MAX_MEMORY_LIMIT <= memory_limit}}}
== Premises ==
We should keep in mind some basic assumptions when incorporating PHP
memory limits based on what users can do.
The memory limit set by computer systems are set for a reason. And that
reason should be enforced; therefore, PHP's memory limit should not be
exceeded, and in case it needs to be exceeded, it should be changed by the
system administration.
Users can set the values they want from the wp-config.php configuration
file. Often, extremely high values are set to hide a memory consumption
problem due to bad programming. With a few visits it usually works, but it
is a short-term fix.
We must be realistic about the memory limits of the WordPress Core and the
normal use of a WordPress, whether it is a simple WordPress or a WordPress
Multisite. Most WordPress sites install plugins and themes that make
memory spikes higher.
PHP has its baseline memory limits that should serve as a reference for
its use and application.
== Proposal ==
Considering that since 2008, and PHP >5.2.0 the {{{memory_limit}}} value
is equal to 128 MB, should we consider an update of this value in the
WordPress base configuration, or at least an update of the values?
The proposal from the WordPress Hosting team is for {{{WP_MEMORY_LIMIT}}}:
- WordPress Single: {{{define('WP_MEMORY_LIMIT', '128M');}}}
- WordPress Multisite: {{{define('WP_MEMORY_LIMIT', '192M');}}}
Another patch should be:
{{{WP_MEMORY_LIMIT <= WP_MAX_MEMORY_LIMIT <= memory_limit}}}
Users can modify the {{{WP_MEMORY_LIMIT}}} and {{{WP_MAX_MEMORY_LIMIT}}}
at {{{wp-config.php}}} and should have some limitations in values, as far
as WordPress cannot overflow PHP.
== Getting the values ==
When doing some calls to PHP functions and values, got this:
- php.ini: {{{memory_limit}}} -> value: {{{256M}}}
- function: {{{memory_get_usage}}} -> value: {{{2097152}}}
- updating the {{{ini_set memory_limit}}} to {{{512M}}} -> value:
{{{memory_limit = 256M}}}
- function: {{{ini_get_all[memory_limit]}}}
{{{
Array (
[global_value] => 256M
[local_value] => 512M
[access] => 7
)
}}}
So, the real values are, always in the {{{ini_get_all[memory_limit]}}}.
After doing some tests, maybe hard-coding the values is a bad idea, but
having some "limits" is cool (like now) but reading the real values.
== New code ==
This is just a proposal of code (need revision and checking by the
WordPress Core Team).
{{{
#!php
$default_max_memory = 128 * MB_IN_BYTES; // this is a security limit.
Should be align with the default PHP memory_limit. Now (PHP 5.3+) is 128M
$memory_default = ini_get( 'memory_limit' );
$ini_get_all = ini_get_all();
// set by global ini
if( isset( $ini_get_all['memory_limit']['global_value'] ) ) {
$max_memory_default = $ini_get_all['memory_limit']['global_value'];
} elseif( $memory_default ) {
$max_memory_default = $memory_default;
} else {
$max_memory_default = $default_max_memory;
}
// set by site / virtualhost / pool ini
if( isset( $ini_get_all['memory_limit']['local_value'] ) ) {
$max_memory_real = $ini_get_all['memory_limit']['local_value'];
} else {
$max_memory_real = $max_memory_default;
}
unset( $memory_default, $ini_get_all );
// default memory in bytes
$max_memory_default_int = wp_convert_hr_to_bytes( $max_memory_default );
if( $max_memory_default_int <= 0 ) $max_memory_default_int =
$default_max_memory;
// site memory in bytes
$max_memory_real_int = wp_convert_hr_to_bytes( $max_memory_real );
if( $max_memory_real_int <= 0 ) $max_memory_real_int =
$default_max_memory;
if( $max_memory_real_int < $max_memory_default_int ) $max_memory_real_int
= $max_memory_default_int; // set the limit to the max memory set wherever
// user did not set the WP_MEMORY_LIMIT in wp-config.php
if ( ! defined( 'WP_MEMORY_LIMIT' ) ) {
define( 'WP_MEMORY_LIMIT', size_format( $max_memory_real_int ) );
// if the WP_MEMORY_LIMIT set by the user is greater than the real
available
} elseif( $max_memory_real_int < wp_convert_hr_to_bytes( WP_MEMORY_LIMIT )
) {
define( 'WP_MEMORY_LIMIT', size_format( $max_memory_real_int ) );
}
// user did not set the WP_MAX_MEMORY_LIMIT in wp-config.php
if ( ! defined( 'WP_MAX_MEMORY_LIMIT' ) ) {
define( 'WP_MAX_MEMORY_LIMIT', size_format( $max_memory_real_int ) );
// if the WP_MAX_MEMORY_LIMIT set by the user is greater than the real
available
} elseif( $max_memory_real_int < wp_convert_hr_to_bytes(
WP_MAX_MEMORY_LIMIT ) ) {
define( 'WP_MAX_MEMORY_LIMIT', size_format( $max_memory_real_int ) );
}
if ( wp_convert_hr_to_bytes( WP_MEMORY_LIMIT ) > wp_convert_hr_to_bytes(
WP_MAX_MEMORY_LIMIT ) ) {
WP_MEMORY_LIMIT = WP_MAX_MEMORY_LIMIT;
}
unset( $default_max_memory, $max_memory_default, $max_memory_real,
$max_memory_default_int, $max_memory_real_int );
}}}
Initial Props: @javiercasares, @crixu, @bernardzijlstra, @mikeschroder.
Original document from the Hosting team at
[https://docs.google.com/document/d/1CFMboqFnHMBifcuozqWKvUQUdxSCqCh-
YcCwMOMeAl0/]
--
Ticket URL: <https://core.trac.wordpress.org/ticket/56390>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list