diff --git a/core/lib/Drupal/Component/Utility/Bytes.php b/core/lib/Drupal/Component/Utility/Bytes.php
index 0bcac31..4232635 100644
--- a/core/lib/Drupal/Component/Utility/Bytes.php
+++ b/core/lib/Drupal/Component/Utility/Bytes.php
@@ -44,4 +44,33 @@ public static function toInt($size) {
     }
   }
 
+  /**
+   * Returns a string representing a byte size.
+   *
+   * @param int $size
+   *   An integer representation of the size in bytes.
+   *
+   * @return string
+   *   A string size expressed as a number of bytes or multiples.
+   */
+  public static function toString($size) {
+    if ($size < Bytes::KILOBYTE) {
+      return $size . ' B';
+    }
+    else {
+      $size = $size / Bytes::KILOBYTE;
+      $units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
+      foreach ($units as $unit) {
+        if (round($size, 2) >= Bytes::KILOBYTE) {
+          $size = $size / Bytes::KILOBYTE;
+        }
+        else {
+          $size = round($size, 2);
+          break;
+        }
+      }
+      return $size . ' ' . $unit;
+    }
+  }
+
 }
diff --git a/core/lib/Drupal/Component/Utility/Environment.php b/core/lib/Drupal/Component/Utility/Environment.php
index 087bcad..a2183cc 100644
--- a/core/lib/Drupal/Component/Utility/Environment.php
+++ b/core/lib/Drupal/Component/Utility/Environment.php
@@ -13,7 +13,7 @@
 class Environment {
 
   /**
-   * Compares the memory required for an operation to the available memory.
+   * Compares the memory required for an operation to the total memory.
    *
    * @param string $required
    *   The memory required for the operation, expressed as a number of bytes with
@@ -42,4 +42,33 @@ public static function checkMemoryLimit($required, $memory_limit = NULL) {
     return ((!$memory_limit) || ($memory_limit == -1) || (Bytes::toInt($memory_limit) >= Bytes::toInt($required)));
   }
 
+  /**
+   * Compares the memory required for an operation to the remaining memory.
+   *
+   * @param 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).
+   * @param string $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 string $used_memory
+   *   (optional) The current memory usage, expressed as a number of bytes
+   *   with optional SI or IEC binary unit prefix. If no value is passed, the
+   *   current PHP memory usage as returned by memory_get_usage will be used.
+   *   Defaults to NULL.
+   *
+   * @return bool
+   *   TRUE if there is sufficient memory to allow the operation, or FALSE
+   *   otherwise.
+   */
+  public static function checkAvailableMemory($required, $memory_limit = NULL, $used_memory = NULL) {
+    if (!$used_memory) {
+      $used_memory = memory_get_usage(TRUE);
+    }
+    return static::checkMemoryLimit(Bytes::toInt($used_memory) + Bytes::toInt($required), $memory_limit);
+  }
+
 }
diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php
index eab55c1..0abd2d4 100644
--- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php
+++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php
@@ -137,6 +137,10 @@ public function apply($operation, array $arguments = array()) {
       $this->logger->warning($e->getMessage(), array());
       return FALSE;
     }
+    catch (\RuntimeException $e) {
+      $this->logger->warning($e->getMessage(), []);
+      return FALSE;
+    }
   }
 
 }
diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php
index 9e21c03..a67501b 100644
--- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php
+++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php
@@ -190,6 +190,9 @@ protected function validateArguments(array $arguments) {
    *
    * @return bool
    *   TRUE if the operation was performed successfully, FALSE otherwise.
+   *
+   * @throws \RuntimeException
+   *   If the operation can not be performed.
    */
   abstract protected function execute(array $arguments);
 
diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php
index 779deef..3ff986b 100644
--- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php
+++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php
@@ -34,6 +34,8 @@
    *
    * @throws \InvalidArgumentException
    *   If one or more of the arguments are not valid.
+   * @throws \RuntimeException
+   *   If the operation can not be performed.
    */
   public function apply(array $arguments);
 
diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
index f9cfb53..f8b7f81 100644
--- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
+++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
@@ -7,7 +7,9 @@
 
 namespace Drupal\system\Plugin\ImageToolkit;
 
+use Drupal\Component\Utility\Bytes;
 use Drupal\Component\Utility\Color;
+use Drupal\Component\Utility\Environment;
 use Drupal\Component\Utility\Unicode;
 use Drupal\Core\Config\ConfigFactoryInterface;
 use Drupal\Core\Form\FormStateInterface;
@@ -139,7 +141,9 @@ public function setResource($resource) {
    */
   public function getResource() {
     if (!is_resource($this->resource)) {
-      $this->load();
+      if (!$this->load()) {
+        return NULL;
+      };
     }
     return $this->resource;
   }
@@ -181,6 +185,15 @@ protected function load() {
       return FALSE;
     }
 
+    // Check if enough memory is available.
+    if (($required_memory = $this->checkAvailableMemory($this->getWidth(), $this->getHeight())) > 0) {
+      // If not, it is not possible to load the GD resource. Mark the image as
+      // invalid, to avoid useless attempts to manipulate it.
+      $this->preLoadInfo = NULL;
+      $this->logger->error("Not enough memory (required: %required_memory) to load image '%file'", ['%required_memory' => Bytes::toString($required_memory), '%file' => $this->getSource()]);
+      return FALSE;
+    }
+
     $function = 'imagecreatefrom' . image_type_to_extension($this->getType(), FALSE);
     if (function_exists($function) && $resource = $function($this->getSource())) {
       $this->setResource($resource);
@@ -436,4 +449,36 @@ public function extensionToImageType($extension) {
   protected static function supportedTypes() {
     return array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
   }
+
+  /**
+   * Ensures that there is enough memory available for a GD operation.
+   *
+   * GD functions that create new GD resources will fail fatally if there is
+   * not enough memory available to perform the operation. This method checks
+   * if there is enough memory available before calling the GD function. The
+   * algorithm implemented follows the one described in
+   * http://php.net/manual/en/function.imagecreatetruecolor.php#99623.
+   *
+   * @param int $width
+   *   The image width.
+   * @param int $height
+   *   The image height.
+   * @param int $bits_per_pixel
+   *   (optional) The bits per pixel of the image. Defaults to 32 (4 bytes).
+   * @param float $tweak_factor
+   *   (optional) A tweak factor as described in
+   *   http://php.net/manual/en/function.imagecreatetruecolor.php#99623.
+   *   Defaults to 1.7.
+   *
+   * @return int
+   *   -1 if there is sufficient memory to allow the operation. Otherwise, the
+   *   required memory, in bytes.
+   *
+   * @see http://php.net/manual/en/function.imagecreatetruecolor.php#99623
+   */
+  public function checkAvailableMemory($width, $height, $bits_per_pixel = 32, $tweak_factor = 1.7) {
+    $required = (int) ($width * $height * ($bits_per_pixel / 8) * $tweak_factor);
+    return Environment::checkAvailableMemory($required) ? -1 : $required;
+  }
+
 }
diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php
index ae72b82..3846aa8 100644
--- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php
+++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\system\Plugin\ImageToolkit\Operation\gd;
 
+use Drupal\Component\Utility\Bytes;
 use Drupal\Component\Utility\Color;
 
 /**
@@ -84,6 +85,11 @@ protected function validateArguments(array $arguments) {
    * {@inheritdoc}
    */
   protected function execute(array $arguments) {
+    // Check if enough memory is available.
+    if (($required_memory = $this->getToolkit()->checkAvailableMemory($arguments['width'], $arguments['height'])) > 0) {
+      throw new \RuntimeException("Not enough memory (required: " . Bytes::toString($required_memory) . ") to perform the image '{$this->getPluginId()}' operation");
+    }
+
     // Get the image type.
     $type = $this->getToolkit()->extensionToImageType($arguments['extension']);
 
diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php
index c77ff31..6712456 100644
--- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php
+++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\system\Plugin\ImageToolkit\Operation\gd;
 
+use Drupal\Component\Utility\Bytes;
 use Drupal\Component\Utility\Color;
 
 /**
@@ -98,6 +99,16 @@ protected function execute(array $arguments) {
       return FALSE;
     }
 
+    // Check that enough memory is available.
+    // @todo more accurate width and height of the image after rotation will be
+    // available after #1551686. In the meantime, the maximum width and height
+    // can never exceed the length of the diagonal of the rectangle represented
+    // by the image.
+    $diagonal = (int) sqrt(pow($this->getToolkit()->getWidth(), 2) + pow($this->getToolkit()->getHeight(), 2)) + 1;
+    if (($required_memory = $this->getToolkit()->checkAvailableMemory($diagonal, $diagonal)) > 0) {
+      throw new \RuntimeException("Not enough memory (required: " . Bytes::toString($required_memory) . ") to perform the image '{$this->getPluginId()}' operation");
+    }
+
     // Stores the original GD resource.
     $original_res = $this->getToolkit()->getResource();
 
diff --git a/core/modules/system/src/Tests/Image/ToolkitGdTest.php b/core/modules/system/src/Tests/Image/ToolkitGdTest.php
index 7304740..1cbec51 100644
--- a/core/modules/system/src/Tests/Image/ToolkitGdTest.php
+++ b/core/modules/system/src/Tests/Image/ToolkitGdTest.php
@@ -412,9 +412,9 @@ function testManipulations() {
   }
 
   /**
-   * Tests that GD resources are freed from memory.
+   * Tests that memory is allocated and released properly.
    */
-  public function testResourceDestruction() {
+  public function testMemoryAllocation() {
     // Test that an Image object going out of scope releases its GD resource.
     $image = $this->imageFactory->get(drupal_get_path('module', 'simpletest') . '/files/image-test.png');
     $res = $image->getToolkit()->getResource();
@@ -433,6 +433,15 @@ public function testResourceDestruction() {
     $this->assertFalse(is_resource($old_res));
     // Check if a new resource has been created successfully.
     $this->assertTrue(is_resource($new_res));
+
+    // Test creation of an image that will exceed the memory available.
+    $old_res = $image->getToolkit()->getResource();
+    $this->assertFalse($image->createNew(20000, 20000));
+    $new_res = $image->getToolkit()->getResource();
+    // Check that a new resource has not been created, and the old one is still
+    // valid.
+    $this->assertEqual($new_res, $old_res);
+    $this->assertTrue(is_resource($old_res));
   }
 
   /**
diff --git a/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php b/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php
new file mode 100644
index 0000000..207053e
--- /dev/null
+++ b/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php
@@ -0,0 +1,205 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Tests\system\Unit\Plugin\ImageToolkit\GdToolkitTest.
+ */
+
+namespace Drupal\Tests\system\Unit\Plugin\ImageToolkit;
+
+use Drupal\Core\Image\Image;
+use Drupal\Core\ImageToolkit\ImageToolkitInterface;
+use Drupal\Tests\UnitTestCase;
+
+/**
+ * Tests the GDToolkit class.
+ *
+ * @coversDefaultClass \Drupal\system\Plugin\ImageToolkit\GDToolkit
+ * @group Image
+ */
+class GdToolkitTest extends UnitTestCase {
+
+  /**
+   * Path of the image file used for testing.
+   *
+   * @var string
+   */
+  protected $source;
+
+  /**
+   * Image object under test.
+   *
+   * @var \Drupal\Core\Image\Image
+   */
+  protected $image;
+
+  /**
+   * The GD Image toolkit used for testing.
+   *
+   * @var \Drupal\Core\ImageToolkit\ImageToolkitInterface|\PHPUnit_Framework_MockObject_MockObject
+   */
+  protected $toolkit;
+
+  /**
+   * The GD Image toolkit operation used for testing.
+   *
+   * @var \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface
+   */
+  protected $toolkitOperation;
+
+  /**
+   * @inheritdoc
+   */
+  protected function setUp() {
+    // Use the Druplicon image.
+    $this->source = __DIR__ . '/../../../../../../simpletest/files/image-test.jpg';
+  }
+
+  /**
+   * Sets the GD toolkit mock.
+   *
+   * @param array $stubs
+   *   (optional) Array containing methods to be replaced with stubs.
+   */
+  protected function setToolkitMock(array $stubs = []) {
+    $logger = $this->getMock('Psr\Log\LoggerInterface');
+    $operation_manager = $this->getMock('Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface');
+    $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
+    $stream_wrapper_manager = $this->getMock('Drupal\Core\StreamWrapper\StreamWrapperManagerInterface');
+    $stubs = array_merge(['getPluginId'], $stubs);
+    $mock_builder = $this->getMockBuilder('Drupal\system\Plugin\ImageToolkit\GDToolkit');
+    $this->toolkit = $mock_builder
+      ->setConstructorArgs([[], '', [], $operation_manager, $logger, $config_factory, $stream_wrapper_manager])
+      ->setMethods($stubs)
+      ->getMock();
+    $this->toolkit->expects($this->any())
+      ->method('getPluginId')
+      ->will($this->returnValue('gd'));
+  }
+
+  /**
+   * Sets the GD toolkit operation mock.
+   *
+   * @param string $class_name
+   *   The name of the GD toolkit operation class to be mocked.
+   * @param ImageToolkitInterface $toolkit
+   *   The image toolkit object.
+   * @param array $stubs
+   *   (optional) Array containing methods to be replaced with stubs.
+   */
+  protected function setToolkitOperationMock($class_name, ImageToolkitInterface $toolkit, array $stubs = []) {
+    $logger = $this->getMock('Psr\Log\LoggerInterface');
+    $mock_builder = $this->getMockBuilder('Drupal\system\Plugin\ImageToolkit\Operation\gd\\' . $class_name);
+    $this->toolkitOperation = $mock_builder
+      ->setMethods($stubs)
+      ->setConstructorArgs([[], '', [], $toolkit, $logger])
+      ->getMock();
+  }
+
+  /**
+   * Mock function for GDToolkit::apply().
+   */
+  public function applyMockedOperation($operation, array $arguments = []) {
+    return $this->toolkitOperation->apply($arguments);
+  }
+
+  /**
+   * Test loading an image.
+   *
+   * @covers ::load
+   */
+  public function testLoad() {
+    $this->setToolkitMock(['checkAvailableMemory']);
+    $this->toolkit->expects($this->any())
+      ->method('checkAvailableMemory')
+      ->will($this->returnValue(-1));
+
+    $this->image = new Image($this->toolkit, $this->source);
+    $this->assertTrue(is_resource($this->image->getToolkit()->getResource()));
+    $this->assertTrue($this->image->isValid());
+    $this->assertEquals(40, $this->image->getWidth());
+    $this->assertEquals(20, $this->image->getHeight());
+  }
+
+  /**
+   * Simulates not enough memory to load an image.
+   *
+   * @covers ::load
+   */
+  public function testNotEnoughMemoryOnLoad() {
+    $this->setToolkitMock(['checkAvailableMemory']);
+    $this->toolkit->expects($this->any())
+      ->method('checkAvailableMemory')
+      ->will($this->returnValue(1024));
+
+    // Getting image from file validates the Image object, since GD toolkit is
+    // lazy loading the resource.
+    $this->image = new Image($this->toolkit, $this->source);
+    $this->assertTrue($this->image->isValid());
+    // Trying to get GD resource without enough memory yields to NULL resource
+    // and invalidates Image object.
+    $this->assertFalse(is_resource($this->image->getToolkit()->getResource()));
+    $this->assertFalse($this->image->isValid());
+    $this->assertEquals(NULL, $this->image->getWidth());
+    $this->assertEquals(NULL, $this->image->getHeight());
+  }
+
+  /**
+   * Simulates not enough memory to create a new image.
+   *
+   * @covers \Drupal\system\Plugin\ImageToolkit\Operation\gd\CreateNew::execute
+   *
+   * @expectedException \RuntimeException
+   */
+  public function testNotEnoughMemoryOnCreateNew() {
+    $this->setToolkitMock(['checkAvailableMemory', 'getToolkitOperation', 'apply']);
+    $this->setToolkitOperationMock('CreateNew', $this->toolkit);
+    $this->toolkit->expects($this->any())
+      ->method('checkAvailableMemory')
+      ->will($this->onConsecutiveCalls(-1, 1024));
+    $this->toolkit->expects($this->any())
+      ->method('getToolkitOperation')
+      ->will($this->returnValue($this->toolkitOperation));
+    $this->toolkit->expects($this->once())
+      ->method('apply')
+      ->will($this->returnCallback([$this, 'applyMockedOperation']));
+
+    $this->image = new Image($this->toolkit, $this->source);
+    // Get the GD resource to force GDToolkit::load, where the memory check is
+    // mocked to succeed.
+    $this->assertTrue(is_resource($this->image->getToolkit()->getResource()));
+    // Try the Image::createNew operation, where the memory check is mocked
+    // to fail. Expected a runtime exception.
+    $this->image->createNew(100, 200);
+  }
+
+  /**
+   * Simulates not enough memory to rotate an image.
+   *
+   * @covers \Drupal\system\Plugin\ImageToolkit\Operation\gd\Rotate::execute
+   *
+   * @expectedException \RuntimeException
+   */
+  public function testNotEnoughMemoryOnRotate() {
+    $this->setToolkitMock(['checkAvailableMemory', 'getToolkitOperation', 'apply']);
+    $this->setToolkitOperationMock('Rotate', $this->toolkit);
+    $this->toolkit->expects($this->any())
+      ->method('checkAvailableMemory')
+      ->will($this->onConsecutiveCalls(-1, 1024));
+    $this->toolkit->expects($this->any())
+      ->method('getToolkitOperation')
+      ->will($this->returnValue($this->toolkitOperation));
+    $this->toolkit->expects($this->once())
+      ->method('apply')
+      ->will($this->returnCallback([$this, 'applyMockedOperation']));
+
+    $this->image = new Image($this->toolkit, $this->source);
+    // Get the GD resource to force GDToolkit::load, where the memory check is
+    // mocked to succeed.
+    $this->assertTrue(is_resource($this->image->getToolkit()->getResource()));
+    // Try the Image::rotate operation, where the memory check is mocked
+    // to fail. Expected a runtime exception.
+    $this->image->rotate(120);
+  }
+
+}
diff --git a/core/tests/Drupal/Tests/Component/Utility/BytesTest.php b/core/tests/Drupal/Tests/Component/Utility/BytesTest.php
index 8e72b6c..ecda509 100644
--- a/core/tests/Drupal/Tests/Component/Utility/BytesTest.php
+++ b/core/tests/Drupal/Tests/Component/Utility/BytesTest.php
@@ -37,6 +37,23 @@ public function testToInt($size, $expected_int) {
   }
 
   /**
+   * Tests \Drupal\Component\Utility\Bytes::toString().
+   *
+   * @param int $size
+   *   The value for the size argument for
+   *   \Drupal\Component\Utility\Bytes::toString().
+   * @param string $expected_string
+   *   The expected return value from
+   *   \Drupal\Component\Utility\Bytes::toString().
+   *
+   * @dataProvider providerTestToString
+   * @covers ::toString
+   */
+  public function testToString($size, $expected_string) {
+    $this->assertEquals($expected_string, Bytes::toString($size));
+  }
+
+  /**
    * Provides data for testToInt.
    *
    * @return array
@@ -62,4 +79,29 @@ public function providerTestToInt() {
     );
   }
 
+  /**
+   * Provides data for testToString.
+   *
+   * @return array[]
+   *   An array of arrays, each containing the argument for
+   *   \Drupal\Component\Utility\Bytes::toString(): size, and the expected return
+   *   value.
+   */
+  public function providerTestToString() {
+    return [
+      [1, '1 B'],
+      [768, '768 B'],
+      [Bytes::KILOBYTE, '1 KB'],
+      [pow(Bytes::KILOBYTE, 2), '1 MB'],
+      [pow(Bytes::KILOBYTE, 3), '1 GB'],
+      [pow(Bytes::KILOBYTE, 4), '1 TB'],
+      [pow(Bytes::KILOBYTE, 5), '1 PB'],
+      [pow(Bytes::KILOBYTE, 6), '1 EB'],
+      [pow(Bytes::KILOBYTE, 7), '1 ZB'],
+      [pow(Bytes::KILOBYTE, 8), '1 YB'],
+      [23476892, '22.39 MB'],
+      [79691776, '76 MB'],
+      [81862076662, '76.24 GB'],
+    ];
+  }
 }
diff --git a/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php b/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php
index 72f04f7..16a5f87 100644
--- a/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php
+++ b/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\Tests\Component\Utility;
 
+use Drupal\Component\Utility\Bytes;
 use Drupal\Component\Utility\Environment;
 use Drupal\Tests\UnitTestCase;
 
@@ -64,4 +65,49 @@ public function providerTestCheckMemoryLimit() {
     );
   }
 
+  /**
+   * Tests \Drupal\Component\Utility\Environment::checkAvailableMemory().
+   *
+   * @dataProvider providerTestCheckAvailableMemory
+   * @covers ::checkAvailableMemory
+   *
+   * @param string $custom_memory_limit
+   *   A custom memory limit argument for
+   *   \Drupal\Component\Utility\Environment::checkAvailableMemory().
+   * @param string $used_memory
+   *   A mocked value for used memory.
+   * @param string $required
+   *   The required memory argument for
+   *   \Drupal\Component\Utility\Environment::checkAvailableMemory().
+   * @param bool $expected
+   *   The expected return value from
+   *   \Drupal\Component\Utility\Environment::checkAvailableMemory().
+   */
+  public function testCheckAvailableMemory($custom_memory_limit, $used_memory, $required, $expected) {
+    $actual = Environment::checkAvailableMemory($required, $custom_memory_limit, $used_memory);
+    $this->assertEquals($expected, $actual);
+  }
+
+  /**
+   * Provides data for testCheckAvailableMemory().
+   *
+   * @return array[]
+   *   An array of arrays, each containing the arguments for
+   *   \Drupal\Component\Utility\Environment::checkAvailableMemory():
+   *   memory_limit, used_memory, required memory and the expected
+   *   return value.
+   */
+  public function providerTestCheckAvailableMemory() {
+    return [
+      // 30MB should be available with limit of 60MB and usage of 10MB.
+      ['60MB', '10MB', '30MB', TRUE],
+      // Limit = required + used.
+      ['40MB', '30MB', '10MB', TRUE],
+      // Exceed memory limit.
+      ['20MB', '10MB', '30MB', FALSE],
+      // Exceed memory limit by 1 byte.
+      ['20MB', '20MB', '1', FALSE],
+    ];
+  }
+
 }
