diff --git a/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php b/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php
index 3f12d4eb22..edde7a7e1a 100644
--- a/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php
+++ b/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php
@@ -32,9 +32,9 @@ public function __sleep() {
             $service_id = 'service_container';
           }
           else {
-            $hash = spl_object_hash($value);
-            if (isset($mapping[$hash])) {
-              $service_id = $mapping[$hash];
+            $class = get_class($value);
+            if (isset($mapping[$class])) {
+              $service_id = $mapping[$class];
             }
           }
           if ($service_id) {
diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php
index 9ca7b28d2b..b2959aac3c 100644
--- a/core/lib/Drupal/Core/DrupalKernel.php
+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -241,7 +241,7 @@ class DrupalKernel implements DrupalKernelInterface, TerminableInterface {
   protected $root;
 
   /**
-   * A mapping from service hashes to service IDs.
+   * A mapping from service classes to service IDs.
    */
   protected $serviceIdMapping = [];
 
@@ -1597,13 +1597,12 @@ protected function addServiceFiles(array $service_yamls) {
    */
   protected function collectServiceIdMapping() {
     if (isset($this->container)) {
-      $this->serviceIdMapping = [];
       foreach ($this->container->getServiceIds() as $service_id) {
         if ($this->container->initialized($service_id)) {
           $service = $this->container->get($service_id);
           // @TODO remove after https://www.drupal.org/node/2536012
           if (is_object($service)) {
-            $this->serviceIdMapping[spl_object_hash($service)] = $service_id;
+            $this->serviceIdMapping[get_class($service)] = $service_id;
           }
         }
       }
