Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am i creating a memory leak?

I have a JavaScript closure which I keep recreating throughout the life time of my web application (single full-ajax page).

I would like to know if it's creating a memory leak.

Here is an example JSFIDDLE

The code in question:

function CreateLinks() {

    var ul = $("<ul></ul>").appendTo('div#links');

    for (var i in myLinks) {

        var li = $('<li>' + myLinks[i].name + '</li>').appendTo(ul);

        //closure starts here
        (function (value) {
            li.click(function (e) {
                $('div#info').append('<label>' + value + '</label><br />');
                RecreateLinks();
            });
        })(myLinks[i].value);

    }
}
like image 206
capdragon Avatar asked Oct 01 '12 18:10

capdragon


People also ask

How do I know if I have a memory leak?

The system can have a myriad of symptoms that point to a leak, though: decreased performance, a slowdown plus the inability to open additional programs, or it may freeze up completely.

What is considered a memory leak?

DEFINITION A memory leak is the gradual deterioration of system performance that occurs over time as the result of the fragmentation of a computer's RAM due to poorly designed or programmed applications that fail to free up memory segments when they are no longer needed.

How do I know if I have memory leaks Windows 10?

Check RAM With Windows' Memory Diagnostics Tool Press Windows key+R, enter "mdsched.exe," then select OK. Select Restart now and check for problems (recommended). The test will begin and may take several hours to complete. Follow any on-screen instructions once the test is complete.


2 Answers

You should be okay IF you make sure that you avoid binding multiple click handlers in the RecreateLinks() function; this can be done by explicitly unbinding existing ones, removing the DOM nodes or making sure that you won't be adding multiple click handlers.

Browsers are getting better at memory allocation strategies, but you shouldn't assume too much. If memory usage is a big concern, try to avoid creating too many closures of which you're not sure they will get garbage collected. One such approach is to use .data() to store your value object and then use a generic click handler instead of a closure.

Profiling JavaScript is not so simple; Chrome does have a Profile tool that can monitor CPU and data performance. This can give you a pretty good gauge on the expected memory consumption, but Chrome is not all browsers, keep that in mind.

like image 180
Ja͢ck Avatar answered Oct 05 '22 15:10

Ja͢ck


Depending on how smart the browser is, it may be better to have "myLinks[i].value" an attribute on your <li> rather than passed via closure. Certain dumb browsers have issues collecting garbage when an event handler references a variable from outside it's scope. JavaScript and DOM run two different GC and the JS one doesn't realize the DOM element/eventhandler is gone and that the variable is no longer in use. This issue may be cleared up by properly removing the event handler via javascript rather than just disposing of the element which it is attached to.

Something akin to:

li.attr('lvalue',myLinks[i].value);
...
var value = $(this).attr('lvalue');

This setup would also allow you to use

$('#links > ul > li').live('click',function(){...});

Which would remove the need for adding individual events each time.

like image 44
aepheus Avatar answered Oct 05 '22 15:10

aepheus