Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will console.log prevent garbage collection?

Tags:

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?

like image 378
Ben Aston Avatar asked Mar 03 '15 18:03

Ben Aston


People also ask

What is the purpose of console log?

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.

Does console log reduce performance?

Of course, console. log() will reduce your program's performance since it takes computational time.

Does console log work in production?

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.

Should I enable garbage collector logs by default?

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 logs in Java?

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.

Why is it important to monitor garbage collection metrics and logs?

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.

What the JVM tells us about the garbage collector’s work?

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.


1 Answers

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

like image 128
hhelwich Avatar answered Sep 27 '22 15:09

hhelwich