Click here to Skip to main content
15,172,576 members
Articles / Web Development / XHTML
Posted 20 Mar 2009


31 bookmarked

jQuery Memory Leak in UpdatePanel

Rate me:
Please Sign up or sign in to vote.
4.74/5 (24 votes)
20 Mar 2009CPOL4 min read
Addressing jQuery memory leak in UpdatePanel


This article discusses how to integrate jQuery library with Microsoft AJAX.NET, in particular with <asp:UpdatePanel/> and to avoid Internet Explorer memory leak while doing so.

jQuery becomes more and more popular. However Microsoft ASP.NET and AJAX.NET provide their own framework of communicating with the server. jQuery AJAX mechanism seems clumsy and un-natural in ASP.NET environment. It's much easier to use ASP.NET UpdatePanels for AJAX functionality and use jQuery for their selectors functionality - pretty much what I understood Microsoft is planning to do. However doing so is tricky and this article explains at least some of the shortfalls of doing so.

Iteration 1 - Sample Page

Let's imagine a sample calc - page that adds 2 numbers:

    <form id="form1" runat="server"> 
   	<asp:ScriptManager runat="server" ScriptMode=Debug> 
		<asp:ScriptReference Path="~/jquery-1.2.6.debug.js" /> 
	<asp:UpdatePanel ID='up' runat="server"><ContentTemplate> 
	    <asp:TextBox ID='i1' runat="server" CssClass='num' Width='50px'/>+ 
	    <asp:TextBox ID='i2' runat="server" CssClass='num' Width='50px'/>= 
	    <asp:TextBox ID='res' runat="server" Width='50px'/> 
	    <asp:Button ID='btn' runat="server" Text='...'/> 
	function add() { 
		$get('res').value = parseInt($get('i1').value) 
			+ parseInt($get('i2').value); 
	$(document).ready(function() { 

This page should not require much explanation if you have some familiarity with ASP.NET and jQuery:

  • We include ScriptManager and jquery script.
  • Standard form
  • We define 3 input controls, first two of which have class='num' - so we can easily find them with jQuery
  • Contents are placed in <asp:UpdatePanel> and button is provided to refresh the panel
  • Function is defined to get 2 numbers, add them up and store the result in the third input box
  • Standard jQuery mechanism is provided to attach change event so when one of the numbers gets changed, calculations are performed.

The resulting page looks like this:

Image 1

The stuff initially seems to work. You can change the numbers and see the result on the screen. However try to refresh the UpdatePanel - and suddenly calculations break.


The reason for failure after the UpdatePanel refresh is that the contents of the UpdatePanel go away along with all the events attached to them.
To solve the problem, we need to use AJAX.NET provided way of attaching events to the page (here I'm attaching only script portion):

function add() {
	$get('res').value = 
		parseInt($get('i1').value) + parseInt($get('i2').value);
Sys.Application.add_load(function() {

Iteration 2 - Memory Leak

The approach suggested in iteration 1 worked for me for a while. Except eventually, I started to notice that Internet Explorer slows down. A lot. And then looking at the process monitor, I noticed that Internet Explorer would leak memory on the moderate size form like a megabyte per UpdatePanel refresh. Looking at the sIEve tool (God bless the heart of the creators), I saw that the input elements are not been destroyed:

Image 2

You can see that every time I click refresh, it creates a new set of input elements without destroying the previous ones.


Googling it for a while I noticed that Internet Explorer memory leak is a sore subject for a lot of developers. Most of the complaints were identified as either pseudo leak (memory is actually released but you need a complete page refresh to see it) or as closures.

The closure is an interesting topic. It refers to the JavaScript event attached to the DOM element. Apparently Internet Explorer uses 2 garbage collectors - one for DOM and another for JavaScript. There are some semantics involved but the bottom line is that if you attached an event to the DOM object, then neither garbage collector would be able to get rid of the involved objects/DOM elements. I strongly encourage you to read more about closures on the WEB.

Well the jQuery is all about closures. They are nice enough to attach to window.unload and cleanup when the page gets unloaded, but they have no idea regarding UpdatePanel. Something needs to be done to cleanup jQuery when UpdatePanel is about to be refreshed.

Iteration 3 - jQuery Cleanup Plugin

UpdatePanel does provide cleanup hooks. When it's about to throw an element away, it checks to see if there is a element.dispose function. If such a function exists, then it will be executed.

So I ended up creating the following jQuery plugin to take care of the situation:

(function($) {
	$.fn.Disposable = function(cln) {
		return this.each(function() {
			var el = this;
			if (!el.dispose) {
				el.dispose = cleanup; // will be called by 
						    // Microsoft for cleanup
				$(window).bind("unload", cleanup);
			function cleanup() {
				if (!el)
				$(window).unbind("unload", cleanup);
				el.dispose = null;
				el = null;
function add() {
	$get('res').value = 
		parseInt($get('i1').value) + parseInt($get('i2').value);
Sys.Application.add_load(function() {

As you can see, this jQuery plugin attaches a cleanup() function to element.dispose. Whenever element.dispose is being called, all the jQuery events will be unbound from the element and allow garbage collector to reclaim all the objects.


The code I'm discussing is actually part of another library I posted a while ago at jQuery Based Ajax.Net library. You are welcome to download that project and browse the code at your leisure.


  • 20th March, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
Writing code since 1987 using whatever language/environment you can imagine. Recently got into the mobile games. Feel free to check them out at

Comments and Discussions

QuestionWhy to bind window_unload? Pin
PsyAfter3-Nov-14 4:11
MemberPsyAfter3-Nov-14 4:11 
AnswerRe: Why to bind window_unload? Pin
gstolarov3-Nov-14 4:56
Membergstolarov3-Nov-14 4:56 
GeneralRe: Why to bind window_unload? Pin
PsyAfter3-Nov-14 5:15
MemberPsyAfter3-Nov-14 5:15 
GeneralRe: Why to bind window_unload? Pin
gstolarov3-Nov-14 7:03
Membergstolarov3-Nov-14 7:03 
SuggestionAdded .remove() line to get it working Pin
DancnDude3-Mar-14 11:52
MemberDancnDude3-Mar-14 11:52 
GeneralRe: Added .remove() line to get it working Pin
gstolarov3-Mar-14 16:32
Membergstolarov3-Mar-14 16:32 
GeneralHi! Please help me.. thank you. Pin
YuTien26-Aug-09 0:20
MemberYuTien26-Aug-09 0:20 
GeneralRe: Hi! Please help me.. thank you. Pin
gstolarov26-Aug-09 4:53
Membergstolarov26-Aug-09 4:53 
GeneralMy vote of 1 Pin
keertiraj5559-Apr-09 4:35
Memberkeertiraj5559-Apr-09 4:35 
GeneralRe: My vote of 1 PinPopular
gstolarov9-Apr-09 16:35
Membergstolarov9-Apr-09 16:35 
GeneralRe: [SORRY] My vote of 1 Pin
keertiraj55511-Apr-09 1:14
Memberkeertiraj55511-Apr-09 1:14 
Generalthis breaks custom server control [modified] Pin
terrytsay20-Mar-09 21:59
Memberterrytsay20-Mar-09 21:59 
GeneralRe: this breaks custom server control Pin
gstolarov21-Mar-09 6:37
Membergstolarov21-Mar-09 6:37 
GeneralRe: this breaks custom server control Pin
terrytsay23-Mar-09 10:28
Memberterrytsay23-Mar-09 10:28 
GeneralAlternative to using plugin if using jQuery 1.3 or higher Pin
TheYo20-Mar-09 14:15
MemberTheYo20-Mar-09 14:15 
GeneralRe: Alternative to using plugin if using jQuery 1.3 or higher Pin
gstolarov20-Mar-09 14:51
Membergstolarov20-Mar-09 14:51 
GeneralRe: Alternative to using plugin if using jQuery 1.3 or higher Pin
gstolarov23-Mar-09 5:31
Membergstolarov23-Mar-09 5:31 
GeneralRe: Alternative to using plugin if using jQuery 1.3 or higher Pin
TheYo23-Mar-09 9:00
MemberTheYo23-Mar-09 9:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.