console.log会阻止垃圾回收吗? [英] Will console.log prevent garbage collection?
问题描述
如果我有一个通常会被垃圾收集但已被记录到控制台的对象,它是否仍有资格进行垃圾回收?
If I have an object that would normally be garbage collected, but has been logged to the console, will it still be eligible for garbage collection?
(function(){
var o = { foo: {} };
console.log(o);
}())
// Can o be collected?
如果是,是否有任何情况下写入控制台(使用其任何方法)都会影响是否有资格收集垃圾?
If yes, are there any circumstances where writing to the console (using any of its methods) can affect eligibility for garbage collection?
编辑:我不相信它会影响基于在Chrome开发工具中查看堆的收集资格。但是,控制台的任何类型的写入都会这样做吗?
推荐答案
如果您将一个对象记录到控制台它无法进行垃圾回收。
If you log an object to the console it can not be garbage collected.
您可以通过输入Chrome控制台来验证这一点:
You can verify this by entering in the chrome console:
var Foo = function() {};
console.log(new Foo());
转到个人资料和抓取快照。这将自动进行垃圾收集。搜索课程Foo。 对象计数列中将有 1
。
Go to "Profiles" and "Take Heap Snapshot". This will do a garbage collection automatically. Search for class "Foo". There will be a 1
in column "Objects count".
这篇关于console.log会阻止垃圾回收吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!