Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I create a memory leak in JavaScript?
    text
    copied!<p>I would like to understand what kind of code causes memory leaks in JavaScript and created the script below. However, when I run the script in Safari 6.0.4 on OS X the memory consumption shown in the Activity Monitor does not really increase.</p> <p>Is something wrong with my script or is this no longer an issue with modern browsers?</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;/body&gt; &lt;script&gt; var i, el; function attachAlert(element) { element.onclick = function() { alert(element.innerHTML); }; } for (i = 0; i &lt; 1000000; i++) { el = document.createElement('div'); el.innerHTML = i; attachAlert(el); } &lt;/script&gt; &lt;/html&gt; </code></pre> <p>The script is based on the Closure section of Google's JavaScript style guide: <a href="http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml?showone=Closures#Closures">http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml?showone=Closures#Closures</a></p> <p>EDIT: The bug that caused the above code to leak has apparently been fixed: <a href="http://jibbering.com/faq/notes/closures/#clMem">http://jibbering.com/faq/notes/closures/#clMem</a></p> <p>But my question remains: Would someone be able to provide a realistic example of JavaScript code that leaks memory in modern browsers?</p> <p>There are many articles on the Internet that suggest memory leaks can be an issue for complex single page applications but I have a hard time finding an examples that I can run in my browser.</p>
 

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