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

  • Home
  • SEARCH
  • 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 4334634
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:33:21+00:00 2026-05-21T10:33:21+00:00

I has a simple JavaScript code which is consuming memory while running infinitely. Memory

  • 0

I has a simple JavaScript code which is consuming memory while running infinitely. Memory consumption is monitored by Google Chrome internal memory profiler.

  setInterval(function(){
     var xhr = new XMLHttpRequest();
     xhr.open('GET', 'json.txt', true);
     xhr.onreadystatechange = function() {
        if(this.readyState == 4 && this.status == 200) {
           console.log(this.responseText);
        }
     };
     xhr.send('');
  }, 500);

Html file with above code sample and json.txt is hosted on on my local server, getting a file isn’t taking more than 500ms (it is always about 7-10ms).

In a long time run the memory graph is looks like that
Initial memory consumption graph

EDIT The same Chrome window after a hour of work
Graph after a hour of work

EDIT
On the long run (hours) not all memory is reclaimed an graph is still ascending.
I understand why the memory is consumed, i didn’t understand why it is not fully reclaimed.

EDIT
This is how i can reduce memory leak

  var callback = function(){
      if(this.readyState == 4 && this.status == 200) {
          console.log(this.responseText);
      }
  } 

  setInterval(function(){
     var xhr = new XMLHttpRequest();
     xhr.open('GET', 'json.txt', true);
     xhr.onreadystatechange = callback;
     xhr.send('');
  }, 500);

This improvement allowing not to link callback’s closure to xhr var.

  • 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-21T10:33:21+00:00Added an answer on May 21, 2026 at 10:33 am

    I imagine this interval isn’t being set in a vacuum. The the function you’re passing into setInterval may have access to the lexical scope it was defined in. This means every variable around it will always be reachable in the garbage collector’s eyes, even if you’re not using those variables within the function. The garbage collector will never clean it up. It might be better to define the function in a more sanitary environment where it wont have closure access to any variables large in size.

    EDIT: Sorry, I’ll try to make myself more clear. This has nothing to do with the XHR – you could put anything (or even nothing) in that setInterval function and it would still leak if there’s some variables in its lexical scope that you would want cleaned up. You just need to make sure the setInterval function doesn’t have closure access to any variables you want cleaned up. For example:

    (function() {
      var a = readFile('a-very-big-file.txt');
    
      setInterval(function() {
        console.log('im leaking!');
      }, 500);
    })();
    

    That will leak. Even though the setInterval doesn’t do anything particularly meaningful, it still has access to “a”, and the garbage collector sees it as reachable.

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

Sidebar

Related Questions

No related questions found

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.