28

Here's a sample page with a couple datepickers. Here's the Drip result for that:

alt text http://www.picvault.info/images/537090308_omoya.png

This page leaks indefinitely in IE6sp1 when I click the Refresh button repeatedly (IE6sp3+, Opera 9, Chrome2, and FF3+ seem to be good). The memory goes up and never goes down until I actually close the browser completely.

I've also tried using the latest nightly of jquery (r6414) and the latest stable UI (1.7.2) but it didn't make any difference. I've tried various things with no success (CollectGarbage, AntiLeak, others).

I'm looking for a solution other than "use a different browser!!1" as I don't have any control over that. Any help will be greatly appreciated!

Update 1: I added that button event to a loop and this is what happens (the sudden drop off is when I terminate IE): alt text

Update 2: I filed a bug report (fingers crossed).

Update 3: This is also on the mailing list.

Update 4: This (as reported on the mailing list) doesn't work, and in fact makes things worse:

$(window).bind("unload", function() {
  $('.hasDatepicker').datepicker('destroy');
  $(window).unbind();
}); 

It's not enough to just call destroy. I'm still stranded with this one and getting very close to ripping jquery out of the project. I love it (I really do!) but if it's broken, I can't use it.

Update 5: Starting the bounty, another 550 points to one helpful individual!

Update 6: Some more testing has shown that this leak exists in IE6 and IE6sp1, but has been fixed in IE6sp2+. Now, about the answers I have so far...

So far all answers have been any one of these:

  1. Abandon IE6sp0/sp1 users or ignore them
  2. Debug jquery and fix the problem myself
  3. I can't repro the problem.

I know beggars can't be choosers, but those simply are not answers to my problem.

I cannot abandon my users. They make up 25% of the userbase. This is a custom app written for a customer, designed to work on IE6. It is not an option to abandon IE6sp0/sp1. It's not an option to tell my customers to just deal with it. It leaks so fast that after five minutes, some of the weaker machines are unusable.

Further, while I'd love to become a JS ninja so I can hunt down obscure memory leaks in jquery code (granted this is MS's fault, not jquery's), I don't see that happening either.

Finally, multiple people have reproduced the problem here and on the mailing list. If you can't repro it, you might have IE6SP2+, or you might not be refreshing enough.

Obviously this issue is very important to me (hence the 6 revisions, bounty, etc.) so I'm open to new ideas, but please keep in mind that none of those three suggestions will work for me.

Thanks to all for your consideration and insights. Please keep them coming!

