WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/08/2016 02:36:37 PM (3 years ago)
Author:
ocean90
Message:

Bootstrap: Enhance core's memory limit handling.

  • Don't lower memory limit if the current limit is greater than WP_MAX_MEMORY_LIMIT.
  • Set WP_MEMORY_LIMIT and WP_MAX_MEMORY_LIMIT to current limit if the memory_limit setting can't be changed at runtime.
  • Use wp_convert_hr_to_bytes() when parsing the value of the memory_limit setting because it can be a shorthand or an integer value.
  • Introduce wp_raise_memory_limit( $context ) to raise the PHP memory limit for memory intensive processes. This DRYs up some logic and includes the existing admin_memory_limit and image_memory_limit filters. The function can also be used for custom contexts, the {$context}_memory_limit filter allows to customize the limit.
  • Introduce wp_is_ini_value_changeable( $setting ) to determine whether a PHP ini value is changeable at runtime.
  • Remove a function_exists( 'memory_get_usage' ) check. Since PHP 5.2.1 support for memory limit is always enabled.

Related commits: [38011-38013]

Props jrf, A5hleyRich, swissspidy, ocean90.
Fixes #32075.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/default-constants.php

    r38011 r38015  
    2929    /**#@-*/
    3030
    31     // set memory limits
    32     if ( !defined('WP_MEMORY_LIMIT') ) {
    33         if ( is_multisite() ) {
    34             define('WP_MEMORY_LIMIT', '64M');
     31    $current_limit     = @ini_get( 'memory_limit' );
     32    $current_limit_int = wp_convert_hr_to_bytes( $current_limit );
     33
     34    // Define memory limits.
     35    if ( ! defined( 'WP_MEMORY_LIMIT' ) ) {
     36        if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {
     37            define( 'WP_MEMORY_LIMIT', $current_limit );
     38        } elseif ( is_multisite() ) {
     39            define( 'WP_MEMORY_LIMIT', '64M' );
    3540        } else {
    36             define('WP_MEMORY_LIMIT', '40M');
     41            define( 'WP_MEMORY_LIMIT', '40M' );
    3742        }
    3843    }
    3944
    4045    if ( ! defined( 'WP_MAX_MEMORY_LIMIT' ) ) {
    41         define( 'WP_MAX_MEMORY_LIMIT', '256M' );
     46        if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {
     47            define( 'WP_MAX_MEMORY_LIMIT', $current_limit );
     48        } elseif ( -1 === $current_limit_int || $current_limit_int > 268435456 /* = 256M */ ) {
     49            define( 'WP_MAX_MEMORY_LIMIT', $current_limit );
     50        } else {
     51            define( 'WP_MAX_MEMORY_LIMIT', '256M' );
     52        }
     53    }
     54
     55    // Set memory limits.
     56    $wp_limit_int = wp_convert_hr_to_bytes( WP_MEMORY_LIMIT );
     57    if ( -1 !== $current_limit_int && ( -1 === $wp_limit_int || $wp_limit_int > $current_limit_int ) ) {
     58        @ini_set( 'memory_limit', WP_MEMORY_LIMIT );
    4259    }
    4360
    4461    if ( ! isset($blog_id) )
    4562        $blog_id = 1;
    46 
    47     // set memory limits.
    48     if ( function_exists( 'memory_get_usage' ) ) {
    49         $current_limit = @ini_get( 'memory_limit' );
    50         $current_limit_int = intval( $current_limit );
    51         if ( false !== strpos( $current_limit, 'G' ) )
    52             $current_limit_int *= 1024;
    53         $wp_limit_int = intval( WP_MEMORY_LIMIT );
    54         if ( false !== strpos( WP_MEMORY_LIMIT, 'G' ) )
    55             $wp_limit_int *= 1024;
    56 
    57         if ( -1 != $current_limit && ( -1 == WP_MEMORY_LIMIT || $current_limit_int < $wp_limit_int ) )
    58             @ini_set( 'memory_limit', WP_MEMORY_LIMIT );
    59     }
    6063
    6164    if ( !defined('WP_CONTENT_DIR') )
Note: See TracChangeset for help on using the changeset viewer.