473,698 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Closures memory leak

Hi,

Do I understand correctly from
http://jibbering.com/faq/faq_notes/closures.html that the following will
create a memory leak in IE?
function createMemoryLea k(id)
{
var el = document.getEle mentById(id);
el.onclick = function()
{
return false;
}
}

If not, could someone give an example that would create a memory leak?
Jul 28 '05 #1
2 1776
On 28/07/2005 11:48, Robert wrote:
Do I understand correctly from
http://jibbering.com/faq/faq_notes/closures.html that the following will
create a memory leak in IE?

function createMemoryLea k(id)
{
var el = document.getEle mentById(id);
el.onclick = function()
{
return false;
}
}


Yes. The element, el, has a property, onclick, which is a function
reference. In the scope chain of that function exists a reference to the
same element, so you now have a loop. The simple fix is to assign null
to el once the listener assignment is complete.

If, for some reason, you needed to keep a reference to el, you would
need to break the loop when the unload event is dispatched. For instance:

var global = this;

function avoidMemoryLeak (id) {
var el;

if(document.get ElementById) {
el = document.getEle mentById(id);
}
if(el) {
el.onclick = function() {
return false;
};

global.onunload = function() {
el = global.onunload
= null;
};
}
}

But, here you wouldn't need to worry as the this operator could be used
within the listener, rather than el.

If you'd end up producing many closure instances where memory leaks may
occur (which could simply mean calling avoidMemoryLeak several times),
then this becomes unmanageable in its current state. Richard Cornford
has produced code that can help in this regard:

<URL:http://www.litotes.dem on.co.uk/example_scripts/finalizer.html>

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jul 28 '05 #2
Michael Winter wrote:
If, for some reason, you needed to keep a reference to el, you would
need to break the loop when the unload event is dispatched.


Well, the page I am working on is altered dynamically using XML
requests. So unload is not good enough. However I will be able to avoid
the memory leak by calling another function that returns a function and
assign it to the event handler.
Jul 28 '05 #3

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

Similar topics

10
14088
by: Matt Kruse | last post by:
I'm aware of the circular reference memory leak problem with IE/closures. I'm not sure exactly how to resolve it in this situation. Also, Firefox appears to grow its memory size with the same code. So I'm wondering if I'm missing something? My test code is as follows: function myObj() { var req = new Object(); req.temp = 0;
8
3409
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? In nut shell, what is/are the realtion/s between the Memory Leak and Memory Corruption. Juts Theoritical Assumtion below:
23
4552
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
2
3052
by: Jake Barnes | last post by:
Using javascript closures to create singletons to ensure the survival of a reference to an HTML block when removeChild() may remove the last reference to the block and thus destory the block is what I'm hoping to achieve. I've never before had to use Javascript closures, but now I do, so I'm making an effort to understand them. I've been giving this essay a re-read: http://jibbering.com/faq/faq_notes/closures.html
10
2070
by: Emre Sevinc | last post by:
Take a look at the following snippet: <html> <head> <script> function add(elementId) { var container = document.getElementById(elementId); for (var i = 0; i < 10; i++) { var elt = document.createElement('div'); elt.innerHTML = "" + i;
2
2365
by: Robert | last post by:
Hello javascript group readers, 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...
14
2174
by: Khookie | last post by:
Woah... is it just me or do C programmers don't bother talking about how cool C can be (compared to Lisp, Haskell, etc.) - functionally speaking? // Lexical scoping - via nested functions #include <stdio.h> int main() { int x = 10;
1
1648
by: Jeremy | last post by:
I'm working on an application that does some DOM manipulation. I want to add a mechanism for "Undo" so that the user can revert to the previous state after performing a mistaken action. Simple idea, not necessarily simple to implement. My first idea for this is to use closures and an array that tracks the inverse of the user's actions. Here's a simple example in which the user can only remove DOM nodes and then undo the removal...
22
9340
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a memory leak someplace. I can not detect the memory leak by running several reports by hand, but when I run tha app as a servrice and process few hundred reports there is significant memory leak. The application can consume over 1GB of memory where it...
0
8676
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
9029
jinu1996
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8898
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
8870
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
7734
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...
0
5860
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
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3051
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
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.