Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you are describing is not a memory leak, it's a garbage that Chrome knows of and that will be removed whenever Chrome decides it's time to do it. To explain this, lets have a closer look at the scenario you have described.</p> <h2>Making memory to 'leak'</h2> <ul> <li>First lets open up a new incognito window (just to be sure that browser extensions are not affecting our results) and navigate to google.com.</li> <li>Then, lets open the Task Manager and enable "JavaScript Memory" column (by right-clicking on the Task Manager window). We need this column to be sure that the memory we will be 'leaking' is being, in fact, allocated by JavaScript. We end up with something like this:</li> </ul> <p><img src="https://i.stack.imgur.com/DDSPO.png" alt="First load"></p> <ul> <li>Now, as you suggested, we should reload the page couple of times and observe the memory of our tab going up:</li> </ul> <p><img src="https://i.stack.imgur.com/73Dcb.png" alt="After 5+ reloads"></p> <p>So far, so good - everything works exactly as you described it.</p> <h2>Wait a second...</h2> <p>However, lave your cursor inactive for half a minute, or go to another tab and you will observe a huge memory usage drop on our 'Tab:google'. Why is that? What happened there? Who cleaned up our 'leaked' memory for us?</p> <h2>The Memory Usage Drop</h2> <p>To investigate that, lets repeat what we have done so far, so that 'Tab:google' uses a lot of memory again. Then, lets open Chrome Developer Tools and start recording on the 'Timeline' tab. After that, lets change a tab for couple of seconds and when memory drops stop 'recording' on the 'Timeline'. You should end up with this:</p> <p><img src="https://i.stack.imgur.com/fIct0.png" alt="enter image description here"></p> <p>In the last couple of seconds of our recording mysterious 'GC Events' appeared. Exactly in the same time when the memory was released. Coincidence? Nope.</p> <h2>GC Events</h2> <p>GC stands for the <a href="http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29" rel="noreferrer">Garbage Collector</a>. It's a mechanism that "attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program". So it turns out that memory of our tab was polluted by garbage and GC was capable of getting rid of these garbage for the whole time (you can even force garbage collection using button at the bottom of the 'Timeline' tab). So why it decided not to? Why it waited for us to stop interacting with the page or change the tab?</p> <h2>Lazy Garbage Collector</h2> <p>The short answer is that garbage collection has to 'freeze' the execution of all scripts before any work can be done. Also, it can take significant amount of CPU time to execute. This can result in lag, choppy animations, unresponsive controls etc. That's why Chrome waits for the <em>right moment</em> to call the garbage collection. And the best moment to do it is when user is not looking.</p> <p>In addition, please note that 'GC Events' come in series, there are always couple of them with short breaks in between. These breaks are meant for 'normal' JavaScript to execute making the garbage collection less noticeable.</p> <h2>Live Objects</h2> <p>Take a look at "JavaScript Memory" tab at the top two screenshots in this post again. You will notice that this column contains two numbers. First one is memory "reserved for JavaScript VM heap", the other one is "how much memory live (reachable) objects comprise" (<a href="https://groups.google.com/forum/#!msg/google-chrome-developer-tools/aTMVGoNM0VY/bLmf3l2CpJ8J" rel="noreferrer">source</a>). When benchmarking your applications you should worry only about the second value, all the rest will be handled by GC.</p> <h2>An example of a leak</h2> <p>A real JavaScript leak can happen ie. in a web chat application. If, over time, it will use more and more 'live' memory while always displaying only last 10 messages <strong>then</strong> we can talk about a leak. Such leak, will eventually crash a tab (or a browser).</p> <h2>Conclusion</h2> <p>For scripts running on the page, reloading the page (or going to another location) is equal to restarting your computer while your ANSI C app is running. After that, you should think about all the memory allocated by your scripts as wiped out. The only reason why, in practice, this may not happen immediately after reloading the page is that browser is waiting for the right moment to clean up. And you, as a web developer, should not be concerned about it.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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