Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - 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?

Edit: I dont believe it will affect eligibility for collection based on watching the heap in Chrome dev tools. But will any category of writing to the console do so?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

If you log an object to the console it can not be garbage collected.

You can verify this by entering in the chrome console:

var Foo = function() {};
console.log(new Foo());

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“.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...