Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy this code is consuming memory and it is not get fully reclaimed?
    primarykey
    data
    text
    <p>I has a simple JavaScript code which is consuming memory while running infinitely. Memory consumption is monitored by Google Chrome internal memory profiler.</p> <pre><code> setInterval(function(){ var xhr = new XMLHttpRequest(); xhr.open('GET', 'json.txt', true); xhr.onreadystatechange = function() { if(this.readyState == 4 &amp;&amp; this.status == 200) { console.log(this.responseText); } }; xhr.send(''); }, 500); </code></pre> <p>Html file with above code sample and <code>json.txt</code> is hosted on on my local server, getting a file isn't taking more than 500ms (it is always about 7-10ms).</p> <p>In a long time run the memory graph is looks like that <img src="https://i.stack.imgur.com/l7O7w.png" alt="Initial memory consumption graph"></p> <p><strong>EDIT</strong> The same Chrome window after a hour of work <img src="https://i.stack.imgur.com/tp50g.png" alt="Graph after a hour of work"></p> <p><strong>EDIT</strong> On the long run (hours) not all memory is reclaimed an graph is still ascending. I understand <strong>why</strong> the memory is consumed, i didn't understand why it is <strong>not fully reclaimed</strong>.</p> <p><strong>EDIT</strong> This is how i can reduce memory leak</p> <pre><code> var callback = function(){ if(this.readyState == 4 &amp;&amp; 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); </code></pre> <p>This improvement allowing not to link callback's closure to <code>xhr</code> var. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload