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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:19:44+00:00 2026-05-26T20:19:44+00:00

I have researched quite a bit about this but mostly by piecing other questions

  • 0

I have researched quite a bit about this but mostly by piecing other questions together, which still leaves some doubt. In an app that does not refresh the browser page at any time and may live for quite a while (hours) without closing (assuming that refreshing a page or navigating to another would restart the js code), what’s the best way to ensure objects are released and that there’s no memory leak.

These are the specific scenarios I’m concerned about:

All of the code below is within a revealing module pattern.

mycode = function(){}()

variables within functions, I’m sure this one is collected by the GC just fine

function(){ var h = "ss";}

variables within the module, should g = null when it’s no longer needed?

var g;
function(){ g = "dd";}

And lastly the life of a jqXHR: is it cleaned up after it returns? Should it be set to null in all cases as a precaution whether kept inside a function or module?

If doing this, is it x cleaned up by the GC after it returns?:

function(){
   var x = $.get();
   x.done = ...;
   x.fail = ...;
}

How about when doing this, will it also be cleaned up after x returns?:

var x;
function(){
   x = $.get();
   x.done = ...;
   x.fail = ...;
}

Lastly, is there a way to cleanup all variables and restart a module without restarting the browser?

  • 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-05-26T20:19:44+00:00Added an answer on May 26, 2026 at 8:19 pm

    variables within functions, I’m sure this one is collected by the GC just fine

    Yes.

    variables within the module, should g = null when it’s no longer needed?

    Sure.

    And lastly the life of a jqXHR: is it cleaned up after it returns? Should it be set to null in all cases as a precaution whether kept inside a function or module?

    Various browsers have had bugs related to XHR that caused the onreadystatechange and anything it closed over to remain uncollectable unless the dev was careful to replace it with a dummy value (xhr.onreadystatechange = new Function('')) but I believe jQuery handles this for you.

    Lastly, is there a way to cleanup all variables and restart a module without restarting the browser?

    Global state associated with the page will take up browser memory until the page is evicted from the browser history stack. location.replace can help you here by letting you kill the current page and replace it with a new version of the same app without expanding the history stack.

    Replace the current document with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session history, meaning the user won’t be able to use the Back button to navigate to it.

    When you use the word “module”, that is not a term that has a well-defined meaning to the browser or its JavaScript interpreter so there is no way to evict a module and only a module from memory. There are several things that you have to worry about that might keep things in memory:

    1. References to JavaScript objects that have been attached to DOM nodes and everything they close over — event handlers are a very common example.
    2. Live setInterval and setTimeout callbacks and everything they close over.
    3. Properties of the global object and everything they close over.
    4. As you noted, properties of certain host objects like XHR instances, web worker callbacks, etc. and (you guessed it) everything they close over.

    Any scheme that is going to unload a module and only a module would need to deal with all of these and figure out which of them are part of the module and which are not. That’s a lot of different kinds of cleanup.

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

Sidebar

Related Questions

I have done quite a bit of research on this but I'm still having
Ok, I've used locks quite a bit, but I've never had this scenario before.
I have done quite a bit of research on this issue and have not
I researched quite a bit on onClick changes for links but couldn't quite figure
I have done quite a bit of research, but the information is a bit
This should be quite easy, and I have done some research on this. I
I have researched this for a few hours and I am kind of frustrated.
I have researched enough on this topic sans any luck :-( My requirement is
I have researched a lot about creating my own app for android with a
I researched over the internet, but could not find anything... I have a mysql

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.