Does JavaScript setInterval() method cause memory leak?

前端 未结 8 1669
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: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.

提交回复
热议问题