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 8878015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:36:37+00:00 2026-06-14T19:36:37+00:00

Every once in a while I find myself doing something along the lines of

  • 0

Every once in a while I find myself doing something along the lines of the following

var img = new Image();
img.src = 'php/getpic.php?z=' + imid + '&th=0';
img.onload = function(){drawImages(img,contexts,sizes)};

Explanation

  1. Create an HTML image element.
  2. Assign its src attribute
  3. Assign its onload event
  4. Pass one or more Canvas contexts to the event handler
  5. Draw the loaded image to the canvases

The thing I am not clear about is this – will the JavaScript garbage collector deal with the task of discarding the img element or do I need to do it myself or else face a slooow memory leak?

  • 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-14T19:36:39+00:00Added an answer on June 14, 2026 at 7:36 pm

    It will leak in IE 6 and 7 (and very old versions of FF) because it creates a circular reference between JavaScript and the DOM. IE 6 and 7 can’t garbage collect any objects which have circular references between the two worlds because they use separate garbage collectors.

    Modern browsers can handle this without leaking.

    To prevent it from leaking in IE 6 and 7, do this when you’re done with img:

    img.onload = null;
    

    If you only care about modern browsers, you don’t have to worry about it. (I’m so glad IE 6 and 7 are finally low enough in market share to suggest that!)


    Update

    The function you assign to onload creates a closure. That closure contains a reference to img. img can’t be garbage collected from the DOM’s memory as long as that closure exists in JScript’s memory (JScript is name for the IE implementation of JavaScript). Likewise, the closure can’t be garbage collected from JScript’s memory as long as img exists in the DOM’s memory, because img.onload has a reference to your function. This is a circular reference. In other words, just because drawImages executes once doesn’t mean it won’t execute again (the JScript engine doesn’t know onload only fires once — that’s the DOM’s domain), so JScript has to keep the closure alive.

    The pattern you have shown is the classic pattern that is known to create memory leaks in IE 6 & 7. It consists of (1) A DOM node, (2) An event handler on that DOM node which creates a closure, and (3) A reference back to that DOM node inside the closure.

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

Sidebar

Related Questions

Every once in a while I find myself typing a one-off script to import
Every once in a while the following scenario turns up while I code in
Every once in a while I find a real head scratcher... Any ideas what
Every once and a while I get this error in IE when making an
Im using Visual studio 2008. Every once in a while files that exist in
In this very simple code example, messages get lost every once in a while.
I have a web scraping script that gets new data once every minute, but
Suppose I want to run the following method foo() once every hour in Grails:
Every once in a while I define an empty CSS class such as .empty{}
I run across this problem every once in a while. For some reason I

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.