473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Closures + XMLHttpRequest + Memory Leak

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
Jul 23 '05 #1
10 14043
Matt Kruse wrote:
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

Not sure if this will solve your problem, but I noticed a few wasted
resources and unclear code.

First, when you say: function myObj() {
var req = new Object();
You are actually instantiating two objects for every call to new myObj.

Second: req.xmlHttpRequest.onreadystatechange =
function() {
if (req.readyState==4) {
req.temp = req.xmlHttpRequest.responseText;
}
};


For everything within the anonymous function, req should be 'this'.

This is how I would I would rewrite this:
function makeXMLRequest( URI ) {
var x = (
window.XMLHttpRequest ?
( new XMLHttpRequest() )
: ( new ActiveXObject( 'Msxml2.XMLHTTP' ) )
);

x.open( 'GET', encodeURI( URI || 'about:blank' ), true );
x.send( null );

return x;
}
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.

The real problem (I think) is that you are creating all of these
objects and not necessarily giving them time to complete the HTTP
request. This is probably preventing the necessary garbage collection
and creating 1000 simultaneous HTTP requests. Ouch.

Hope that helps.

Jul 23 '05 #2
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!

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #3
Matt Kruse wrote:
if (req.readyState==4) {


Oops, this is actually:

if (req.xmlHttpRequest.readyState==4) {

of course :)

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #4
Matt Kruse wrote:
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.onreadystatechange = null;

- At this point will remove the curricular reference and free the
closure.
}
};
req.xmlHttpRequest.open("GET","/",true);
req.xmlHttpRequest.send(null);
return req;
} <snip> What is the best way to avoid memory leaking in this example?


Belt and braces would have you nulling the reference to the
XMLHttpRequest object once it is finished with.

Richard.
Jul 23 '05 #5
Richard Cornford wrote:
<snip>
- At this point will remove the curricular reference ...

<snip> ^^^^^^^^^^

That should have been 'circular'.

Richard.
Jul 23 '05 #6
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".

Instead, I found that this appears to work:

delete req.xmlHttpRequest['onreadystatechange'];

And in fact, I'm doing this just to be safe:

delete req.xmlHttpRequest['onreadystatechange'];
req.xmlHttpRequest = null;
req = null;
CollectGarbage();

That *appears* to work in IE. I can see the memory usage grow to over 150MB,
then drop back to 30MB, so I assume the leak is gone. However, I don't know
of any way to test for sure. Do you?

--
Matt Kruse
http://www.JavascriptToolbox.com
Jul 23 '05 #7
Matt Kruse wrote:
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...


Clearly I misunderstood. Sorry I didn't catch that based on your
original post.
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? :)


See above.
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!


See above.

Jul 23 '05 #8
Matt Kruse wrote:
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".


Yes, you are right. It looks like I went for assigning a reference to a
small harmless (and not 'inner') function to clear the closure in my
version.

<snip> And in fact, I'm doing this just to be safe:

delete req.xmlHttpRequest['onreadystatechange'];
req.xmlHttpRequest = null;
req = null;
CollectGarbage();

<snip>

Calling CollectGarbage without verifying its existence will result in
errors on browsers that do not support it.

Richard.
Jul 23 '05 #9
i ussing xmlhttprequest for few years. Typicaly i use sinchronus data
trander

example of my uses

senddata(method,data,debug,url)
debug = shows respondense content

myResult = sendData('update',{id:1,name:34});

after this call i get myResult javascript array (or string)

for asyncronus can be added callback function

Jul 23 '05 #10
zi******@gmail.com wrote:
i ussing xmlhttprequest for few years. Typicaly i use sinchronus data
trander

example of my uses

senddata(method,data,debug,url)
debug = shows respondense content

myResult = sendData('update',{id:1,name:34});

after this call i get myResult javascript array (or string)

for asyncronus can be added callback function


Jul 23 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mark D. Anderson | last post by:
About a month ago Richard Cornford did an interesting analysis of a memory leak in jscript (internet explorer) when there are "circular" references between DOM objects and (real) jscript objects:...
2
by: Robert | last post by:
Hi, Do I understand correctly from http://jibbering.com/faq/faq_notes/closures.html that the following will create a memory leak in IE? function createMemoryLeak(id) { var el =...
1
by: Cylix | last post by:
I have read an article about the memory leak issue on nowadays website, "Circular References-When mutual references are counted between Internet Explorer's COM infrastructure and any scripting...
2
by: Robert | last post by:
Hello javascript group readers, I have a question regarding how to prevent memory leaks in Internet Explorer when using closures. I already knew about the circular reference problem, and until...
2
by: m0nkeymafia | last post by:
I have spent the past few weeks trying to figure a way around this problem, and have yet to find a good enough solution. Internet Explorer leaks memory when I update a div container using...
2
by: Jay | last post by:
I have a web app running on the windows CE device. In one of the asp.net pages - it has javascript code. That seems to have a memory leak. When I run the web app - in about one hour, the app hangs....
10
rizwan6feb
by: rizwan6feb | last post by:
I am working on an ajax chat application, done most of the work but when i tested it on IE (both on IE6 and IE7), found that there is a memory leak. The application works fine on FF. How can i fix...
18
by: Daniel Orner | last post by:
Hi all, I've been trying to pin down a memory leak in IE6 for several WEEKS now. I've done my share of googling etc., and know all about common leaks like circular references and closures, but...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.