Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
FITC - Advanced JavaScript Debugging
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Memory Leak</title>
<meta name="author" content="@ramisayar">
<link href="" rel="stylesheet" integrity="sha256-MfvZlkHCEqatNoGiOXveE8FIwMzZg4W85qfrfIFBfYc= sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src=""></script>
<script src=""></script>
<div class="container">
<h1>Generate Memory Leaks</h1>
<p>By <a href="">@ramisayar</a>.</p>
<p>The following code will generate a memory leak in all browsers today. It is specific to jQuery. It is near impossible to figure out without debugging and introspection tools. Sample from <a href="">here</a>.</p>
<div id="data"></div>
<pre class="prettyprint languague-js">
function go() {
.html(new Array(100000).join('text')) // div with a text, maybe AJAX-loaded
.click(function() { })
document.getElementById('data').innerHTML = '';
<p>"The leak happens because elem is removed by cleaning parent innerHTML, but the data remains in jQuery.cache. More importantly, the event handler references elem, so both handler and elem stay in memory with the whole closure." from <a href="">JavaScript Info</a>.</p>
<button id="run_memory_link" class="btn btn-default">Run 1000 Times</button>
<script type="text/javascript" src=""></script>
<script src="" integrity="sha256-Sk3nkD6mLTMOF0EOpNtsIry+s1CsaqQC1rVLTAy+0yc= sha512-K1qjQ+NcF2TYO/eI3M6v8EiNYZfA95pQumfvcVrTHtwQVDG+aHRqLi/ETn2uB+1JqwYqVG3LIvdm9lj6imS/pQ==" crossorigin="anonymous"></script>
<script src=""></script>
<script type="text/javascript">
function go() {
for(var i = 0; i != 1000; i++){
.html(new Array(100000).join('text')) // div with a text, maybe AJAX-loaded
.click(function() { })
document.getElementById('data').innerHTML = '';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment