473,320 Members | 1,896 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.

Javascript memory leaks?

Jack Slocum claims here

http://www.jackslocum.com/yui/2006/1...id-javascript-
memory-leaks/

that "almost every site you visit that uses JavaScript is leaking memory".

Anybody know anything about this?
Does *Javascript* leak memeory, or
does the *browser* leak memory?
Oct 3 '06 #1
3 5270
Jim Land wrote:
http://www.jackslocum.com/yui/2006/1...id-javascript-
memory-leaks/
that "almost every site you visit that uses JavaScript is leaking
memory".
I'm skeptical. This conclusion would require that the "Leak Extension" for
firefox be detecting only valid leak scenarios. I see no proof or evidence
on its site that this is the case. Experienced javascript developers _know_
when leaks are created and can easily scan code for closures with circular
references in their scope chain, for example.

Since FF doesn't suffer from the same circular reference problem that IE
does, I'm not sure how this extension can reliably detect situations where
IE would leak memory.
Does *Javascript* leak memeory, or
does the *browser* leak memory?
Javascript as a language certainly cannot leak memory, because there is no
way to manually allocate and deallocate memory. Since it is garbage
collected, it's up to the host environment to worry about cleaning up
memory. So, it's the browser's implementation of javascript that would cause
a memory leak.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Oct 3 '06 #2
Matt Kruse wrote:
Jim Land wrote:
http://www.jackslocum.com/yui/2006/1...id-javascript-
memory-leaks/
that "almost every site you visit that uses JavaScript is leaking
memory".

I'm skeptical. This conclusion would require that the "Leak Extension" for
firefox be detecting only valid leak scenarios.
It reports memory leaks only for very specific cases (and seems
somewhat buggy at that). I don't know whether it tests for actual
leaks or just scenarios that might induce leaks. I tested adding a
closure using both dot property access and addEventListener, only the
addEventListener version reported a memory leak.

I see no proof or evidence
on its site that this is the case. Experienced javascript developers _know_
when leaks are created and can easily scan code for closures with circular
references in their scope chain, for example.
I think that's a rather optimistic generalisation. It may be better to
say that web developers should know how to detect memory leaks and
should test client performance thoroughly (and optimise where
necessary). Developers should also be aware of what causes memory
leaks so that they can either be avoided or cleaned up.

Since FF doesn't suffer from the same circular reference problem that IE
does, I'm not sure how this extension can reliably detect situations where
IE would leak memory.
According to Leak Monitor, you can create memory leaks using
addEventListener with Firefox, just like those in IE (though apparently
it's fixed in version 1.8, I tested in Firefox 1.5).

Does *Javascript* leak memeory, or
does the *browser* leak memory?

Javascript as a language certainly cannot leak memory, because there is no
way to manually allocate and deallocate memory. Since it is garbage
collected, it's up to the host environment to worry about cleaning up
memory. So, it's the browser's implementation of javascript that would cause
a memory leak.
ACK.
--
Rob

Oct 3 '06 #3
VK
Javascript as a language certainly cannot leak memory,
because there is no way to manually allocate and
deallocate memory.
If Java-like memory management systems would exclude memory leaking as
such: the world would program on nothing but Java long ago :-)

Such systems can perfectly leak as well, the difference is in the
mechanics and in the definition itself of "leaked memory". We can
compare C-like memory management and Java-like memory management
respectively with the garbage removal in a building and in a private
house. In a flat we put all trash into trash can and manually dump it
into collector. The question of "when the trash will be removed?" has
no application to the situation: it is removed at the moment of
dumping. In a private house there is a "garbage day" each week when we
have to place our container at the right place at the right position.
We cannot tell for sure when the track will come: at 6am, 7am or
afternoon. The only thing that should bother us is to place the
container properly. If it is placed properly, it will be eventually
emptied up sometime during the trash pickup day.
It seems that many C-ground programmers just refuse to fully accept
such memory management. Theoretically they know about it, but as soon
as it comes to the practical development they just come back to what
they used to. This is why I guess 99% of "memory monitoring" software
for javascript is written from C positions and therefore 100% useless
for javascript.

In javascript memory management is *automated* which means that lesser
you put your sneeky hands into it - better it works :-)) :-|

