473,320 Members | 2,004 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,320 software developers and data experts.

XMLHttpRequest & garbage collector

Hello,

I got one question, that may seems trivial to you, but I really dont
understand it. Let's say we got this code:

function ajax()
{
var obj;
obj = new ActiveXObject("Microsoft.XMLHTTP");
obj.onreadystatechange = function ()
{
[anycode];
};
obj.open("GET", "http://www.park.sk", true);
obj.send(null);
}
(for simplicity i created XHR just for IE as activex). As far as I
know, browsers's garbage collector should discard all function's local
variables after functions exits, if none of inner objects (variables)
got reference out of that function. But as we all know, XHR object
persists in memory and becuase it refers to locally created function
(onreadystatechange), all local varibales stay in memory as well
(because of closures). But why does garbace collector not discard
activex object after function ajax exits? It does not have any
reference out of that function's scope.

It looks like that browser creates some hidden reference to that
activex object AFTER i call "send" method (so browser wont discard
it), and delete that reference after XHR objects's state change to
LOADED.

Please make it clear to me. I really cant find the answer.

Nov 11 '07 #1
4 2956
VK
On Nov 11, 2:17 pm, daniel.inter...@park.sk wrote:
It looks like that browser creates some hidden reference to that
activex object AFTER i call "send" method (so browser wont discard
it),
No, an outside reference is created at the moment of executing
new ActiveXObject(...
In case of IE a reverence is placed to the query pool provided by
HTTP.sys This reference also ensures that the entire closure - so the
whole function ajax() with all its current arguments and internal vars
- gets "frozen" so excluded from the garbage collection.

As a side note: the default garbage collection frequency for HTTP.sys
pool is 3 minutes, JScript garbage collector frequency is 1 minute -
unless one has changes default registry settings. That means that with
a low load it may take up to 4 minutes before memory release. It is
not a problem for an automated memory management, because with higher
load GC passes gets more frequent right a way. Just a note for a C++
programmer if she looks at SysMonitor after each operation - a totally
useless doing most of the time in Java/JavaScript :-)
and delete that reference after XHR objects's state change to
LOADED.
It may or may not - depends on what is there in your closure, that is
especially true for outside objects - and ActiveX are alien objects to
JScript engine. In many serious libraries the clearance is made
manually, see for instance AJAXToolBox:
http://www.ajaxtoolbox.com/request/source.php

See also:

http://blogs.msdn.com/ericlippert/ar.../17/53038.aspx
"How Do The Script Garbage Collectors Work?"

http://msdn.microsoft.com/msdnmag/issues/02/04/web/
"Why doesn't the garbage collector release a reference to an ActiveX®
object when the page is refreshed?"

Nov 11 '07 #2
VK
On Nov 11, 5:28 pm, VK <schools_r...@yahoo.comwrote:
As a side note: the default garbage collection frequency for HTTP.sys
pool is 3 minutes
Corection: 120 sec, so 2 min

So with the default UriScavengerPeriod and GcValTrigger registry value
up to 180 sec (3min) before the memory is actually released. Again:
with the low load / big free memory.

Nov 11 '07 #3
da*************@park.sk wrote:
I got one question, that may seems trivial to you, but
I really dont understand it. Let's say we got this code:

function ajax()
{
var obj;
obj = new ActiveXObject("Microsoft.XMLHTTP");
obj.onreadystatechange = function ()
{
[anycode];
};
obj.open("GET", "http://www.park.sk", true);
obj.send(null);
}
(for simplicity i created XHR just for IE as activex). As
far as I know, browsers's garbage collector should discard
all function's local variables after functions exits,
Not if a closure is formed. If a closure is created the containing
function's local variables cannot be discarded until the closure is
removed.
if none of inner objects (variables)
got reference out of that function.
But as we all know, XHR object persists in memory and
becuase it refers to locally created function
(onreadystatechange), all local varibales stay in memory as well
(because of closures). But why does garbace collector not discard
activex object after function ajax exits?
That is IE's circular reference memory leak problem. You have created a
circular chain of references that includes a COM object (the XML HTTP
request object) and javascript objects (the inner function object and
the Activation/Variable object of the outer function). IE (at least <=
6) cannot break that circle and cannot see when it is isolated from the
rest of the system, so it cannot garbage collect any of the objects
involved.
It does not have any
reference out of that function's scope.
That doesn't matter here.
It looks like that browser creates some hidden reference to
that activex object AFTER i call "send" method (so browser
wont discard it), and delete that reference after XHR
objects's state change to LOADED.
No, if you break the circle then IE will garbage collect the objects
when they are finished with.
Please make it clear to me. I really cant find the answer.
Google for "IE Memory leak" and you will find answers (and 50% or so of
them will not be paranoid over-reactions).

Richard.

Nov 11 '07 #4
Richard Cornford wrote:
>
That is IE's circular reference memory leak problem. You have created a
circular chain of references that includes a COM object (the XML HTTP
request object) and javascript objects (the inner function object and
the Activation/Variable object of the outer function). IE (at least <=
6) cannot break that circle and cannot see when it is isolated from the
rest of the system, so it cannot garbage collect any of the objects
involved.
I test it on IE7, the same problem!
Nov 11 '07 #5

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

Similar topics

2
by: Eric | last post by:
Hi, I'm used to C/C++ where if you "new" something you really need to "delete" it later. Is that also true in javascript? if i do "mydate = new date();" in a function and dont "delete mydate" when...
10
by: pachanga | last post by:
The Hans-Boehm garbage collector can be successfully used with C and C++, but not yet a standard for C++.. Is there talks about Garbage Collector to become in the C++ standard?
1
by: jarit | last post by:
Hi, Found these coding guidelines for C#, HTML, Javascript, Java, HTML, PL/SQL, T-SQL, VB and VBScript. Well written and free to download. www.demachina.com/products/swat Jeroen
13
by: Mingnan G. | last post by:
Hello everyone. I have written a garbage collector for standard C++ application. It has following main features. 1) Deterministic Finalization Providing deterministic finalization, the system...
28
by: Goalie_Ca | last post by:
I have been reading (or at least googling) about the potential addition of optional garbage collection to C++0x. There are numerous myths and whatnot with very little detailed information. Will...
25
by: Koliber (js) | last post by:
sorry for my not perfect english i am really f&*ckin angry in this common pattern about dispose: ////////////////////////////////////////////////////////// Public class...
42
by: coder_lol | last post by:
Thanks everyone again for contributing to helping me clear C++ confusions. I did some serious reading on copy constructors and assignments and I think I've got a good handle on the memory stuff. ...
1
Dasty
by: Dasty | last post by:
Hello, I got one question, that may seems trivial to you, but I really dont understand it. Let's say we got this code: function ajax() { var obj; obj = new...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.