Wouldn't a lazy memory leak resolve?

Suppose the following code:

var deferred = $.Deferred();
deferred.done(function(){
    // do something with events, references to dom, etc...
});

deferred.fail(function(){
    // do something with events, references to dom, etc...
});

      

  • If I never call resolve()

    or fail()

    will it cause a memory leak since I am storing references in callbacks?

  • If I call one but not the other, will there be another garbage collection? So if I call fail()

    , will jquery get rid of done()

    ?

I might be thinking wrong, but would like some clarification. The closest I've found so far is Should I always call JQuery Deferred.resolve or Deferred.reject? ... But the use case is slightly different because in this example, the user never defined a callback for fail()

.

I also came across this, Is it possible to reverse jQuery's undo? however I am not trying to undo deferred

.

I realize this is not best practice, but for the purpose of the question, I am still wondering if it will cause memory problems.

Thank,

+3


source to share


1 answer


If I never call solution () or fail (), this will leak memory, since I am storing references in callbacks?

If the variable itself is deferred

no longer available to any of your codes (which also means there is no other promises depending on it) then it will be eligible for garbage collection even if it was never resolved or denied. If you want to know more about what "achievable" means, then you might be better off showing your actual code that bothers you.

If, on the other hand, you are still holding onto a variable deferred

somewhere, or else the promises that are still available depend on that promise (which means these promises have a reference to that), then it doesn't matter if deferred object is resolved or not, it will still be alive and cannot be collected garbage.

Promises are regular Javascript objects when it comes to garbage collection, so they follow all the same garbage collection rules as regular objects.

If I call one but not the other, will there be another garbage collection? So if I call fail () jquery gets rid of done ().

The code itself is not garbage collected. These are the contents of variables and objects that are garbage collected. So, in the code you are showing, this is the content of the variable deferred

(the promise object that the variable points to) that is the garbage collection object. And, as I said above, it doesn't matter if you made up your mind to reject it or not. The important thing is that the object is still available to your code or not. If any code can still reach the variable, deferred

or if some other code or promises has an available reference to that promise.




As an example, if I have top level code in my page:

<script>
var deferred = $.Deferred();
deferred.done(function(){
    // do something with events, references to dom, etc...
});
deferred.resolve();
</script>

      

The variable deferred

is still available and still alive, so the Deferred object it points to cannot be garbage collected.




Alternatively, if I have this:

<script>
$("#submit").click(function() {
    var p = $.get(myURL);
    p.done(function(data) {
        if (data) {
            $("#msg").html(data.msg);
        }
    });
});
</script>

      

Then, once the ajax call completes and the handler is called .done()

, then there is no more code that can reach that variable instance p

, since the ajax operation is done so that it is referencing the promise, and therefore it cannot initiate more reliable callbacks. And it went out of the scope of the click handler callback function and there are no real time event handlers that could be accessed to p

make it unavailable. At this point, he will be eligible for garbage collection.

+6


source







All Articles