It also impossible to have a leak with fully dereferened objects. They
may remain in the memory for uncertain period of time but it is not a
leaked memory. A memory leak occurs then Garbage Collector cannot mark
a scavenger as free for removal: which means that despite the program's
author may think the opposite, there is at least one reference left
somewhere to this object. The "circular reference leak" in IE is a
classical sample of such situation.
>From the explicit memory management actions I can name only one:
dereferencing of an unused object by either setting it to null or by
using delete operator. (Important: setting a reference to undefined
value doesn't free the reference: it will remain a valid reference with
a valid explicitly assigned value). If all references to the object are
removed ("trash container is placed into the right position in front of
the house"): then in the next scan Garbage Collector will mark the
relevant scavenger (internal representation of the object) as garbage
collection available. Garbage Collector works in a "double stage" mode:
i) on the first stage it marks unused scavengers, ii) on the second
stage it actually destroys the heaps. Depending on available free
memory, intensity of the program process etc. the second stage may
happen a minute or an hour later: or it will never happen at all until
exit: if available memory is big and/or memory usage remains low. That
is NORMAL, that is OK, that was made this way for maximum optimization
(I feel like scream it out loud sometimes :-)

In Java itself you can *ask* the system to expedite the process by
using System.gc() method. It doesn't enforce the garbage collection, it
only trigs free memory check and it removes the garbage *if* the free
memory level is getting low. So most of the time System.gc() does
nothing useful except bringing a false feeling of peace to the
programmer who uses it.

JScript (but not other javascript versions AFAIK) can be more rude: by
using undocumented CollectGarbage() method you can *enforce* an
immediate removal of all scavengers marked as free to remove. But why
would you need it in a practical application? Are we so terribly short
on memory to sacrifice the execution speed for 100Kb - 500Kb ?

Internal invocation in javascript (at least in JScript) is made as
simple as a moo-cow: there are not any virtual table bindings and
dispid's are not cached. It means that in the code like:
for (var i=0; i<10000; ++i) {
foobar.foo.bar();
}
the dispatcher will request 10000 times a reference to the object
matching "foobar.foo.bar" string in the names table. That may seem as
ineffective, but it allows to keep the engine small and clean and it
allows many nice javascript'ish things like a function referencing
other function which doesn't exists yet at the moment of referencing.
In application to the memory management it means once again that you
have neither tasks nor worries but deference unused objects properly:
if you have objects needed at the beginning and never needed anymore
from some point to the end. If you have not such objects, then you can
do your best for the memory management by *doing nothing* and by
letting the automation to make the job for you.

Used memory growth by itself is *meaningless* in Java-like memory
systems. It can use say 9Mb out of allowed 10Mb while doing foo = 2 *
2; - and at the next moment it will start doing matrix transformations
and the memory usage will fail to 100Kb.

Oct 3 '06 #4

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

Similar topics

4
by: Maurice | last post by:
Hi there, I'm experiencing big memory problems on my webserver. First on an old RedHat 7.2 system, now on an other fresh installed Suse 8.2 system: Linux version 2.4.20-4GB...
5
by: Justice | last post by:
Currently I'm doing some experimenting with the XMLHTTP object in Javascript. Now, the XMLHttp object is asynchronous (at least in this case), and the following code causes a significant memory...
0
by: Steve Binney | last post by:
My code makes synchronous HttpWebRequest and HttpRebResponse calls. In VS 2003, I am getting memory leaks and event handle leaks. I am closing all streams and using "using"statements. I have...
2
by: Generic Usenet Account | last post by:
I have been using STL for a long time now, without any problems. Recently we generated a purification report on our software using Rational Purify, and we found some memory leaks. My colleague...
7
by: julian.tklim | last post by:
Hi, I need to build an editable Datagrid with add & delete buttons on each row using javascript. DataGrid need not be pre-populated with values. To make the thing complicated, one of the...
8
by: =?Utf-8?B?QW1yaXQgS29obGk=?= | last post by:
Okay, after much research, I have discovered a few interesting things in ASP.NET. I have a MasterPage that has a WebForm on it and it looks like this: <body> <form id="controls"...
9
by: Joshua | last post by:
I posted this originally in the csharp group, but I think that may be the wrong group. This seems more appropriate: I'm running into an issue with a memory leak in an Asp.Net web page. In the...
2
by: jhaxo | last post by:
How can I implement a cross browser javascript strip chart that does not leak memory. I have treid several off the shelf and home made charts. the basic test is to leave them running, redrawing the...
16
by: graham.keellings | last post by:
hi, I'm looking for an open source memory pool. It's for use on an embedded system, if that makes any difference. Something with garbage collection/defragmentation would be nice. It should have...
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.