Event Handling, DOM, Closures And Memory Leaks

Oct 18, 2006

I have a question regarding how to prevent memory leaks in Internet
Explorer when using closures. I already knew about the circular
reference problem, and until now was able to prevent memory leak
problems. But I needed to store DOM elements and can't solve it
anymore. So I search the group archive to see if I missed any
valuable information. I found some interesting articles, but somehow
could not apply it to my problem or I did not understand it fully.
In particular the articles that talked about setting variables to
NULL seemed as an easy solution, but I think I didn't understand it,
because it didn't seem to work.

So, let me explain my problem in more detail. I am working on some
very dynamic and complex page. It uses AJAX (XMLHttpRequest) to alter
different parts of the page. This already disqualifies the finalize
method solution to cleanup memory leak problems. I use several
"component classes" to do the work of creating DOM elements in some
container element and provide an easy to use interface for
manipulation the content. For example I can call
component.setBackgroundColor("red")
and the component takes care of changing the style on the correct
DOM element that is encapsulated in the component. In reality the
component uses more complex interface method, but I hope you
get the picture of why I do this.

Let me show you some example code:

function MyComponent()
{
var div;
var handler = null;

this.generate = function generate()
{
div = document.createElement("div");
div.onclick = MyComponent.createClickHandler(this);
// normally more elements are created here
return div;
}

this.setBackgroundColor = function setBackgroundColor(value)
{
div.style.backgroundColor = value;
}

this.getHandler = function getHandler()
{
return handler;
}

this.setHandler = function setHandler(value)
{
handler = value;
}

}

MyComponent.createClickHandler = function createClickHandler(component)
{
return function(event)
{
var handler = component.getHandler();
if (handler != null)
handler(event);
}
}

This "component class" can be used like this:

var container = document.getElementById("container");
var component = new MyComponent();
container.appendChild(component.generate());
....
component.setBackgroundColor("red");
component.setHandler(function(event) {alert("Stop touching me!")});

The problem, of course, is that this code will create a memory leak
in Internet Explorer. I need the component in the event handler to
get the handler dynamically, but the div is stored there too,
creating a circular reference.

One of the things I tried doing is making a DOMStorage "class" like
this:

function DOMStorage()
{
var map = new Object();

this.get = function get(id)
{
return map[id];
}

this.put = function put(id, obj)
{
map[id] = obj;
}

}

var storage = new DOMStorage(); //global

Instead of storing the div element directly in the component, I store
it under an id in the DOMStorage and use it to retrieve it later.
This actually prevented the memory leak. I don't really understand
why, because I still see a circular reference. Maybe Internet
Explorer does not count references in the global scope as a circular
reference? When I move the global storage to inside the container
object I get the memory leak again.

Unfortunately I am unable to use a global DOMStorage, because the
"component class" in instantiated many times, and they must all have
their seperate DOM elements.

Perhaps I have to generate unique ID's when I put a DOM element into
the global storage? It seems so over-the-top for something that works
perfectly fine in Firefox.

What are my alternatives?

View 2 Replies


ADVERTISEMENT

Prevent Memory Leaks In IE6 And IE7?

Apr 26, 2011

What is the best way to prevent memory leaks in IE6 and IE7?

I'm working on an internal web app that just gets slower and slower, using more and more CPU load and memory, unless and until you close the browser and start, again.

View 1 Replies View Related

Memory Leaks In AJAX Application (in Opera)

Mar 1, 2006

I have the AJAX-script. It eats memory about 4Kb per one callback.
Script reflects messages from server application in real-time. I form
messages, and put them into the iframe. If mesages more than 40 last
message delete. Can you check the script and say about my mistakes? Code:

View 3 Replies View Related

Checking For Memory Leaks And Garbage Collection

Mar 9, 2007

I'm working on an Ajax library that I plan to use on several upcoming
projects. Everything seems to work just great...

Now I want to get into the finer aspects of checking things... How
much memory am I using, are my objects making themselves available for
garbage collection adequately, etc.

Are there tools you can recommend (especially for IE and Firefox) that
I can use to get this information?

View 1 Replies View Related

JQuery :: Tablesorter Plugin Leaks Memory In IE6 And IE7

Jun 15, 2009