Update 7: The bounty has ended and Keith's answer was auto-accepted by SO. I'm sorry that only half the points were awarded (since I didn't select the answer myself), but I'm still really stuck so I think half is fair.

I am hopeful that the jquery/jquery-ui team can fix this problem but I'm afraid that I'll have to write this off as "impossible (for now)" and stop using some or all of jquery. Thanks to everyone for your help and consideration. If someone comes along with a real solution to my problem, please post and I'll figure out some way to reward you.

9
  • 1
    Hey Mike, Bit upset that nobody from the jquery team has evenbothered to comment on the google group. I know they are around at the moment as they have commented on other (more trivial) matters. I spent some time looking how other frameworks destroy elements. I am starting to like dojo more and more!!
    – redsquare
    Jul 6, 2009 at 22:49
  • 1
    Quickly looking at this issue, it looks like a bug in IE6, not JQuery, which Microsoft fixed in later versions of IE. The JQuery guys are probably sick of dealing with issues caused by the red headed step child of web browsers, internet explorer. Just out of curiosity why are are you stuck on IE6? Jul 15, 2009 at 13:04
  • 1
    @nemo: Yes, it is definitely an IE6 bug--but since I can't get all my users to upgrade to IE6sp3+, jquery's gotta change. Many, many corporate users have no choice what IT pushes out and in my case, I have hundreds of old machines accessing the site with the old, unsupported version of IE. Jul 15, 2009 at 16:23
  • Is it really impossible to get your clients to upgrade to the latest IE6 version? I get that many apps written for IE6 don't run with IE7/8, but they should run on IE6SP2.
    – arturh
    Jul 17, 2009 at 14:48
  • It would be far less painful if I could add work-around IE's bug with some server-side code. If such a fix is impossible, I guess my only options are (1) avoid JS like that used by jquery/jqui or (2) upgrade hundreds of machines on 3 continents. Fun. Jul 17, 2009 at 17:04

9 Answers 9

22
+275

I hate to say this, your approach is correct and professional, but I'd be tempted to just leave it.

The consequences of not fixing this is that IE6 users will notice their machine getting slower and slower and ultimately either crashing completely or more likely crashing IE6.

So what?

Really - why is this your problem?

Yours definitely won't be the only site they visit with this leak, and they will see IE6 crash regularly regardless of what you do, because that's what it does.

It's unlikely that anyone still on IE6 could even point out your application as one that leaks.

Finally when IE6 does crash it reports IE6 as the culprit - you can legitimately point out that this is a bug in IE6 that Microsoft have fixed in a new release.

Your expensive time is better spent on improving the application for the users not trapped in legacy hell - your app should basically work for IE6 users, but this sort of issue can suck away all of your time and not fix their problem. IE6 is still going to be an unsupported, crash ridden, security hole of a browser.

I suspect the jQuery devs take a similar view to me. Also you have to do some really ugly stuff to get round this bug in IE6, including hacky DOM work that stops the leak but is actually much slower.


Update

Ok, this isn't an easy problem to fix - MS describe the IE6 bug (and provide advice on how to fix it) here: http://msdn.microsoft.com/en-us/library/bb250448(VS.85).aspx

Basically this isn't a problem with javascript or jQuery - the actual issue is with the IE6 DOM - when HTML elements are added to the page (by javascript, rather than being in the page when it loads) IE can't garbage collect them unless they are created in a very specific way.

This is back to front from how jQuery UI builds elements (see DOM insertion order bug in the link above) and so this isn't something that either the jQuery devs or you can easily fix.

So how do you fix the issue? Well, you can stick with the legacy pop-up calendar for IE6 or you can write your own one.

I would recommend the former, but if you really want to built the latter there are some basic rules to follow:

  1. Always add elements top-down - for instance if you want to built a table add the <table> element into the page's DOM, then add <tr> then <td> and so on. This is back to front as it's much quicker to build the entire table and then add it to the DOM - unfortunately that's where IE6 loses track of it.

  2. Only use CSS and HTML 3.2 attributes - sounds dumb, but IE6 creates extra objects to store the extra attributes (or 'expando' properties) and these also leak.

  3. Kinda related to (2), but as @gradbot mentions IE6 has problems garbage collecting javascript variables - if they reference a DOM element inside an event fired from that element you can get problems. This is also compounded by javascript references to DOM elements that have 'expando' properties.

If you have a look around online there may already be a drop-down DHTML calendar that sticks to these rules - it won't be as pretty, quick or configurable as the jQuery UI one, but I'm sure I've seen it done without leaking in IE6.

I think the best bet is to keep as much static as possible - for instance you could load the calendar grid (week numbers and day column headings) with the page and then dynamically load in the numbers (and nothing else). Create the day numbers as links, with javascript in the href - not best practice normally but far less likely to leak in IE6.

10
  • 1
    Incidentally the workaround involves building tables into the DOM in a completely different way - it would involve a complete re-write of the jQuery UI datepicker control and the new control would take a couple of seconds (of IE6 appearing to hang) to render.
    – Keith
    Jul 16, 2009 at 10:49
  • 1
    The specific issue that causes this leak is here: msdn.microsoft.com/en-us/library/bb250448(VS.85).aspx and the problem is the DOM insertion order leak
    – Keith
    Jul 16, 2009 at 11:02
  • 2
    @Keith: unfortunately, this is for a custom app written by my company. With 25% of my users running <IE6sp3, I do not have a choice as to what I support--trust me, I'd love to just force everyone to upgrade. I also understand that is sucks jquery has to work around IE's bug, but that's why I love jquery: it lets me stop worrying about browser problems and versions, etc. Or so I thought. If I can't fix the leak, I have to rip out jquery or at least whatever part of it leaks and do something else. Jul 16, 2009 at 11:30
  • My point is that you can just let the leak happen for those 25% users - their browser's gonna crash anyway. Your application will still basically work for them, and I'm not suggesting that you don't support them at all. You can't fix this leak with jQuery UI because of how the DatePicker builds the table that it displays. You could create your own one from scratch using specific IE6 only code, but that will still be slow for them. My own product's customer base is 20-25% IE6 and I think here I'd just drop the DatePicker for those users and ask them to type a date instead. Degrade gracefully.
    – Keith
    Jul 16, 2009 at 12:30
  • 1
    I think your best bet is to leave the IE6 users with the old calendar then. That way they don't lose anything while you still add value for the 75% of users who can take advantage of it. I find that while jQuery is mostly pretty good with IE anything in jQuery UI tends to just be too much for it. Even IE8 has appallingly poor performance when it comes to doing anything dynamic on the page. I find myself returning to the old design mantra of degrade gracefully regularly.
    – Keith
    Jul 16, 2009 at 20:25
4

It's obvious that the problems you've been describing stem from a flaw in IE6 that you can't subvert with a software fix (be it a jQuery update, a manual call to CollectGarbage, or some other JavaScript/DOM hack).

There are 3 options, in my mind, that would fix this problem.

  1. I would imagine that your customers/users are using IE6 SP0 because of some company standard or regulation, or even because some older web-app they still use doesn't support newer browsers. If it's not an option to upgrade to IE7 (or therefore IE8), you could get in contact with your customers' IT department and politely point out that updating IE6 with the latest service packs would not only fix a problem with an application that they are paying for, but also patch many security and performance flaws that undoubtedly exist in IE6 SP0. Admittedly, that might not be a comfortable situation, but it might solve the problems you are encountering, while still allowing them to work with a browser that require for whatever reason.

  2. If you can convince your customers' IT department that IE6 is antiquated, they may be willing to allow your users to upgrade to a newer browser. It's not a stretch to say that someone running an IT department would be more willing to force employees to upgrade a piece of software if they knew it was either a) riddled with flaws and security holes or b) approaching its end of support date (as IE6 SP0 is). IE6 SP0 on XP Pro SP2 is supported until July 13, 2010 - so it still has some time, but pointing that out, along with other flaws/limitations you could find might make them think seriously about upgrading sooner rather than later.

  3. If you can't convince anyone to upgrade their browsers either to IE6 SPX, or to IE7/8, then I don't know if you have a choice but to remove the offending control from your page, and pursue a different option until the user's browser permits it. There are assuredly many implementations of a date picker control available online which would suit your needs. It might not be as snazzy as the jQuery version, but you don't have many other options at this point.

I hope you find a solution!

2
  • I think 1 & 2 are reasonable arguments for making the switch, but unfortunately I doubt they'd work. Companies regularly have their HR, logistics, payroll, procurement, CRM, prospect management, intranet and everything else written specially by other companies. Most of those are stuck on IE6 too because big companies buy infrastructure from other big companies who have the same problem. IE6 hasn't been supported at all since February, on any release of any OS (though an OS that can run it still is in XP), and even that hasn't pushed it out as a requirement for corporate customers.
    – Keith
    Jul 17, 2009 at 17:09
  • I agree they probably wouldn't work, but it's still an option one should look into so you could say "I've pursued and exhausted all other options." I think that the software you mentioned would fall under the category of "some company standard/regulation or some older web-app that doesn't support newer browsers." As far as IE6 Support... According to Microsoft, there are still several scenarios where it's still supported: support.microsoft.com/gp/lifesupsps/#Internet_Explorer Jul 17, 2009 at 17:27
2

try deleting these objects after destroying the datepicker object:

$.datepicker = null;
$.fn.datepicker = null;
3
  • @Michael Haren: can you try adding this into your window.unload handler from update 4?
    – Stobor
    Jul 15, 2009 at 5:46
  • Sorry, it still leaks. It might be slightly slower, but it still leaks 500-1000kb per refresh. Thanks for the suggestion, though. Jul 15, 2009 at 12:33
  • This won't stop the leak because it is the HTML content dynamically added to the DOM that isn't being collected when the page refreshes.
    – Keith
    Jul 16, 2009 at 10:51
2

This problem is either in a IE6-only part of jQuery, or in a general part of jQuery that is lacking IE6 especific code (as noted in the comments). Either way, it's still a bug in jQuery that needs addressing. about:blank You'll either have to dig yourself into jQuery or file a bug ticket. If you manage to fix it, don't forget to attach a diff to the bugtracker, so the project gets a little bit better. ;)

If I get some spare time, I'll try to help you with this.

Edit

Ok, so the problem seems unsurmountable.

The leak you are facing is an IE 6 SP 0 only problem, a leak caused by IE's approach to DOM. Doesn't matter what JS framework you use, it refuses to work correctly.

So, your current options are:

  • Die trying to get your users to upgrade IE 6 to a newer version/Service Pack,
  • Die (as in leak) in IE (loosing customers) or
  • Die trying to work on IE.

But that doesn't necesarily means you can't work this out. What about just trying to side pass the wole thing?

Show every non IE 6 SP 0 user the jQuery datepicker, and only IE 6 SP 0 another more resilient (and probably basic) datepicker with IE's conditional comments. This way you can keep the eye candy/functionality in your software, and allow IE 6 users to have the same basic functionality.

