MDL-24962 backup - circular refs. cleaner in backup_controller
authorEloy Lafuente <stronk7@moodle.org>
Mon, 15 Nov 2010 07:16:51 +0000 (07:16 +0000)
committerEloy Lafuente <stronk7@moodle.org>
Mon, 15 Nov 2010 07:16:51 +0000 (07:16 +0000)
backup/controller/backup_controller.class.php

index 6cadb7d..9db2e3d 100644 (file)
@@ -139,6 +139,23 @@ class backup_controller extends backup implements loggable {
         }
     }
 
+    /**
+     * Clean structures used by the backup_controller
+     *
+     * This method clean various structures used by the backup_controller,
+     * destroying them in an ordered way, so their memory will be gc properly
+     * by PHP (mainly circular references).
+     *
+     * Note that, while it's not mandatory to execute this method, it's highly
+     * recommended to do so, specially in scripts performing multiple operations
+     * (like the automated backups) or the system will run out of memory after
+     * a few dozens of backups)
+     */
+    public function destroy() {
+        // Only need to destroy circulars under the plan. Delegate to it.
+        $this->plan->destroy();
+    }
+
     public function finish_ui() {
         if ($this->status != backup::STATUS_SETTING_UI) {
             throw new backup_controller_exception('cannot_finish_ui_if_not_setting_ui');
@@ -161,7 +178,9 @@ class backup_controller extends backup implements loggable {
         // containing all the steps will be sent to DB. 100% (monster) useless.
         if ($status == backup::STATUS_AWAITING) {
             $this->save_controller();
-            $this->logger = self::load_controller($this->backupid)->logger; // wakeup loggers
+            $tbc = self::load_controller($this->backupid);
+            $this->logger = $tbc->logger; // wakeup loggers
+            $tbc->destroy(); // Clean temp controller structures
         }
     }