Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leak with KnockoutJS foreach binding

I run into an issue when running my KnockoutJS v3.4.2 (test) application in Google Chrome.
The memory usage of my page keeps increasing.

The test code is a very simple piece of code, that changes the items in an observable array every second:

HTML:

<html>
    <head>
        <title>KnockoutJS</title>
    </head>
    <body>
        <h1>Foreach test</h1>
        <ul id="ul-numbers" data-bind="foreach: { data: listOfItems }">
            <li>
                <span data-bind="text: $data"></span>
            </li>
        </ul>

        <script type="text/javascript" src="./lib/knockout.js"></script>
        <script type="text/javascript" src="./index.js"></script> 
    </body>
</html>

JavaScript:

var vm = {
    listOfItems: ko.observableArray()
};

window.setInterval(function updateList(){
    var array = [];

    for(var i = 0 ; i < 1000; i++){
        var num = Math.floor( Math.random() * 500);
        array.push(num);
    }

    vm.listOfItems(array);
}, 1000);

ko.applyBindings(vm);

Memory usage:

  • In Firefox the memory usage doesn't increase:
    start: 459.6 MB ---> After +- 1 hour: 279.4 MB

  • In chrome the memory usage keeps increasing (memory of individual tab):
    start: 52.912 MB ---> After +- 1 hour: 566.120 MB

  • In edge the memory usage also keeps increasing (memory of individual tab):
    start: 109.560 MB ---> After +- 1 hour: 385.820 MB

Am I doing something wrong in this code snippet? Or would this be a bug in Google Chrome or KnockoutJS?

like image 278
Alexander Cosman Avatar asked Apr 06 '17 07:04

Alexander Cosman


1 Answers

Apparently this was a browser issue.

When I now run my test project the memory doesn't increase.
The test project can be found here: https://github.com/knockout/knockout/issues/2223

Solved in Google chrome version '58.0.3029.110'.

like image 120
Alexander Cosman Avatar answered Oct 02 '22 16:10

Alexander Cosman