Index: lib/MT/Builder.pm
===================================================================
--- lib/MT/Builder.pm	(revision 1295)
+++ lib/MT/Builder.pm	(working copy)
@@ -11,6 +11,8 @@
 
 use constant NODE => 'MT::Template::Node';
 
+use Scalar::Util qw( weaken );
+
 sub new { bless { }, $_[0] }
 
 sub compile {
@@ -207,8 +209,9 @@
                 $rec->[3] = '';
             }
         }
-        $rec->[5] = $opt->{parent} || $tmpl;
-        $rec->[6] = $tmpl;
+        # Avoids circular reference between NODE and TOKENS, MT::Template.
+        weaken($rec->[5] = $opt->{parent} || $tmpl);
+        weaken($rec->[6] = $tmpl);
         push @{ $state->{tokens} }, $rec;
         $pos = pos $text;
     }
@@ -254,8 +257,13 @@
 
 sub _text_block {
     my $text = substr ${ $_[0]->{text} }, $_[1], $_[2] - $_[1];
-    push @{ $_[0]->{tokens} }, bless [ 'TEXT', $text, undef, undef, undef, $_[0]->{tokens}, $_[0]->{tmpl} ], NODE
-        if (defined $text) && ($text ne '');
+    if ((defined $text) && ($text ne '')) {
+        my $rec = bless [ 'TEXT', $text, undef, undef, undef, $_[0]->{tokens}, $_[0]->{tmpl} ], NODE;
+        # Avoids circular reference between NODE and TOKENS, MT::Template.
+        weaken($rec->[5]);
+        weaken($rec->[6]);
+        push @{ $_[0]->{tokens} }, $rec;
+    }
 }
 
 sub syntree2str {
@@ -290,7 +298,8 @@
     } else {
         $cond = {};
     }
-    $ctx->stash('builder', $build);
+    # Avoids circular reference between MT::Template::Context and MT::Builder.
+    local($ctx->{__stash}{builder}) = $build;
     my $res = '';
     my $ph = $ctx->post_process_handler;
 
Index: lib/MT/Template.pm
===================================================================
--- lib/MT/Template.pm	(revision 1295)
+++ lib/MT/Template.pm	(working copy)
@@ -59,6 +59,7 @@
 use MT::Builder;
 use MT::Blog;
 use File::Spec;
+use Scalar::Util qw( weaken );
 
 sub class_label {
     MT->translate("Template");
@@ -141,7 +142,8 @@
     return $tmpl->{context} = shift if @_;
     require MT::Template::Context;
     my $ctx = $tmpl->{context} ||= MT::Template::Context->new;
-    $ctx->stash('template', $tmpl);
+    # Avoids circular reference between MT::Template and MT::Template::Context.
+    #$ctx->stash('template', $tmpl);
     return $ctx;
 }
 
@@ -598,13 +600,17 @@
 sub createElement {
     my $tmpl = shift;
     my ($tag, $attr) = @_;
-    return bless [ $tag, $attr, undef, undef, undef, undef, $tmpl ], NODE;
+    my $node = bless [ $tag, $attr, undef, undef, undef, undef, $tmpl ], NODE;
+    weaken($node->[6]);
+    $node;
 }
 
 sub createTextNode {
     my $tmpl = shift;
     my ($text) = @_;
-    return bless [ 'TEXT', $text, undef, undef, undef, undef, $tmpl ], NODE;
+    my $node = bless [ 'TEXT', $text, undef, undef, undef, undef, $tmpl ], NODE;
+    weaken($node->[6]);
+    $node;
 }
 
 sub insertAfter {
@@ -722,6 +728,7 @@
 package MT::Template::Node;
 
 use strict;
+use Scalar::Util qw( weaken );
 
 sub setAttribute {
     my $node = shift;
@@ -804,7 +811,7 @@
 
 sub parentNode {
     my $node = shift;
-    $node->[5] = shift if @_;
+    weaken($node->[5] = shift) if @_;
     $node->[5];
 }
 
