Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V8中一个微妙的内存泄露方式 #17

Open
Aaaaaaaty opened this issue Aug 31, 2017 · 2 comments
Open

V8中一个微妙的内存泄露方式 #17

Aaaaaaaty opened this issue Aug 31, 2017 · 2 comments
Labels

Comments

@Aaaaaaaty
Copy link
Owner

Aaaaaaaty commented Aug 31, 2017

写在最前

本次的分享一段代码来重新认识在V8的垃圾回收机制。
欢迎关注我的博客,不定期更新中——

一段来自meteor工程师提出的导致内存泄漏的代码

var theThing = null  
var replaceThing = function () {
  var originalThing = theThing
  var unused = function () {
    if (originalThing)
      console.log("hi")
  }
  theThing = {
    longStr: new Array(1000000).join('*'),
    someMethod: function () {
      console.log(someMessage)
    }
  };
};
// setInterval(replaceThing, 1000)

打印内存快照

1、首先打开一个空白的html,以防其他变量干扰。

2、将这段代码输入到命令行中,并运行一次replaceThing()

3、打印堆快照:

image

4、在window/:file// window/下找到在控制台打印的变量

image

可以看出此时的someMethod虽然没有在函数体中留有对originalThing的引用,但事实是函数运行后someMethod被输出到了全局,其内部有着对originalThing的引用导致其不会得到释放。可想而知如果继续运行replaceThing,那么originalThing会被赋值为theThing同时其中的someMethod还保有着上一次originalThing对象引用,从而形成了一个循环引用,内部变量全都不会被释放掉从而导致内存的增长。运行多次后打印快照可看到如下结果:

从打印快照的结果不难理解之前所说的由于循环引用而导致内部变量释放不掉从而内存占用过多的事实。

So Why?

我们可以知道在这个最关键的someMethod方法中并没有对originalThing的引用!只有unused方法引用了originalThing,但unused方法又没有形成闭包。那么到底是哪里疏漏了呢?

来看下来自meteor-blog的解释:

Well, the typical way that closures are implemented is that every function object has a link to a dictionary-style object representing its lexical scope. If both functions defined inside replaceThing actually used originalThing, it would be important that they both get the same object, even if originalThing gets assigned to over and over, so both functions share the same lexical environment. Now, Chrome’s V8 JavaScript engine is apparently smart enough to keep variables out of the lexical environment if they aren’t used by any closures - from the Meteor blog.

原文出自Node.js Garbage Collection Explained

个人认为关键在于这句话:

If both functions defined inside replaceThing actually used originalThing, it would be important that they both get the same object, even if originalThing gets assigned to over and over, so both functions share the same lexical environment.

那么个人理解为:someMethod与unused方法定义在了同一个父级作用域内也就是同一个函数内,那么这两个方法将共享相同的作用域,可见unused持有了对originalThing引用,即便它没有被调用,同时someMethod形成闭包导出到全局,这个时候someMethod会一直保持,同时由于共享了作用域则强制originalThing不被回收。故而导致最后的内存泄漏。也就是我们在上图中看到的someMethod方法在context中嵌套包含了originalThing对象,导致内存增长的结果。

最后

不定时更新中——
有问题欢迎在issues下交流。

@Aaaaaaaty Aaaaaaaty added the Node label Aug 31, 2017
@paladin-t
Copy link

为什么最近都是求 star 作为 github 内容的收尾,star 那么重要吗

@Aaaaaaaty
Copy link
Owner Author

Aaaaaaaty commented Sep 1, 2017

@paladin-t 没那么重要 只是得到了比较开心

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants