#5285 closed bug (fixed)
IE7 & IE8 leak memory in jQuery.remove, probably caused by jQuery.data
Reported by: | EliAndrewC | Owned by: | |
---|---|---|---|
Priority: | major | Milestone: | 1.4 |
Component: | data | Version: | 1.3.2 |
Keywords: | memory-leak data event remove | Cc: | |
Blocked by: | Blocking: |
Description
Note: this problem was explored in depth on this Stack Overflow question and answer: http://stackoverflow.com/questions/1462649/jquery-memory-leak-with-dom-removal
I've attached a page which demonstrates a memory leak in remove. Interestingly, this memory leak is caused by the remove function's call to jQuery.event.remove which calls jQuery.data to see whether there are any events.
As documented in the Stack Overflow answer, this call to jQuery.data ends up creating an attribute to store the jQuery.cache id using the value of the expando variable as the attribute name. However, it does this even when we're only reading the data, so if we add a check for this
if (!id && data===undefined)
return undefined
then it makes the memory leak go away. I actually have no idea why this makes the memory leak go away, since the attribute that stores the jQuery.cache id is deleted by the jQuery.removeData function, which is called in remove.
So basically, I have no idea why this memory leak exists, but it definitely does in both IE8 and IE8 compatibility mode (which makes me assume that it also exists in IE7). This problem does not occur in Firefox.
Hopefully my attached proof-of-memory-leak plus the info I've provided will be more enlightening to people more familiar with jQuery's internals.
Attachments (1)
Change History (5)
Changed 14 years ago by
Attachment: | jquery_memleak.html added |
---|
comment:1 Changed 14 years ago by
Resolution: | → fixed |
---|---|
Status: | new → closed |
This appears to be fixed in SVN.
comment:2 Changed 12 years ago by
Hey, did you actually run the test case? If I change it to use 1.5 or 1.5.1 and run it in sieve (with IE 6), I still get the leak? I'm asking because I'm seeing some leaks in IE with Flot, and they mostly seem to come from exactly this use case, appending some stuff to a div and then deleting it afterwards.
One can download sieve here:
comment:3 Changed 12 years ago by
Hm, when I try it outside sieve, I don't seem to be getting the same result. Tried modifying the test case to increase the garbage produced and ran it directly in IE 6, 7 and 8 from the IE Collection. The memory usage is going up and down, but doesn't really seem to increase.
Sorry for the probably false alarm.
comment:4 Changed 12 years ago by
Hi guys, please see this issue again -- I have exactly the same problem with FireFox 5.0.1
I have a table with about 5 rows, each row is loaded by ajax like this: (each tr has custom attribute "job_id") $(document).ready(function() {
$(function() {
runJobRowUpdater();
});
}); function runJobRowUpdater() {
var jobRows = $("tr[job_id]"); for (var i = 0; i < jobRows.length; i++) {
var $row = $(jobRows[i]); var job_id = $row.attr("job_id") if (job_id) {
updateJobRow(job_id);
}
}
} function updateJobRow(job_id) {
var $row = $("tr[job_id='" + job_id + "']"); if (job_id) {
$row.load("/jobs/" + job_id + "/row", function() {
setTimeout("updateJobRow( " + job_id + " )", 5000);
});
}
}
So, each 5 seconds each row is loaded by ajax and sets timeout for next 5 seconds and so on. After half an hour FF reports something like 2GB(!!!!) memory usage
Standalone html file which demonstrates this memory leak. I refer to the googleapi jquery.min.js to avoid having to attach it, but I did test with the latest jQuery (1.3.2)