Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

一个关于setTimeout内存泄露的问题

最底下的代码在chrome运行,很容易就能发现有内存泄露
如果把let func = setTimeout(...)
abc.timer =func ,那么就不会了

求大神问一下,泄露的原理是什么,苦于不知道找什么关键词去寻答案。。。

var abc = {}
setInterval(
    () => {
        if (abc.timer) clearTimeout(abc.timer)
        abc.timer = setTimeout(
            () => {

            }, 10000
        )
    }, 10
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

没复现你说的 Bug。

我是执行你这坨代码后,在 Chrome 开发者工具里分别采集 Performance 和 Memory 快照,间隔时间 1 分钟,连续采样 5 次,内存没有上涨,且 GC 很规律、没有明显波动。手头有 70.0 和 83.0 两个版本,都试了。

你是在什么浏览器上发现的问题?又是什么版本的?


P.S. Google 上倒是找了一个类似的问题,不过是 for 循环产生的内存泄露,是 V8 GC 本身的问题,很早就修复了。

大概是这样的代码:

for (var i = 0; i < 10000000; i++) {
  var x = setTimeout(function() {}, 1);
  clearTimeout(x);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...