473,569 Members | 2,822 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JS/DOM Garbage Collector - Leak prevention

1 New Member
Hello Folks!

Ive recently read some Articles about Javascript/DOM GC CleanUp Problems when doing operations like
Expand|Select|Wrap|Line Numbers
  2. var Obj = createElement("TD");
  3. Obj.onclick = function() { myFunc(); };
  4. document.body.appendChild(Obj);
  5. .....
  6. document.body.removeChild(Obj); //<- Leak because JS function still set
I work right now on a Ajax Grid Class that can have different Events/Properties Attached

I have made the following Code right now to dynamicly remove Events and Objectreference s before removing the DOM Object

(The Sample here demonstrates how i remove entire <TR>'s with Content inside (taking gc Problem into account) / in this case all TR's of a given Table)
Expand|Select|Wrap|Line Numbers
  1.             var Table = document.getElementById("myTable");
  2.             for (var iRow = Table.rows.length - 1; iRow >= 1; iRow--) {
  3.               var CurrentRow = Table.rows[iRow];
  4.               var Cell = CurrentRow.getElementsByTagName("TD");
  5.               for (var i=0; i < Cell.length; i++) {
  6.                 var InputField = Cell[i];
  7.                 for(var Event in InputField) {
  8.                   //alert("Event:"+Event+" Value:"+InputField[Event]+" Typeof:"+typeof(InputField[Event]));
  9.                   if (typeof(InputField[Event]) == "object") { InputField[Event] == null; }
  10.                   if (typeof(InputField[Event]) == "function") { InputField[Event] = null; }
  11.                 }
  12.               }
  13.               Table.deleteRow(iRow);
  14.             }
(I also null Objects because i use some Propeties to store Lookup Object References)

Note: Normaly i would just remove a single TR rather hole Content of a Table

To my Questions:
I suppose NULL'ing the Listener destroys the link in the same way as removeEventList ener would!?
Is this correct?

My next question is, is the above Sample practicable or too many for loops/properties to work with?
I like it because its dynamically and doesnt seem to need extra attention
(also keep in mind that i would normaly only remove a single TR including its TD Content)
^Drawback it loops through really many unneeded Properties while processing

Or would it be better to make a Pseudo Class for each Grid that hold for every Event the informations and Update according?
But that means extra work also multiple Grids would mean multiple History->EventListene rs

or is there a even better aproach?
Hope i can get with some of you people in touch about this ;-)
Aug 21 '08 #1
2 1744
392 Recognized Expert Contributor
A couple of things come to mind here.

1. First of all I do not see a memory leak in the example provide. The JS function is a child of the "Obj" Object so if you remove the element you destroy it's child elements/attributes too. Also what you are showing is an element that references something else, but for leak to occur your element would have to be referenced by something else.

2. Have you verified that a memory leak is actually happening using run time debugging tools? If not then I would not worry about a theoretical leak.

3. Even if a memory leak does exist it is almost never an issue for JS development since the code's scope is all at page level. As soon as the user goes to the next page all of the elements and objects from the previous page are dropped and the browser starts on rendering/executing the next page. So any leaks that are happening never run long enough to create a problem.

4. Even if a leak really does exist and you for some reason are never going to load any other pages, or refresh the page you are on, I believe you can get around the problem by using anonymous function instances. You would change this:

Expand|Select|Wrap|Line Numbers
  1.   Obj.onclick = function() { myFunc(); };
To this :

Expand|Select|Wrap|Line Numbers
  1.   Obj.onclick = new Function(" myFunc(); ");
Expand|Select|Wrap|Line Numbers
  1.   Obj.onclick = new Function(" ////  Paste your myFunc() code here.  //// ");

This will create a new instance of the function instead of a reference to it.
Aug 26 '08 #2
16,027 Recognized Expert Moderator MVP
What you mention about leaving the page preventng memory leaks is not quite true. For example, see Closures - IE memory leak problem. IE can leak memory until the browser is closed if you have a circular reference.
Aug 28 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

by: jzink | last post by:
what is the best performance monitor for me to add to check for the memory being used by my asp.net app ----- Jeremy wrote: ---- >> If I keep an eye on the memory being utilitized by the worker processe (via taskmgr) I see it start at around 30K and will grow to around 140k Task Manager is not an exact way of monitering memory usage, if...
by: Lasse Skyum | last post by:
Hi folks, I've been wanting to write a garbagecollector for C++ for a little while... my idea is that all objects inherited from "CGCObject" will be garbage-collected if no longer used. So all I would have to do is: CMyClass *pTest = new CMyClass(); // CMyClass inherits CGCObject My aproach has been the "mark and sweep" algorithm, but...
by: HalcyonWild | last post by:
Hi, I installed the free version(command line only) of the digital mars c++ compiler. It said it features a garbage collection mechanism, but there was no documentation. I figured out that you have to extend the class mentioned in gc.h file. But it does not compile. Does the compiler automatically extend the gc classes, while compiling.
by: R. MacDonald | last post by:
Hello, all, I am currently working on a .Net (VB) application that invokes routines in unmanaged (Fortran) DLLs. The unmanaged routines then communicate with the .Net application by means of a call-back mechanism. These calls pass a string that contains a "command" and a pointer to a SafeArray that (depending on the command) either...
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...
by: Johnny E. Jensen | last post by:
Hellow I'am not sure what to think about the Garbage Collector. I have a Class OutlookObject, It have two private variables. Private Microsoft.Office.Interop.Outlook.Application _Application = null; Private Microsoft.Office.Interop.Outlook.NameSpace _Namespace = null; The Constructor: public OutlookObject()
by: daniel.internet | 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 ()
by: Sebastian Nibisz | last post by:
SGCL is precise, parallel garbage collection library for C++ (at this time for Windows 32/64 only). SGCL is free software published under University of Illinois/NCSA Open Source License. Get it at: http://sourceforge.net/projects/sgcl/ Regards Sebastian Nibisz
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use one, and in what percentage of your projects is one used? I have never used one in personal or professional C++ programming. Am I a holdover to...
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...
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...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
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...
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...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.