javascript - Huge Memory Leaks with XMLHttp POST Request -


how can avoid xhr post memory leak? go through lots of web pages regarding xhr memory leak there no solutions. problem similar this blog explains problem no solutions.

my problem: have web app continuously send date server (2mb 80mb) , make 10 300 requests. post request. request no big problem this.

how can solve this? circular reference, scope , closer etc try no success. try use delete keyword readystate change, delete previous xhr object , try reuse xhr , xhr reference null , changing coding patters etc

this sample code. functionality need

 var base_string =  "abcdefghijklmnopqust01234567890!@#$%^&:abcdefghijklmnopqust01234567890!@#$%^&abcdefghijklmnopqust01234567890!@#$%^&";             base_string += base_string;               base_string += base_string;              base_string += base_string;              base_string += base_string;               base_string += base_string;               base_string += base_string;               base_string += base_string;               base_string += base_string;              base_string += base_string;               base_string += base_string;               base_string += base_string;              base_string += base_string;               base_string += base_string;               base_string += base_string;               base_string += base_string;               base_string += base_string;               base_string += base_string;               this.sampledata = base_string;             var datatosend = this.sampledata.substring( 0, 2000000 );               this.xhr = [];            this.xhr[0] = new xmlhttprequest();            function sendrequest (){                var self = this;                self.xhr[0].onload = function (test) {                    sendrequest ();                };                 self.xhr[0].open("post", "http://localhost/upload.php" + "?n=" +  math.random(), true);                self.xhr[0].send(datatosend);            }            sendrequest (); 

how can accomplish without memory leaks?

for every request send, add new onload handler.

self.xhr[0].onload = function (test) {     sendrequest (); }; 

the old handler not deleted @ point , stays in memory. garbage collector not able free memory.

in case ever need 1 eventlistener, recommend moving attachment of listener out of sendrequest function so, , memory leak should gone.

this.xhr = []; this.xhr[0] = new xmlhttprequest(); xhr[0].onload = function (test) {     sendrequest (); }; function sendrequest (){     xhr[0].open("post", "http://localhost/upload.php" + "?n=" +  math.random(), true);     xhr[0].send(datatosend); } sendrequest ();  

edit: version 2

i have tried version produces better results. memory never surpasses 2.6g on setup. derivation of jaromandas work. it's basicly version addition of removeeventhandler , delete

function sendrequest (){ function run(e){   xhr.upload.removeeventlistener('load',run)   sendrequest() } var xhr = new xmlhttprequest(); xhr.upload.addeventlistener('load', run); xhr.open("post", "http://localhost:2345/" + "?n=" +  math.random(), true); xhr.send(datatosend); delete xhr } sendrequest (); 

Comments