I have a table, 10 cols, 200 rows. Using tablesorter causes a memory leak on every page refresh of almost 2mB. A smaller table causes a proportionately smaller memory leak. Is there way to clear this memory? I've tried setting inner html of the table to '', but it makes no difference. Is there even a universal method i can call to remove any trace of any jquery plugin I have on the page?

View 3 Replies View Related

JQuery :: Memory Leaks With Ajax Calls?

Oct 27, 2010

I have a jquery based system with ajax calls instead of page refreshes. Every time I return some ajax content, it replaces the content on the main div.

function execcmdcallback(data, textStatus, XMLHttpRequest)
{
$("#divmain").html(data);
data = null;
}

Each time this executes, the browser memory increases by about 600-900kb. Can anyone suggest a way of doing this where the old memory is freed so there is no significant increase in browser memory on each ajax call ?

I've studied this a bit, [URL]..., but I have to admit I don't quite understand it I see the same issue on IE8, FF and Chrome. FF comsumes less memory per ajax call and frees some memory seconds later, but still the total working set is increasing on each call by 300k in FF and 600 to 900 in chrome and IE

View 15 Replies View Related

Replacing HTML Without Memory Leaks And Redundant Code

Dec 1, 2010

I'm building a JavaScript-based calendar for a client that will require me to replace the page's HTML based on the user's input. For example, if the user clicks on a particular date, then the month/week calendar will be replaced with the day calendar. Needless to say, there are several event listeners involved. However, if I suddenly swap out the month calendar for a day calendar, does that mean that there are several event listeners in memory for elements that no longer exist? Or are those listeners destroyed when the elements are destroyed? Basically my question is, every time I swap out the HTML, do I have to detach all of the old events too?

View 3 Replies View Related

AJAX :: Memory Leaks - Replacing Nodes With Frequent Updates

Aug 7, 2010

I've been searching the web for a while now, and I haven't come across a conclusive solution for memory leaks due to replacing nodes with frequent AJAX updates. I wrote a class that pulls an RSS feed frequently (e.g. every 5 seconds) and updates an HTML element with new information. In the process, it removes the old nodes and replaces them with new ones. It works fine visually. The only problem is that it has terrible memory leak complications; when I put the script on to run, it increases the memory usage by about 1MB every few seconds at an accelerated 1000-ms delay between updates!

(That's 100MB every few minutes!) I discovered this when I let the script run for a few hours straight, not thinking anything of it, and when I returned to my computer, it was frozen up Opera seems to be the worst at its memory management on this script, Safari and Chrome are in the middle, and Firefox seems to handle it the best, but nonetheless there are memory leaks in all four. (I haven't tested IE yet, but based on what I read, I would expect that it might even be worse than Opera!)

[Code]....

View 4 Replies View Related

Stop Memory Leaks - Extension Tend To Crash When There Is No Internet Connection

Dec 4, 2010

The problem is, the extension tend to crash when there is no internet connection, and sometimes, the http requests fail to communicate with gmail server, they got built up over time and end up flooding the memory.

[Code]...

View 4 Replies View Related

Event Handling And Nested Div

Jan 20, 2008

I have two div elements (both dynamically created, one within the other). The parent div has an event attached to it:

contextmenuDIV.onmouseout = function () {doSomething()}

for example. However the onmouseout event will fire when you mouseover the child div as (of course) I am technically leaving the div layer (despite it being the parent).

Is there anyway to cancel this? I've read all about bubbling and all that, but it's just confusing me even more!

Anyway, at all, to allow me to have divs within another div element but only allow the event to fire for the parent!

View 2 Replies View Related

Inline Vs Dom Event Handling?

Jan 11, 2009

why I should use the dom level 2 event handling over inline events like ...

Code HTML4Strict:
<a href="http://www.yahoo.com" onClick="myFunction()">click me</a>

I work in a team of developers and our pages are dynamically created using Java.I can at any moment change the inline JS across a site due to this so changes are easier then if the site was just static HTML.As far as reasons to use dom 2 over inline, I looking for something beyond:

it separates behavior from html

it is a best practice

it is the 'modern' way of doing it

I need facts that explain why it is a best practice or why 'modern' is better like : you can only assign one function action to the event.That was just an example which I see but is not true to me.I can either assign multiple functions to it like:

