简体   繁体   中英

What happens in JavaScript if I never call a callback?

Suppose I have the following code

function myFunction(param, callback) {
    ...
    if (err) {
        console.log("error");
        console.log(err);
    }
    else {
        callback(data);
    }
}

In the case of no error, the callback is called. In the case of an error, it is not. Suppose the calling function looks something like the following

myFunction(param, function(data) {
...
});

Are there memory leak issues or similar? Is there a better way to handle this scenario?

A JavaScript object will not be eligible for reclamation as long as it is strongly-reachable: that is, if it can be reached by traversing the object graph from a root object (which basically amounts to a global property or, possibly closed-over, variable). Any object that is no longer strongly reachable is no longer accessible via JavaScript and will be reclaimed by the GC (when the GC feels like it).

In this case the function-object (callback) passed to myFunction is only strongly-reachable for the duration of the function call when it is accessible via the callback parameter*. Because the function-object is not strongly-reachable after the function (eg it was not saved to a global property) then the function-object is eligible for reclamation - along with any function scopes that it referenced should they no longer be strongly-reachable - as soon as the function terminates.

Thus in this case , there is no "memory leak". However, imagine this case:

window.myCallbacks = []
function myFunction(param, callback) {
    ...
    window.myCallbacks.push(callback)  // hmm, maybe always strongly-reachable?
}

Happy coding.


Technically, a real smart JavaScript engine could determine that the object named by callback was no longer strongly-reachable (via callback ) in the "if" branch. I am not sure if any of the JS engines actually go this far, but the question becomes more interesting when talking about function scopes bound in closures (and if this keeps all the objects named by all the variables, even those not accessed later, strongly-reachable).

Adding to the above answer see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management . Where it says:

This algorithm reduces the definition of "an object is not needed anymore" to "an object is unreachable".

This algorithm assumes the knowledge of a set of objects called roots (In JavaScript, the root is the global object). Periodically, the garbage-collector will start from these roots, find all objects that are referenced from these roots, then all objects referenced from these, etc. Starting from the roots, the garbage collector will thus find all reachable objects and collect all non-reachable objects.

No need to worry about leaking memory with out calling a callback. You may want to have an error callback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM