You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
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.
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.
写在最前
本次的分享一段代码来重新认识在V8的垃圾回收机制。
欢迎关注我的博客,不定期更新中——
一段来自meteor工程师提出的导致内存泄漏的代码
打印内存快照
1、首先打开一个空白的html,以防其他变量干扰。
2、将这段代码输入到命令行中,并运行一次
replaceThing()
3、打印堆快照:
4、在window/:file// window/下找到在控制台打印的变量
可以看出此时的someMethod虽然没有在函数体中留有对originalThing的引用,但事实是函数运行后someMethod被输出到了全局,其内部有着对originalThing的引用导致其不会得到释放。可想而知如果继续运行replaceThing,那么originalThing会被赋值为theThing同时其中的someMethod还保有着上一次originalThing对象引用,从而形成了一个循环引用,内部变量全都不会被释放掉从而导致内存的增长。运行多次后打印快照可看到如下结果:
从打印快照的结果不难理解之前所说的由于循环引用而导致内部变量释放不掉从而内存占用过多的事实。
So Why?
我们可以知道在这个最关键的someMethod方法中并没有对originalThing的引用!只有unused方法引用了originalThing,但unused方法又没有形成闭包。那么到底是哪里疏漏了呢?
来看下来自meteor-blog的解释:
原文出自Node.js Garbage Collection Explained
个人认为关键在于这句话:
那么个人理解为:someMethod与unused方法定义在了同一个父级作用域内也就是同一个函数内,那么这两个方法将共享相同的作用域,可见unused持有了对originalThing引用,即便它没有被调用,同时someMethod形成闭包导出到全局,这个时候someMethod会一直保持,同时由于共享了作用域则强制originalThing不被回收。故而导致最后的内存泄漏。也就是我们在上图中看到的someMethod方法在context中嵌套包含了originalThing对象,导致内存增长的结果。
最后
不定时更新中——
有问题欢迎在issues下交流。
The text was updated successfully, but these errors were encountered: