function Environment::checkMemoryLimit

Same name in other branches
  1. 9 core/lib/Drupal/Component/Utility/Environment.php \Drupal\Component\Utility\Environment::checkMemoryLimit()
  2. 8.9.x core/lib/Drupal/Component/Utility/Environment.php \Drupal\Component\Utility\Environment::checkMemoryLimit()
  3. 10 core/lib/Drupal/Component/Utility/Environment.php \Drupal\Component\Utility\Environment::checkMemoryLimit()

Compares the memory required for an operation to the available memory.

Parameters

string $required: The memory required for the operation, expressed as a number of bytes with optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8bytes, 9mbytes).

$memory_limit: (optional) The memory limit for the operation, expressed as a number of bytes with optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8bytes, 9mbytes). If no value is passed, the current PHP memory_limit will be used. Defaults to NULL.

Return value

bool TRUE if there is sufficient memory to allow the operation, or FALSE otherwise.

3 calls to Environment::checkMemoryLimit()
EnvironmentTest::testCheckMemoryLimit in core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php
Tests \Drupal\Component\Utility\Environment::checkMemoryLimit().
LargeQueryTest::testMaxAllowedPacketQueryTruncating in core/modules/mysql/tests/src/Kernel/mysql/LargeQueryTest.php
Tests truncation of messages when max_allowed_packet exception occurs.
system_requirements in core/modules/system/system.install
Implements hook_requirements().

File

core/lib/Drupal/Component/Utility/Environment.php, line 27

Class

Environment
Provides PHP environment helper methods.

Namespace

Drupal\Component\Utility

Code

public static function checkMemoryLimit($required, $memory_limit = NULL) {
    if (!isset($memory_limit)) {
        $memory_limit = ini_get('memory_limit');
    }
    // There is sufficient memory if:
    // - No memory limit is set.
    // - The memory limit is set to unlimited (-1).
    // - The memory limit is greater than or equal to the memory required for
    //   the operation.
    return !$memory_limit || $memory_limit == -1 || Bytes::toNumber($memory_limit) >= Bytes::toNumber($required);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.