It might not be such a clean option, but you'd still be able to use what you want, and IE6 will still be able to work without leaking.

The only problem will be that you'll have a bigger burden, by having to degug two distinct datepickers. But you'll have to debug IE 6 anyway so, it may be your best bet at the moment.

4
  • 1
    I'm not sure why we can assume the problem is with IE-specific code in jquery... This is a browser flaw that jquery will need to work around but currently doesn't. Jul 13, 2009 at 13:53
  • Of course it is a IE flaw, but that's the reason that one uses JQuery on the first place. I don't want to seem ungrateful but it's still a bug in JQuery. It may be an IE bug impossible to fix with javascript, but it's still a problem for JQuery and JQuery users. Personally, I'd love to see IE6 dye a painfull, slow, messy and bloody death. Jul 13, 2009 at 16:29
  • I think you misunderstood me. You said it's most likely a problem with jquery's IE-specific code. I'm saying that we have no reason to assume that. I bet instead that it's actually IE's crummy garbage collector choking on code that works fine with most other browsers Jul 14, 2009 at 0:06
  • 3
    to echo Michael, it's most likely not in any existing IE-specific code, but instead in the fact that there should_be some IE6-specific code which isn't currently there...
    – Stobor
    Jul 15, 2009 at 5:43
1

The problem with IE 6 is that it has two garbage collectors. One for JavaScript and one for the DOM. So for example if you attach a function to a DOM event and then delete the DOM element the function will still exist in memory.

Check out this slide show. It's a bit tongue in cheek but it's good information.

They fixed this issue in IE 7. I tried your page in IE8 in windows 7 and I'm not seeing a memory leak.

5
  • very interesting to know - is this across all versions of IE?
    – dplante
    Jul 3, 2009 at 4:38
  • I'm not sure let me dig a bit on google. I know it's the case with IE 6.
    – gradbot
    Jul 3, 2009 at 4:42
  • 1
    Looks like they fixed it in IE 7. I'll update my answer. quirksmode.org/blog/archives/2006/04/ie_7_and_javasc.html
    – gradbot
    Jul 3, 2009 at 4:43
  • 1
    I'm sorry gradbot, bit this doesn't really help me. Jul 15, 2009 at 2:07
  • I'll have to take a stab at it when I get some free time this weekend. I've been burning my free time with Project Euler problems as of late.
    – gradbot
    Jul 15, 2009 at 4:31
1

The problem here lies a bit deeper than 'just' jquery. Jquery along with many other browsers "leak" circular references between DOM objects and object listeners. Say you have an input field that has attached to it a listener, then you remove the element from the dom and do not have any reference to the listener in your code. Now any modern browser (>=ie7, ff, chrome, safari, opera) will live with that and garbage collect it, while IE6 will think that because there is a listener attached to a dom element it should not garbage collect the dom and the listener itself.

To get around that some folks use very complicated design patterns as highlighted for example in events code in Google Doctype. To fix the problem for IE6 you would really need to rewrite a portion of jquery to work around IE6 issue and/or switch to using a different library and/or not attach any event listeners in your application to DOM events.

0

Can you try this demo here. It uses the same method as dojo implements to remove elements from the dom. Some quick testing it seems to ease the leaks, not fully but much better.

UPDATE After spending a little time on this I am convinced it is nothing to do with the datepicker itself.

My tests show that by just reloading a dummy page every 1 second sees ie leaking memory. If you then include jquery on this page the leaks increase slightly (overhead of parsing the script) If you then add jquery-ui into the mix then again there is another slight increase in memory leakage.

To prove this if you avoid reloading the page and instead have a button that just adds an input, creates the datepicker on it then removes it, you see very little if any leaks.

1
  • 2
    Running my test page without binding the datepicker does not leak. Memory usage goes up a little but levels off. Once I add the .datepicker() line, it leaks like crazy. Jul 15, 2009 at 2:08
0

Take a look at this snippet that cleans up DOM nodes. You may find it useful. https://stackoverflow.com/a/9519996/139667

-1

The best debugger available for IE6 is Visual Studio. (Even the free edition will work.) As Janie mentions, if your problem is only happening on IE6 you'll want to debug on IE6, paying special attention to code that only runs there.

2
  • I'm not sure why we can assume the problem is with IE-specific code in jquery... This is a browser flaw that jquery will need to work around but currently doesn't. Jul 13, 2009 at 13:53
  • good point. That was what I'd intended; I guess it wasn't clear. Jul 13, 2009 at 15:59

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.