473,626 Members | 3,119 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.XMLHT TP");
obj.onreadystat echange = 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
(onreadystatech ange), 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 2992
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...@y ahoo.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 UriScavengerPer iod 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.XMLHT TP");
obj.onreadystat echange = 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
(onreadystatech ange), 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
9363
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 the function exits do i have a memory leak or other trouble brewing? ie: function MyFn() { var mydate;
10
2037
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
3843
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
3793
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 can manage resources as well as objects. The programming style is clear and easy, conforming to RAII (Resource Acquisition Is Initialization) idiom of C++ programmers. The memory usage is very efficient, acyclic garbage is
28
3163
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 this work be library based or language based and will it be based on that of managed C++? Then of course there are the finer technical questions raised (especially due to pointer abuse). Is a GC for C++ just a pipe dream or is there a lot of work...
25
3007
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 MyClass:IDisposable
42
2678
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. Well, I came across Scott Meyer's SmartPtr example from some 10 years ago. I like the template member function for type conversion to solve inheritance issues. On MSVS 2003, I get the below error, if I declare the constructor SmartPtr(T*...
1
1583
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 ActiveXObject("Microsoft.XMLHTTP"); obj.onreadystatechange = function ()
30
3809
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 the end of the app for example: class test { public: int x;
0
8262
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8196
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7192
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6122
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5571
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.