diff --git a/src/HTML5.php b/src/HTML5.php
index 94faa1e0..c857145f 100644
--- a/src/HTML5.php
+++ b/src/HTML5.php
@@ -212,7 +212,10 @@ public function save($dom, $file, $options = array())
$trav = new Traverser($dom, $stream, $rules, $options);
$trav->walk();
-
+ /*
+ * release the traverser to avoid cyclic references and allow PHP to free memory without waiting for gc_collect_cycles
+ */
+ $rules->unsetTraverser();
if ($close) {
fclose($stream);
}
diff --git a/src/HTML5/Serializer/OutputRules.php b/src/HTML5/Serializer/OutputRules.php
index 1c4e8929..ec467f22 100644
--- a/src/HTML5/Serializer/OutputRules.php
+++ b/src/HTML5/Serializer/OutputRules.php
@@ -182,6 +182,13 @@ public function setTraverser(Traverser $traverser)
return $this;
}
+ public function unsetTraverser()
+ {
+ $this->traverser = null;
+
+ return $this;
+ }
+
public function document($dom)
{
$this->doctype();