Code HTML4Strict:
<a href="http://www.yahoo.com" onClick="myFunctionA();myFunctionB();myFunctionC()">click me</a>

or I can call my functions from a single function call like

Code HTML4Strict: <a href="http://www.yahoo.com" onClick="myMultiFunctionCall()">click me</a>

and then have a function defined before the inline call like

Code JavaScript:
function myMultiFunctionCall()
{[code].....

Here the second statement overwrites the first.By the way, here is a con for using dom and to me a big one considering debugging: you can't see what event handlers are assigned to what unlike inline where it is obvious because it is in the page. see http:[url].......

View 5 Replies View Related

Mouse Over Event Handling?

Nov 17, 2011

I am having a list (ul) in which a mouse over will populate its sub categories as list under that(like tree) and so on. My problem is, when passes mouse over a 2nd or 3rd level element, 2 mouse over events will be fired (The actual one and its parent) The sample code is given below

HTML Code:
<ul>
<li>
Root[code]............

View 2 Replies View Related

Event Handling :: EventHandlers For Each Of The 3 Div's Should Be Fired?

Apr 14, 2011

I have a question about event handling in javascript.Let's say I have 3 divs, directly in the body of an html document.

<html>
<body>
<div id="div1black"></div>[code].......

Each of those div's is positioned differently using css, as displayed in the image below: Each div has an event listener for the click event.In the image above I marked a point. If the user would click on that point, are all 3 event listeners supposed to fire? As I understood it, there's a capture and a bubbling phase, but these only go down through and up from the ancestors of the target right? And none of the div's are ancestors of eachother, they just happened to overlap because of the css position.

How would I let all registered eventsHandlers be fired whenever a user clicks on overlapping HTML elements? (so the eventHandlers for each of the 3 div's should be fired).

View 2 Replies View Related

Cross Browser Event Handling

Feb 28, 2007

I have a document with a parent element that has mousedown,mouseup and mousemove events registered to it. The parent element has some child elements as well. I observe inconsistent behaviours between browser types when I hold the mouse button down, move the pointer and release the button.

ie7 - mouseup fires when over a child or parent

op9 - mouseup fires only when over the parent

ff2 - mouseup fires only over a child or parent but only if the pointer has moved since the mousedown occured

View 2 Replies View Related

Event Handling Function Walk Through

Jan 24, 2009

Because much of the talented JS community doesn't frequent the 'looking to hire' marketplace boards, I wanted to link to my post looking for help in understanding fully a group of functions I would like to use. While much of this I understand, along with some of the OOP principles used, there are some hang-ups I'm running into. Therefore, I'm looking for someone to walk me through the functions and help me understand the logic behind it. And since time is money, especially for those who are more skilled, I would rather respect this and pay for the conversation.

Further, if the person who takes up the task doesn't object, I will document the functions use for the community to benefit, since these are public boards. Of course, giving full credit to the original writers and the person who helped me understand.

View 1 Replies View Related

JQuery :: KeyDown Event - Clear Memory From Pressed Keys

Aug 17, 2011

I have a code for the keydown event:
-when you press the UpArrow key the image moves up
-when you press the DownArrow key the image moves down.....and so on
But don't forget that this is under the keydown event...so when I keep pressing the UpArrow key the image moves up but when I release the key the image still moves up. I already put the code $('myImage').stop(); on my image on keyup event but this doesn't do anything. There is a code to clear my memory from pressed keys or something?

View 7 Replies View Related

Problem On Handling Two Iframes' Onload Event

Jul 20, 2005

I create two iframes dynamically to get data from the server.
I want to deal with the data after it's downloaded, so I set the two
iframes' onload event handlers to current document(not the iframe self), the
handlers' JS code is generated dynamically too.

But I found only the second event is triggered and I can't get the first
iframe's onload event.

Why? The data in the first iframe is really arrived, but it doesn't trigger
the onload event.

Is there any other better way to conceal download data from server, not
refresh current document?

View 1 Replies View Related

JQuery :: Handling Window.change Event In IE

Jan 8, 2010

i'm trying to do some event delegation, and i can't seem to get the onchange event working in IE. i've tried:

$(document).change(function () { alert('fuck. me. IE sux'); }); $(document.body).change(function () { alert('fuck. me. again'); });

and later i have:

<select id="aSelect"> <option value="0">a first option</option> <option value="1">a second option</option> <option value="2">a third option</option> <option value="3">a fourth option</option> </select>

but this doesn't work in IE. i've also tried to wire these up manually: document.body.onchange = function (e) { alert('foo); }; and document.onchange, but those don't work either. what's the story here (other than the fact that IE is sucking balls)?

View 5 Replies View Related

JQuery :: Handling Of Click Event - API Documentation ?

Aug 21, 2009

Suppose I have this syntax:

I want tellme() to handle the value of href of the clicked link.

What syntax do I put in tellme()?

I got this far:

Here is question: How would I get this info from the API documentation?

View 7 Replies View Related

JQuery :: IE Click Event Handling In 3 States

Aug 4, 2009

I'm working on a small plugin that extends a checkbox to behave as if it has 3 states. Basically it just adds an anchor above the underlying checkbox, intercepts mouse clicks, and cycles the checkbox between:
disabled + unchecked
enabled + unchecked
enabled + checked

It works fine in FF. But in IE7, for some reason, the click events are always sent to the underlying checkbox instead of the overlaid anchor. The anchor is sized to completely overlay the checkbox (and in fact, I made it's slightly bigger so clicking at the very edges
works). Incidentally, I'm using jQuery 1.2.6 since this is part of a much larger web application and we haven't yet worked out the incompatibilities we're seeing with the latest jQuery version.

Here's the plugin code (the alerts are for debugging in IE)
(function($) {
$.fn.checkbox3s = function(options) {
var opts = $.extend({}, $.fn.defaults, options);
return this.each(function() {
var proxy, cbx = $(this), parent = cbx.parent();
parent.css({position:"relative"});
proxy = $("<a href='#'></a>").css({
position:"absolute", .....

View 5 Replies View Related

Javascript Event Handling And Page Load

Nov 13, 2007

I understand that when loading a page, the html parser is suspended until any script that it comes across is executed. i also believe that certain browsers will allow user input for those parts rendered whilst the rest of the page is still being parsed. suppose that a user clicks a button that is visible and 'active' and fires off some lengthy javascript execution causing the ui to 'freeze' for a while. my question is, on a page that is still being parsed but allows user input on those parts parsed and rendered, will the parsing of the rest of the page cease whilst the event handler (onclick) is working or will the user see the page render simultaneously as the event handler does its job?

View 7 Replies View Related

JQuery :: Handling .hover Event With Nested List?

Nov 17, 2011

I have a nested lists. I'm using hover event to trigger an event. But when I hover child nodes, the event of theancestor list is being fired. How can I get rid of this situation

View 1 Replies View Related

JQuery :: Handling Paste Event On Mouse Click?

Apr 14, 2011

I need to call a function when user copy and paste the text value in the text box. I tried using "mousedown" event. But "mousedown" event handles left and right clicks when user clicks on the paste link on right click unable to handle the event. I am using jquery 1.5.

View 1 Replies View Related

JQuery :: Handling Key Event In Chrome Or Safari During A Popup?

Jun 9, 2009

I am currently following the image popup example from:[URL]If I run the demo on Windows XP on Chrome or Safari, the escape keydoesn't close the popup window. How do I handle this in jQuery for

View 2 Replies View Related

Cross-Browser Event Handling - In IE8 It Doesn't Work?

Jul 18, 2010

I'm trying to augment Object.prototype with an addEvent method that will add event listeners, and will work regardless of whether the browser is IE or not.Here's what I have: So far it seems to work in non-IE browsers, but in IE8 it doesn't work. Where am I going wrong?

Code:
(function(){
try{[code]....

View 2 Replies View Related

Event Handling For Dynamically Created Html Element?

Mar 23, 2011

Am new to javascript and am having this problem;

//Some other code
//Here I create an input element of type text and assign a onclick event property
var quantityTxt = document.createElement("input");
quantityTxt.type = "text";
quantityTxt.onclick = calcAmount();

[Code]...

Now my problem is that the above function gets called even when the cell has not been clicked, hope am clear enough,

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved