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?
The console. log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user.
Of course, console. log() will reduce your program's performance since it takes computational time.
Now you are rest assured your console. log(…) will work properly on your local or staging server but will not output anything on your production server. Comments, suggestions or corrections are welcome.
Before talking about how to enable garbage collector logging we should ask ourselves one thing. Should I turn on the logs by default or I should only turn them on when issues start appearing? On modern devices, you shouldn’t worry about performance when enabling the garbage collector logs.
What Are Garbage Collection (GC) Logs The garbage collector log is a text file produced by the Java Virtual Machine that describes the work of the garbage collector. It contains all the information you could need to see how the memory cleaning process works. It also shows how the garbage collector behaves and how much resources it uses.
That’s why having a proper tool that helps you visualize and understand the metrics and logs produced by the garbage collector is crucial. This is one of the reasons we built Sematext Logs and Sematext JVM Monitoring – for ease of visualization and understanding of such data.
By using logs we can understand what the JVM tells us about the garbage collectors’ work. The garbage collector log is a text file produced by the Java Virtual Machine that describes the work of the garbage collector. It contains all the information you could need to see how the memory cleaning process works.
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“.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With