 core/includes/bootstrap.inc                        | 42 ++++++++++------------
 core/modules/color/color.module                    |  8 ++---
 core/modules/simpletest/simpletest.install         |  3 +-
 .../Drupal/system/Tests/Bootstrap/MiscUnitTest.php | 30 ++++++++--------
 core/modules/system/system.install                 |  2 +-
 5 files changed, 37 insertions(+), 48 deletions(-)

diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc
index b5c58e3..be7be60 100644
--- a/core/includes/bootstrap.inc
+++ b/core/includes/bootstrap.inc
@@ -18,6 +18,7 @@
 use Symfony\Component\DependencyInjection\Exception\RuntimeException as DependencyInjectionRuntimeException;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpKernel\DataCollector\MemoryDataCollector;
 use Drupal\Core\Language\Language;
 use Drupal\Core\Lock\DatabaseLockBackend;
 use Drupal\Core\Lock\LockBackendInterface;
@@ -3041,33 +3042,26 @@ function _drupal_shutdown_function() {
 }
 
 /**
- * Compares the memory required for an operation to the available memory.
+ * Compares a required PHP memory limit to the configured limit.
  *
- * @param $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).
- * @param $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.
+ * @param int $required
+ *   The required memory limit, expressed as a number of bytes with optional SI
+ *   or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8bytes, 9mbytes).
  *
- * @return
- *   TRUE if there is sufficient memory to allow the operation, or FALSE
- *   otherwise.
- */
-function drupal_check_memory_limit($required, $memory_limit = NULL) {
-  if (!isset($memory_limit)) {
-    $memory_limit = ini_get('memory_limit');
+ * @return bool
+ *   TRUE if required limit is less than or equal to the configured limit, or
+ *   FALSE otherwise.
+ */
+function drupal_check_memory_limit($required) {
+  // Get PHP's memory limit.
+  $data_collector = new MemoryDataCollector();
+  $memory_limit_bytes = $data_collector->getMemoryLimit();
+  // Is it unlimited?
+  if ($memory_limit_bytes == -1) {
+    return TRUE;
   }
-
-  // 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) || (parse_size($memory_limit) >= parse_size($required)));
+  // Is the required limit less than or equal to the configured limit?
+  return parse_size($required) <= $memory_limit_bytes;
 }
 
 /**
diff --git a/core/modules/color/color.module b/core/modules/color/color.module
index dc8be4a..9521187 100644
--- a/core/modules/color/color.module
+++ b/core/modules/color/color.module
@@ -307,11 +307,9 @@ function color_scheme_form_submit($form, &$form_state) {
     // available.  memory_get_usage(TRUE) returns a more accurate number than
     // memory_get_usage(), therefore we won't inadvertently reject a color
     // scheme change based on a faulty memory calculation.
-    $usage = memory_get_usage(TRUE);
-    $memory_limit = ini_get('memory_limit');
-    $size = parse_size($memory_limit);
-    if (!drupal_check_memory_limit($usage + $required, $memory_limit)) {
-      drupal_set_message(t('There is not enough memory available to PHP to change this theme\'s color scheme. You need at least %size more. Check the <a href="@url">PHP documentation</a> for more information.', array('%size' => format_size($usage + $required - $size), '@url' => 'http://www.php.net/manual/ini.core.php#ini.sect.resource-limits')), 'error');
+    $required += memory_get_usage(TRUE);
+    if (!drupal_check_memory_limit($required)) {
+      drupal_set_message(t('There is not enough memory available to PHP to change this theme\'s color scheme. You need at least %size. Check the <a href="@url">PHP documentation</a> for more information.', array('%size' => format_size($required), '@url' => 'http://www.php.net/manual/ini.core.php#ini.sect.resource-limits')), 'error');
       return;
     }
   }
diff --git a/core/modules/simpletest/simpletest.install b/core/modules/simpletest/simpletest.install
index 020b3cd..48d75b4 100644
--- a/core/modules/simpletest/simpletest.install
+++ b/core/modules/simpletest/simpletest.install
@@ -61,8 +61,7 @@ function simpletest_requirements($phase) {
 
   // Check the current memory limit. If it is set too low, SimpleTest will fail
   // to load all tests and throw a fatal error.
-  $memory_limit = ini_get('memory_limit');
-  if (!drupal_check_memory_limit(SIMPLETEST_MINIMUM_PHP_MEMORY_LIMIT, $memory_limit)) {
+  if (!drupal_check_memory_limit(SIMPLETEST_MINIMUM_PHP_MEMORY_LIMIT)) {
     $requirements['php_memory_limit']['severity'] = REQUIREMENT_ERROR;
     $requirements['php_memory_limit']['description'] = t('The testing framework requires the PHP memory limit to be at least %memory_minimum_limit. The current value is %memory_limit. <a href="@url">Follow these steps to continue</a>.', array('%memory_limit' => $memory_limit, '%memory_minimum_limit' => SIMPLETEST_MINIMUM_PHP_MEMORY_LIMIT, '@url' => 'http://drupal.org/node/207036'));
   }
diff --git a/core/modules/system/lib/Drupal/system/Tests/Bootstrap/MiscUnitTest.php b/core/modules/system/lib/Drupal/system/Tests/Bootstrap/MiscUnitTest.php
index f94c419..ef56385 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Bootstrap/MiscUnitTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Bootstrap/MiscUnitTest.php
@@ -8,6 +8,7 @@
 namespace Drupal\system\Tests\Bootstrap;
 
 use Drupal\simpletest\UnitTestBase;
+use Symfony\Component\HttpKernel\DataCollector\MemoryDataCollector;
 
 /**
  * Tests miscellaneous functions in bootstrap.inc.
@@ -26,21 +27,18 @@ public static function getInfo() {
    * Tests that the drupal_check_memory_limit() function works as expected.
    */
   function testCheckMemoryLimit() {
-    $memory_limit = ini_get('memory_limit');
-    // Test that a very reasonable amount of memory is available.
-    $this->assertTrue(drupal_check_memory_limit('30MB'), '30MB of memory tested available.');
-
-    // Get the available memory and multiply it by two to make it unreasonably
-    // high.
-    $twice_avail_memory = ($memory_limit * 2) . 'MB';
-    // The function should always return true if the memory limit is set to -1.
-    $this->assertTrue(drupal_check_memory_limit($twice_avail_memory, -1), 'drupal_check_memory_limit() returns TRUE when a limit of -1 (none) is supplied');
-
-    // Test that even though we have 30MB of memory available - the function
-    // returns FALSE when given an upper limit for how much memory can be used.
-    $this->assertFalse(drupal_check_memory_limit('30MB', '16MB'), 'drupal_check_memory_limit() returns FALSE with a 16MB upper limit on a 30MB requirement.');
-
-    // Test that an equal amount of memory to the amount requested returns TRUE.
-    $this->assertTrue(drupal_check_memory_limit('30MB', '30MB'), 'drupal_check_memory_limit() returns TRUE when requesting 30MB on a 30MB requirement.');
+    $data_collector = new MemoryDataCollector();
+    $memory_limit = $data_collector->getMemoryLimit();
+    // Is PHP's memory limit configured to be unlimited?
+    if ($memory_limit == -1) {
+      $this->assertTrue(drupal_check_memory_limit('1024M'), '1024 MiB of memory is available.');
+    }
+    else {
+      $this->assertTrue(drupal_check_memory_limit($memory_limit), 'Current memory limit is available.');
+      $this->assertTrue(drupal_check_memory_limit($memory_limit - 1), 'One byte less than current memory limit is available.');
+      $this->assertFalse(drupal_check_memory_limit($memory_limit + 1), 'One byte more than current memory limit is not available.');
+      $this->assertTrue(drupal_check_memory_limit((int) ($memory_limit / 2)), 'Half current memory limit is available.');
+      $this->assertFalse(drupal_check_memory_limit($memory_limit * 2), 'Twice current memory limit is not available.');
+    }
   }
 }
diff --git a/core/modules/system/system.install b/core/modules/system/system.install
index 6c170da..7a709f2 100644
--- a/core/modules/system/system.install
+++ b/core/modules/system/system.install
@@ -203,7 +203,7 @@ function system_requirements($phase) {
     'value' => $memory_limit == -1 ? t('-1 (Unlimited)') : $memory_limit,
   );
 
-  if (!drupal_check_memory_limit(DRUPAL_MINIMUM_PHP_MEMORY_LIMIT, $memory_limit)) {
+  if (!drupal_check_memory_limit(DRUPAL_MINIMUM_PHP_MEMORY_LIMIT)) {
     $description = '';
     if ($phase == 'install') {
       $description = t('Consider increasing your PHP memory limit to %memory_minimum_limit to help prevent errors in the installation process.', array('%memory_minimum_limit' => DRUPAL_MINIMUM_PHP_MEMORY_LIMIT));
