3 namespace Drupal\Component\Utility;
6 * Provides PHP environment helper methods.
11 * Compares the memory required for an operation to the available memory.
13 * @param string $required
14 * The memory required for the operation, expressed as a number of bytes with
15 * optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8bytes,
17 * @param $memory_limit
18 * (optional) The memory limit for the operation, expressed as a number of
19 * bytes with optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G,
20 * 6GiB, 8bytes, 9mbytes). If no value is passed, the current PHP
21 * memory_limit will be used. Defaults to NULL.
24 * TRUE if there is sufficient memory to allow the operation, or FALSE
27 public static function checkMemoryLimit($required, $memory_limit = NULL) {
28 if (!isset($memory_limit)) {
29 $memory_limit = ini_get('memory_limit');
32 // There is sufficient memory if:
33 // - No memory limit is set.
34 // - The memory limit is set to unlimited (-1).
35 // - The memory limit is greater than or equal to the memory required for
37 return ((!$memory_limit) || ($memory_limit == -1) || (Bytes::toInt($memory_limit) >= Bytes::toInt($required)));