LogManager and garbage collector

The description of the getLogger () LogManager method says the following:

"(...) The logger associated with the String name can be 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 handle it correctly when the Logger was garbage collected."

How is it possible that the GC can erase Logger since the LogManager must support all references to all Loggers in order to be able to resolve Loggers names?

Isn’t it so strange that the Java API allows the situation that at the beginning of my application I create and configure my Loggers, but forget to save refrences, and later, if I use Logger.getLogger ('someName') I will get a new one, by default one the installation of my Loggers that I installed?

+7
source share
1 answer

How is it possible that the GC can erase Logger since the LogManager must support all references to all Loggers in order to be able to resolve Loggers names?

Perhaps if LogManager stores weak links to registrars (which I think).

Isn’t it so strange that the Java API allows the situation that at the beginning of my application I create and configure my Loggers, but forget to save refrences, and later, if I use Logger.getLogger ('someName') I will get a new one, by default one the installation of my Loggers that I installed?

It can be argued that if you care about your logger, you should keep a link to it. The documentation is very clear:

The application must save its own link to the Logger object to avoid garbage collection. LogManager can only keep a weak link.

+7
source

All Articles