![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I'm aware of the circular reference memory leak problem with IE/closures. I'm not sure exactly how to resolve it in this situation. Also, Firefox appears to grow its memory size with the same code. So I'm wondering if I'm missing something? My test code is as follows: function myObj() { var req = new Object(); req.temp = 0; if (window.XMLHttpRequest) { req.xmlHttpRequest = new XMLHttpRequest(); } else if (window.ActiveXObject) { req.xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); } req.xmlHttpRequest.onreadystatechange = function() { if (req.readyState==4) { req.temp = req.xmlHttpRequest.responseText; } }; req.xmlHttpRequest.open("GET","/",true); req.xmlHttpRequest.send(null); return req; } // Create a whole bunch of these objects to check for memory leak for (var i=0; i<1000; i++) { var x = new myObj(); } What is the best way to avoid memory leaking in this example? -- Matt Kruse http://www.JavascriptToolbox.com |
|
function myObj() { var req = new Object(); |
|
req.xmlHttpRequest.onreadystatechange = function() { if (req.readyState==4) { req.temp = req.xmlHttpRequest.responseText; } }; |
#3
| |||
| |||
|
|
First, when you say: function myObj() { var req = new Object(); You are actually instantiating two objects for every call to new myObj. |
|
This is how I would I would rewrite this: |

|
Now a call to x = makeXMLRequest (NOT new makeXMLRequest) will return an instance of either XMLHttpRequest OR a new ActiveXObject of type 'Msxml2.XMLHTTP'. This should result in more efficient garbage collection as well. |
#4
| |||
| |||
|
|
if (req.readyState==4) { |

#5
| |||
| |||
|
|
I'm aware of the circular reference memory leak problem with IE/closures. I'm not sure exactly how to resolve it in this situation. Also, Firefox appears to grow its memory size with the same code. So I'm wondering if I'm missing something? snip function myObj() { var req = new Object(); req.temp = 0; if (window.XMLHttpRequest) { req.xmlHttpRequest = new XMLHttpRequest(); } else if (window.ActiveXObject) { req.xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); } req.xmlHttpRequest.onreadystatechange = function() { if (req.readyState==4) { req.temp = req.xmlHttpRequest.responseText; |
|
} }; req.xmlHttpRequest.open("GET","/",true); req.xmlHttpRequest.send(null); return req; } snip What is the best way to avoid memory leaking in this example? |
#6
| |||
| |||
|
|
- At this point will remove the curricular reference ... snip> ^^^^^^^^^^ |
#7
| |||
| |||
|
|
req.xmlHttpRequest.onreadystatechange = null; - At this point will remove the curricular reference and free the closure. |
#8
| |||
| |||
|
|
Random wrote: First, when you say: function myObj() { var req = new Object(); You are actually instantiating two objects for every call to new myObj. This is a simplified example of my real case, which needs to do this. But that's unrelated... |
|
This is how I would I would rewrite this: Well, that doesn't something entirely different. You can't really solve a problem by rewriting it and removing functionality, can you? ![]() |
|
Now a call to x = makeXMLRequest (NOT new makeXMLRequest) will return an instance of either XMLHttpRequest OR a new ActiveXObject of type 'Msxml2.XMLHTTP'. This should result in more efficient garbage collection as well. Well that's because you've removed the handling function completely! |
#9
| |||
| |||
|
|
Richard Cornford wrote: req.xmlHttpRequest.onreadystatechange = null; - At this point will remove the curricular reference and free the closure. That's what I thought too, but no luck. IE says "type mismatch". |
|
And in fact, I'm doing this just to be safe: delete req.xmlHttpRequest['onreadystatechange']; req.xmlHttpRequest = null; req = null; CollectGarbage(); snip |
#10
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |