Recently, the project encountered a common problem: Memory leakage occurs when Ajax regularly acquires WebService data.
Baidu has failed to solve many solutions on the Internet. Some methods do have some memory recovery in a short period of time, but after more than a dozen hours of operation, the memory size of IE continues to grow to a large value.
Finally, google (which seems to have proved the sentence: technical post or google) found a solution for foreigners.
The Code is as follows:
1 // Test 1 2 var interval; 3 function fire () {4 interval = setinterval (getcount, 1000); 5} 6 7 function getcount () {8 var xhr = new XMLHttpRequest (); 9 10 xhr. open ("Post", "C. asmx/getcount ", true); 11 xhr. onreadystatechange = function (event) {12 if (xhr. readystate = 4 & xhr. status = 200) {13 document. getelementbyid ("tdmsg "). innerhtml = "Test 1:" + xhr. responsetext; 14 xhr. onreadystatechange = new function; 15 xhr = NULL; 16} 17}; 18 xhr. send (null); 19} 20 21 fire (); 22 23 // test 224 var interval2; 25 function fire2 () {26 interval2 = setinterval (getcount2, 200 ); 27} 28 29 function getcount2 () {30 var xhr = new XMLHttpRequest (); 31 32 xhr. open ("Post", "D. asmx/getcount ", true); 33 xhr. onreadystatechange = function (event) {34 if (xhr. readystate = 4 & xhr. status = 200) {35 document. getelementbyid ("testdiv "). innerhtml = "Test 2:" + xhr. responsetext; 36 xhr. onreadystatechange = new function; 37 xhr = NULL; 38} 39}; 40 xhr. send (null); 41} 42 43 fire2 (); 44 45 // test 346 var interval3; 47 function fire3 () {48 interval3 = setinterval (getcount3, 300 ); 49} 50 51 function getcount3 () {52 var xhr = new XMLHttpRequest (); 53 54 xhr. open ("Post", "e. asmx/getcount ", true); 55 xhr. onreadystatechange = function (event) {56 If (xhr. readystate = 4 & xhr. status = 200) {57 document. getelementbyid ("testdiv1 "). innerhtml = "Test 3:" + xhr. responsetext; 58 xhr. onreadystatechange = new function; 59 xhr = NULL; 60} 61}; 62 xhr. send (null); 63} 64 65 fire3 ();
A total of three WebServices are called, and simple data accumulation is returned.
Here we use three WebServices to test the stability of IE.
The WebService code is as follows:
1 public class C : System.Web.Services.WebService 2 { 3 4 private static long count = 0; 5 6 [WebMethod] 7 public string GetCount() 8 { 9 10 return (++count).ToString();11 }12 }
IE running effect:
Test 1 uses a one-second call interval to calculate the running time:
Test results:
Initial memory:
After running for 21617 seconds (about 360 minutes:
Memory:
6 hours of stress testing, IE memory increased by about 10 MB. Because the previous scheme basically increased memory by about 5 MB in an hour.