jQuery-selectable is causing memory leaks. How can I prevent this? Is there something I am missing?
I have a complex web app that uses multiple instances of jquery UI-selectable. I have been using sIEve to track memory leaks and I notice the number of leaks is equal to the number of ui-selectable instances.
I wrote a simple test page that uses selectable as minimally as I could think of. In sIEve, it caused 1 memory leak (div#b).
Here is the HTML:
<!DOCTYPE html>
<html>
<head><title>test leaks with selectable</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery-ui.js"></script>
<script type="text/javascript" src="testLeaks.js"></script>
</head>
<body>
<div id="b"></div>
</body>
</html>
And here is testleaks.js:
function begin() {
$('#b').selectable();
}
function unloadCleanup() {
$('#b').selectable('destroy');
$('#b').remove();
}
$(window).unload(unloadCleanup);
$(document).ready(begin);
I only included unloadCleanup and the .unload handler after I already saw the leak. It makes no difference with or without it.
This seems to be evidence that simply activating jquery-selectable causes a memory leak.
Next I changed testleaks.html to include sub-divs:
<div id="b">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
In sIEve, I loaded the page and selected one of the sub-divs. The number of resulting leaks was 11 – one for the selectable div itself, and one for each selectee.
Selecting more than one sub-div did not increase the number of leaks. The number of leaks was 11 if any sub-divs were selected, and 1 if no sub-divs were selected.
Using jQuery 1.6.4, and jQuery-UI 1.8.16 with selectable, dialog, and datepicker, and the lightness theme.
Is there something I’m doing wrong?
The cause was not
selectable. It was apparentlydatepicker.Out of curiosity I updated to jQuery 1.7.1 and jQuery UI 1.8.18 with the same components (selectable, dialog, and datepicker). It still had the same issue with memory leaks.
I tried the same version without datepicker, using only selectable and dialog, and the memory leaks disappeared.