nodejs setTimeout内存泄漏? [英] nodejs setTimeout memory leak?

查看:419
本文介绍了nodejs setTimeout内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

v0.10.4

这里是一个简单的循环,导致内存使用量不断增加:

Here's the simple loop that results in an ever-increasing memory usage:

function redx(){
      setTimeout(function(){ redx() },1000);
      console.log('loop');
}

redx();

我做错了什么?

好的,只是尝试了在范围内引用超时对象的建议,并且似乎垃圾收集确实在大约40秒后启动,这是TOP的缩写日志:

OK, just tried the suggestion to reference the timeout object in the scope and it seems that garbage collection does kick in after about 40 seconds, here's abbreviated logs from TOP:

3941根20 0 32944 7284 4084 S 4.587 3.406 0:01.32节点
3941根20 0 32944 7460 4084 S 2.948 3.489 0:01.59节点
3941根20 0 32944 7516 4084 S 2.948 3.515 0:01.68节点
3941根20 0 33968 8400 4112 S 2.948 3.928 0:02.15节点
3941根20 0 33968 8920 4112 S 3.275 4.171 0:02.98节点
3941根20 0 33968 8964 4112 S 2.948 4.192 0:03.07节点
3941根20 0 33968 9212 4112 S 2.953 4.308 0:03.16节点
3941根20 0 33968 9212 4112 S 2.953 4.308 0:03.25节点
3941根20 0 33968 9212 4112 S 3.276 4.308 0:03.35节点
3941根20 0 33968 9212 4112 S 2.950 4.308 0:03.44节点

3941 root 20 0 32944 7284 4084 S 4.587 3.406 0:01.32 node
3941 root 20 0 32944 7460 4084 S 2.948 3.489 0:01.59 node
3941 root 20 0 32944 7516 4084 S 2.948 3.515 0:01.68 node
3941 root 20 0 33968 8400 4112 S 2.948 3.928 0:02.15 node
3941 root 20 0 33968 8920 4112 S 3.275 4.171 0:02.98 node
3941 root 20 0 33968 8964 4112 S 2.948 4.192 0:03.07 node
3941 root 20 0 33968 9212 4112 S 2.953 4.308 0:03.16 node
3941 root 20 0 33968 9212 4112 S 2.953 4.308 0:03.25 node
3941 root 20 0 33968 9212 4112 S 3.276 4.308 0:03.35 node
3941 root 20 0 33968 9212 4112 S 2.950 4.308 0:03.44 node

推荐答案

不知道为什么,但是如果您在nodejs函数范围内引用超时对象,显然可以正确地进行垃圾回收.

No idea why but apparently if you reference the timeout object in the scope of the function nodejs will do the garbage collect that correctly.

function redx(){
      var t = setTimeout(function(){ redx() },50);
      console.log('hi');
}

redx();

这篇关于nodejs setTimeout内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