Does JavaScript setInterval() method cause memory leak?

前端 未结 8 1656
Happy的楠姐
Happy的楠姐 2020-11-29 22:15

Currently developing a JavaScript based animation project.

I have noticed that, proper use of setInterval(), setTimeout() and even re

相关标签:
8条回答
  • 2020-11-29 22:48

    EDIT: Yury's answer is better.


    tl;dr IMO there is no memory leak. The positive slope is simply the effect of setInterval and setTimeout. The garbage is collected, as seen by sawtooth patterns, meaning by definition there is no memory leak. (I think).

    I'm not sure there is a way to work around this so-called "memory leak." In this case, "memory leak" is referring to each call to the setInterval function increasing the memory usage, as seen by the positive slopes in the memory profiler.

    The reality is that there is no actual memory leak: the garbage collector is still able to collect the memory. Memory leak by definition "occurs when a computer program acquires memory but fails to release it back to the operating system."

    As shown by the memory profiles below, memory leak is not occurring. The memory usage is increasing with each function call. The OP expects that because this is the same function being called over and over, there should be no memory increase. However, this is not the case. Memory is consumed with each function call. Eventually, the garbage is collected, creating the sawtooth pattern.

    I've explored several ways of rearranging the intervals, and they all lead to the same sawtooth pattern (although some attempts lead to garbage collection never happening as references were retained).

    function doIt() {
        console.log("hai")
    }
    
    function a() {
        doIt();
        setTimeout(b, 50);
    }
    function b() {
        doIt();
        setTimeout(a, 50);
    }
    
    a();
    

    http://fiddle.jshell.net/QNRSK/14/

    function b() {
        var a = setInterval(function() {
            console.log("Hello");
            clearInterval(a);
            b();                
        }, 50);
    }
    b();
    

    http://fiddle.jshell.net/QNRSK/17/

    function init()
    {
        var ref = window.setInterval(function() { draw(); }, 50);
    }
    function draw()
    {
        console.log('Hello');
    }
    init();
    

    http://fiddle.jshell.net/QNRSK/20/

    function init()
    {
        window.ref = window.setInterval(function() { draw(); }, 50);
    }
    function draw()
    {
        console.log('Hello');
        clearInterval(window.ref);
        init();
    }
    init();​
    

    http://fiddle.jshell.net/QNRSK/21/

    Apparently setTimeout and setInterval are not officially parts of Javascript (hence they are not a part of v8). The implementation is left up to the implementer. I suggest you take a look at the implementation of setInterval and such in node.js

    0 讨论(0)
  • 2020-11-29 22:49

    Each time you make a function call, it creates a stack frame. Unlike lots of other languages, Javascript stores the stack frame on the heap, just like everything else. This means that every time you call a function, which you're doing every 50ms, a new stack frame is being added to the heap. This adds up and is eventually garbage collected.

    It's kinda unavoidable, given how Javascript works. The only thing that can really be done to mitigate it is make the stack frames as small as possible, which I'm sure all the implementations do.

    0 讨论(0)
提交回复
热议问题