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

JS/DOM Garbage Collector - Leak prevention

Hello Folks!

Ive recently read some Articles about Javascript/DOM GC CleanUp Problems when doing operations like
Expand|Select|Wrap|Line Numbers
  1.  
  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
  7.  
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 Objectreferences 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.             }
  15.  
(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 removeEventListener 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->EventListeners

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 1735
pronerd
392 Expert 256MB
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(); ");
or
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
acoder
16,027 Expert Mod 8TB
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

1
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...
2
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...
8
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...
5
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...
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...
56
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 =...
4
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...
72
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...
350
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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: 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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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.