JQuery 3.1.1 memory leak on appendTo and empty

I found a memory leak in my code that looks like the following 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>
Run codeHide result

The same code that is written by innerHTML does not leak memory. The tab with this code in Chrome 57.0.2987.133 (64-bit) has grown from 37 to 161 MB. The problem exists if we use any of these methods in our code:

  • .html ();

  • .append ();

  • .appendTo ();

  • .prependTo ();

  • .prepend ();

I am not sure about .remove () and .empty (); I could not find a solution to this problem. All the posts I found are too old. Here is the result of my test: enter image description here

+4
source share
1 answer

I have this mem leak in Chrome 57, but in 58 everything is fine!

  • OS: Linux mint 17 x64
  • Chrome: 58.0.3029.81 (64-bit)

memory leak test

+1
source

Source: https://habr.com/ru/post/1675779/


All Articles