Index: /CKEditor/trunk/CHANGES.html
===================================================================
--- /CKEditor/trunk/CHANGES.html	(revision 5480)
+++ /CKEditor/trunk/CHANGES.html	(revision 5481)
@@ -84,4 +84,5 @@
 		<li><a href="http://dev.fckeditor.net/ticket/5671">#5671</a> : SCAYT bookstrap script could be added multiple times unnecessarily.</li>
 		<li><a href="http://dev.fckeditor.net/ticket/5617">#5617</a> : Html filter system does not allow two 'text' filter rules.</li>
+		<li><a href="http://dev.fckeditor.net/ticket/5663">#5663</a> : General memory clean up after destroying last instance.</li>
 		<li>Updated the following language files:<ul>
 			<li><a href="http://dev.fckeditor.net/ticket/5432">#5432</a> : Dutch;</li>
Index: /CKEditor/trunk/_source/core/ckeditor.js
===================================================================
--- /CKEditor/trunk/_source/core/ckeditor.js	(revision 5480)
+++ /CKEditor/trunk/_source/core/ckeditor.js	(revision 5481)
@@ -70,4 +70,14 @@
 };
 
+/**
+ * Perform global clean up to free as much memory as possible
+ * when there are no instances left
+ */
+CKEDITOR.on( 'instanceDestroyed', function ()
+	{
+		if ( CKEDITOR.tools.isEmpty( this.instances ) )
+			CKEDITOR.fire( 'reset' );
+	});
+
 // Load the bootstrap script.
 CKEDITOR.loader.load( 'core/_bootstrap' );		// @Packager.RemoveLine
Index: /CKEditor/trunk/_source/core/dom/domobject.js
===================================================================
--- /CKEditor/trunk/_source/core/dom/domobject.js	(revision 5480)
+++ /CKEditor/trunk/_source/core/dom/domobject.js	(revision 5481)
@@ -144,4 +144,9 @@
 	var customData = {};
 
+	CKEDITOR.on( 'reset', function()
+		{
+			customData = {};
+		});
+
 	/**
 	 * Determines whether the specified object is equal to the current object.
Index: /CKEditor/trunk/_source/core/tools.js
===================================================================
--- /CKEditor/trunk/_source/core/tools.js	(revision 5480)
+++ /CKEditor/trunk/_source/core/tools.js	(revision 5481)
@@ -1,3 +1,3 @@
-﻿/*
+/*
 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
 For licensing, see LICENSE.html or http://ckeditor.com/license
@@ -12,4 +12,9 @@
 {
 	var functions = [];
+
+	CKEDITOR.on( 'reset', function()
+		{
+			functions = [];
+		});
 
 	/**
Index: /CKEditor/trunk/_source/plugins/button/plugin.js
===================================================================
--- /CKEditor/trunk/_source/plugins/button/plugin.js	(revision 5480)
+++ /CKEditor/trunk/_source/plugins/button/plugin.js	(revision 5481)
@@ -271,2 +271,7 @@
 	this.add( name, CKEDITOR.UI_BUTTON, definition );
 };
+
+CKEDITOR.on( 'reset', function()
+	{
+		CKEDITOR.ui.button._.instances = [];
+	});
Index: /CKEditor/trunk/_source/plugins/wysiwygarea/plugin.js
===================================================================
--- /CKEditor/trunk/_source/plugins/wysiwygarea/plugin.js	(revision 5480)
+++ /CKEditor/trunk/_source/plugins/wysiwygarea/plugin.js	(revision 5481)
@@ -811,22 +811,19 @@
 			else
 			{
-				body.setAttribute( 'onpageshow', body.getAttribute( 'onpageshow' )
-						+ ';event.persisted && CKEDITOR.tools.callFunction(' +
-						CKEDITOR.tools.addFunction( function()
-						{
-							var allInstances = CKEDITOR.instances,
-								editor,
-								doc;
-							for ( var i in allInstances )
-							{
-								editor = allInstances[ i ];
-								doc = editor.document;
-								if ( doc )
-								{
-									doc.$.designMode = 'off';
-									doc.$.designMode = 'on';
-								}
-							}
-						} ) + ')' );
+				var currentHandler = body.getAttribute( 'onpageshow' );
+				body.setAttribute( 'onpageshow', ( currentHandler ? currentHandler + ';' : '') +
+							'event.persisted && (function(){' +
+								'var allInstances = CKEDITOR.instances, editor, doc;' +
+								'for ( var i in allInstances )' +
+								'{' +
+								'	editor = allInstances[ i ];' +
+								'	doc = editor.document;' +
+								'	if ( doc )' +
+								'	{' +
+								'		doc.$.designMode = "off";' +
+								'		doc.$.designMode = "on";' +
+								'	}' +
+								'}' +
+						'})();' );
 			}
 		} )();
