Reputation: 2109
I am wondering if the following jquery code causes any memory leak:
$( function() {
var parent=$('table#mytable tbody').get(0);
$('tr:last', parent).click(function(){...});
});
For my understanding, $('tr:last', parent)
is the last row which is the DOM object, but in the anonymous function, the closure has this DOM object in scope, so there is a circular reference between DOM and js objects.
But if it really has leak, then I can see there are many such kind of code in the popular book "jQuery in Action" published by Manning. It is harmful "best practice" in jQuery coding?
But i don't know if my understanding is correct. I hope your comments and corrections. Thanks!
Upvotes: 4
Views: 2570
Reputation: 1397
The previously submitted answers (from 3 years ago, sigh) are wrong. While jQuery 1.0 - 1.2 would leak here, looking at the version of jQuery from when this question was asked http://download.oldapps.com/jquery/jquery-1.3.js the following line was included in the function which adds all event listeners:
// Nullify elem to prevent memory leaks in IE
elem = null;
Which is the exact leak prevention suggestion from the linked documentation.
Upvotes: 0
Reputation: 122926
IE is leak prone. You can test your code for leaks in IE with Microsofts javascript memory leak detector.
Firefox can leak too. This article explains why. There is also a leak detector plugin for Firefox (haven't tested it yet).
Upvotes: 1
Reputation: 2051
This code will cause memory leak in internet explorer 6 and internet explorer 7 not in other browser please refer these aritcles by Douglas Crockford and IBM Devloper Site
Upvotes: 2