Why does this javascript have a memory leak in Chrome?

Viewed 669

I have a timer function that creates a large amount of data, declared with var. Why does the object not get garbage-collected? The number shown by usedJSHeapSize keeps growing. Task Manager in Chrome also shows memory increasing.

I'm testing this in Windows 10, Chrome, using VS 2017.

If I copy and paste the code into a separate file called test.html and open that in Chrome, it also shows the leak.

I've tested this code in Edge and IE (using Developer Tools instead of usedJSHeapSize) and I see no memory leak.

Is this an issue with Chrome?

    <script type="text/javascript">
        function refreshTimer() {
            try {
                var longStr = new Array(1000000).join('*');
                document.getElementById('div1').textContent = 'usedJSHeapSize: ' + window.performance.memory.usedJSHeapSize;
            }
            catch (err) {
                document.getElementById('div1').textContent = 'refreshTimer: ' + err;
            }
        }
        window.setInterval("refreshTimer()", 3*1000);
    </script>
    <div id="div1" style="font-family:Calibri"></div>

I expect that there would be no memory leak because the large data object is declared with var and should be garbage-collected when it leaves scope.

Edit to my my original post:

I have run Chrome with and without "--enable-precise-memory-info" and it makes no difference. I have observed memory growing in Chrome->More Tools->Task Manager and in Windows Task manager with just one instance of Chrome running with my test.html file. The only links I can find that mention this as a possible bug in Chrome are these:

Javascript garbage collection of typed arrays in Chrome

https://bugs.chromium.org/p/chromium/issues/detail?id=232415

These are old posts though and I can't believe the bug would live this long.

So - I'm still perplexed.

1/2/2019 - adding a comment to move this question to the top of SO. If anyone knows, please add your thoughts.

1 Answers

You've run into a security issue with Chrome. Chrome does not expose true memory usage via "window.performance.memory". Attackers could use this information to attack the web browser.

Related