gpt4 book ai didi

java - 日志管理器和垃圾收集器

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:23:18 25 4
gpt4 key购买 nike

在LogManager的getLogger()方法的描述中,是这样写的:

"(...)如果没有对 Logger 的强引用,与 String 名称关联的 Logger 可能随时被垃圾回收。此方法的调用者必须检查返回值是否为 null,以便正确处理记录器已被垃圾收集的情况。”

GC 怎么可能删除 Logger,因为 LogManager 必须保留对所有 Logger 的所有引用才能解析 Logger 的名称?

这不是有点奇怪吗,Java API 允许这样一种情况,即在我的应用程序开始时我创建并设置了我的记录器但忘记保留引用,然后如果我使用 Logger.getLogger( 'someName' ) 我将获得我设置的新的、默认的 Loggers 吗?

最佳答案

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?

如果 LogManager 保持 weak references 是可能的记录器(我相信它确实如此)。

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?

有人可能会争辩说,如果您关心记录器,就应该保留对它的引用。 documentation对此非常清楚:

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.

关于java - 日志管理器和垃圾收集器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13977860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com