Reduce memory usage on ServiceContainer destruction
authorTim Starling <tstarling@wikimedia.org>
Fri, 31 Aug 2018 03:05:32 +0000 (13:05 +1000)
committerDaniel Kinzler <daniel.kinzler@wikimedia.de>
Mon, 3 Sep 2018 16:36:32 +0000 (16:36 +0000)
The closures in ServiceContainer::$serviceInstantiators are circular
references which prevent destruction of the object. So, delete these
when destroy() is called. Also delete the service instances for good
measure.

Change-Id: Ic8487cb533a09a8fcc69eba4f5d1bbb71558ae08

includes/services/ServiceContainer.php

index 30f8295..d934d27 100644 (file)
@@ -100,6 +100,12 @@ class ServiceContainer implements DestructibleService {
                        }
                }
 
+               // Break circular references due to the $this reference in closures, by
+               // erasing the instantiator array. This allows the ServiceContainer to
+               // be deleted when it goes out of scope.
+               $this->serviceInstantiators = [];
+               // Also remove the services themselves, to avoid confusion.
+               $this->services = [];
                $this->destroyed = true;
        }