Reputation: 624
I found a memory leak in my code that looks like next snippet
function random() {
return Math.floor(Math.random() * 1000);
}
var _target = $('#target');
function add() {
_target.empty();
for (var i = 0; i < 100; i++) {
_target.append('<tr><td>'+random()+'</td><td>'+random()+'</td><td>'+random()+'</td></tr>')
}
}
var addInt = setInterval(add, 500);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Memory leak test: jquery</title>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<table id="target"></table>
</body>
</html>
The same code that is written by innerHTML doesn't give memory leak. A tab with this code in Chrome 57.0.2987.133 (64-bit) has grown from 37 Mb to 161 Mb. The problem exists if we use any of these methods in our code :
.html();
.append();
.appendTo();
.prependTo();
.prepend();
I'm not sure about .remove() and .empty(); I couldn't find solution of this problem. All the posts that I've found are too old. Here is result of my test :
Upvotes: 4
Views: 380
Reputation: 3935
I've this mem leak in Chrome 57, but in 58 all is fine!
Upvotes: 1