递归setTimeout是否会导致堆栈信息内存泄漏? [英] Does a recursive setTimeout cause a stack info memory leak?
问题描述
我有以下情况:
let func = () => {
//...
let id = setTimeout(() => {
console.trace();
clearTimeout(id);
func();
}, 2000);
}
func();
尽管我读到清除超时句柄将释放用于关闭的内存和处理程序本身,但我仍然很好奇堆栈信息是否可能泄漏.我故意放下console.trace()
调用,看起来堆栈信息会无限期地增长.这不是问题吗?我知道它不是在类似递归的堆栈上,在那里我们可能会遇到堆栈大小异常,但是我仍然担心堆栈信息会不断增长.
Although I read that clearing the timeout handle will release the memory for closures and the handler itself, I am still curious if there might be a possible leak in stack information. I put the console.trace()
call on purpose and it looks like the stack information grows indefinitely. Isn't that a concern? I know that it is not on a recursive-like stack where we might get a stack size exception but I'm still concerned about the stack information that keeps growing.
推荐答案
否.当func
完成执行时,堆栈展开.
No. When func
finishes execution, the stack unwinds.
如果func是递归的,则堆栈将如下所示:
If func would be recursive, the stack would look like:
[init] -> func -> func -> func -> func -> func -> ....
对于您而言,它是:
[init] -> func -> setTimeout
<---- <----
[timer] -> func -> setTimeout
<---- <----
[timer] -> func -> setTimeout
<---- <----
...
这篇关于递归setTimeout是否会导致堆栈信息内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!