Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8336073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:52:30+00:00 2026-06-09T03:52:30+00:00

I am reading an online JavaScript tutorial in which is claimed the following code

  • 0

I am reading an online JavaScript tutorial in which is claimed the following code will cause memory leak in IE earlier than version 8.

function setHandler() {
  var elem = document.getElementById('id')
  elem.onclick = function() { /* ... */ }
}

The author also provided the fix to prevent the memory leak:

function setHandler() {
  var elem = document.getElementById('id')
  elem.onclick = function() { /* ... */ }
  elem=null;
}

Why does the original code cause a memory leak and how does the fix prevent it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:52:32+00:00Added an answer on June 9, 2026 at 3:52 am

    The article gives a good explanation, the problem is IE and circular references.

    That means that when you do:

    function setHandler() { 
      var elem = document.getElementById('id') // (1)
      elem.onclick = function() { /* ... */ } //(2)
    }
    

    the first line of the function is refencing the second, and the second is referencing the first and that causes IE to not free the memory it allocated to create the elem variable.

    You are breaking the reference by explicitly removing the second reference, by “destroying” the value of elem, in the line

    elem = null
    

    so IE can free the memory

    The second reference happens because there is a problem with a closure, the inner function bound in the onclick has access to elem (it exists in the function scope), so it’s “locked” there.

    In other words a there are two references to elem, one created in the var statement and one is made in the onclick function, and that reference won’t be released until the closure is released.

    You can find more info here, here and in this stack overflow answer

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was looking online for good javascript tagging libraries. I was reading the following
I was reading some online material on reserved JavaScript keywords and I came across
I'm reading through the excellent online book http://nodebeginner.org/ and trying out the simple code
Reading the Online Developer Guide page on XML layouts , I've found the following
I am reading an online tutorial that says if the <script></script> is right on
I'm learning Java by reading the online Java book, but im finding it hard
I'm looking for some advice on how to go about reading the online documentation
After searching online and reading the Oracle documentation for this, I need some help
I pulled up the NWmatcher source code for some light morning reading and noticed
Reading online about this topic is confusing. Is there a way to cut around

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.