Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LogManager and Garbage Collector

In the description of the getLogger() method of the LogManager, it is written the following:

"(...)Logger associated with the String name may be garbage collected at any time if there is no strong reference to the Logger. The caller of this method must check the return value for null in order to properly handle the case where the Logger has been garbage collected."

How is it possible, that the GC can erase the Logger, since LogManager has to keep all the references to all Loggers to be able to resolve the names of the Loggers?

Isn't that a bit strange, that Java API allows a situation, that at the start of my app I create and setup my Loggers but forget to keep refrences, and then later if I use Logger.getLogger( 'someName' ) I will get new, default one instad of my Loggers which I set up?

like image 947
Protechnologia.pl Avatar asked Dec 20 '12 17:12

Protechnologia.pl


1 Answers

How is it possible, that the GC can erase the Logger, since LogManager has to keep all the references to all Loggers to be able to resolve the names of the Loggers?

It's possible if LogManager keeps weak references to the loggers (which I believe it does).

Isn't that a bit strange, that Java API allows a situation, that at the start of my app I create and setup my Loggers but forget to keep refrences, and then later if I use Logger.getLogger( 'someName' ) I will get new, default one instad of my Loggers which I set up?

One could argue that, if you care about your logger, you should keep a reference to it. The documentation is very clear on that:

The application should retain its own reference to the Logger object to avoid it being garbage collected. The LogManager may only retain a weak reference.

like image 69
NPE Avatar answered Oct 23 '22 21:10

NPE