简体   繁体   中英

Javascript inner function memory leak?

I've been working with Javascript recently but I'm not actually very proficient with it. I'm trying to track down what looks to be a memory leak, since my Memory Usage seems to steadily increase. One idea I have for what might be causing this is the use of inner functions. I use inner functions when dealing with XMLHTTPRequests (and other related objects) and these requests get created every few seconds. I feel like perhaps I'm not closing it properly or something. So here's some code for one of the inner functions, if someone could tell me if this is the problem and how to fix it that would be great. (This is in the context of IE, I do not know and cannot find out if it happens in other browsers).

    me.request.open("GET", url, true);
    me.request.onreadystatechange = onReadyStateChange;
    me.request.send(); 

    function onReadyStateChange() {
        if (4 == me.request.readyState) {
            if (me.request.status == 200) {
                var results = me.request.responseText;
                var resultsString = me.request.responseText.toString();
                me.stringOperation(resultsString);
                me.request.abort();
                me.request = null;
            } else {
                me.request.abort();
                me.request = null;
            }
        }
    }

Add

function noop() {}

And then in onReadyStateChange , after me.request = null , me.request.onreadystatechange = noop .

This removes a cyclic reference between me , and onReadyStateChange that crosses the JS object / host object barrier.

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