Java "dead"对象未被垃圾收集

标签 java memory-leaks garbage-collection

我知道在Java中的垃圾收集期间,不再有任何引用的对象会被标记为“死亡”,以便垃圾收集器可以将它们从内存中删除。

我的问题是,在垃圾收集阶段,所有“死亡”对象是否都会从内存中删除,或者其中一些对象会存活下来?为什么“死”对象会在垃圾回收阶段幸存下来?

稍后编辑

感谢您的所有回答。我可以推断,“死”对象不会被删除的主要原因是垃圾收集器运行方式的时间或间隔限制。 但是,假设垃圾收集器可以到达所有“死”对象,我想知道是否有一种方法可以声明、引用、使用、取消引用等对象,以便以某种方式跳过删除阶段,即使它已经“死了”。我在想也许属于具有静态方法或内部类或类似内容的类的对象可能由于某种原因保留在内存中,即使它们没有对它们的引用。
这样的场景可能吗?

谢谢

最佳答案

My question is if, during a garbage collection phase, all of the "dead" objects get deleted from memory or some of them survive? Why would a "dead" object survive a garbage collection phase?

当前所有 HotSpot GC 都是分代收集器。引用自维基百科:

"It has been empirically observed that in many programs, the most recently created objects are also those most likely to become unreachable quickly (known as infant mortality or the generational hypothesis). A generational GC (also known as ephemeral GC) divides objects into generations and, on most cycles, will place only the objects of a subset of generations into the initial white (condemned) set. Furthermore, the runtime system maintains knowledge of when references cross generations by observing the creation and overwriting of references. When the garbage collector runs, it may be able to use this knowledge to prove that some objects in the initial white set are unreachable without having to traverse the entire reference tree. If the generational hypothesis holds, this results in much faster collection cycles while still reclaiming most unreachable objects."

对于您的问题来说,这意味着大多数 GC 周期仅收集年轻代中的垃圾对象。最老一代中的垃圾对象可以存活多个 GC 周期......直到老一代最终被收集。 (在新的 G1 GC 中,显然一次会收集一点老年代......这可能会进一步延迟回收。)

(名义上)无法访问的对象存活的其他原因包括:

  • 带有(未执行的)终结器的无法访问的对象由垃圾收集器附加到终结队列,以便在 GC 完成后进行处理。

  • 软引用、弱引用或幻像引用的对象实际上仍然可以访问,并且在 GC 完成后由各自的引用队列管理器处理。

  • 可通过 JNI 全局引用等访问的对象。 (感谢@bestss)

  • 存在与实例、其类及其类加载器相关的各种隐藏引用。

  • 存在从内部实例到其外部实例的隐藏引用。

  • 类中有一个隐藏的引用,指向代表其字符串文字的 intern String 对象。

但是,这些都是可达性定义的结果:

"A reachable object is any object that can be accessed in any potential continuing computation from any live thread." - JLS 12.6.1

还值得注意的是,GC 的规则具有保守性。他们说可达的对象不会被删除,但他们并没有说(严格)不可达的对象将被删除。这允许出现无法访问对象但运行时系统无法识别的情况。

<小时/>

您的后续问题:

However, supposing that the Garbage Collector can reach all of the "dead" objects, I was wondering if there is a way to declare, reference, use, dereference, etc.. an object such that somehow it would skip the deletion phase even though it is "dead".

“死亡”并不是一个明确定义的术语。如果垃圾收集器可以到达对象,则根据定义它们是可到达的。当它们仍然可以访问时,它们不会被删除。

如果它们都已死亡并且可访问(无论“死亡”意味着什么!),那么它们可访问这一事实意味着它们不会被删除。

你的建议没有意义。

I was thinking maybe objects belonging to classes which have static methods or inner classes or something like that may be kept in memory for some reason, even though they have no references to them. Is such a scenario possible?

静态方法没有引用...除非它们恰好位于调用堆栈上。然后,局部变量可以像任何其他方法调用一样包含引用。适用正常的可达性规则。

只要类本身存在,静态字段就是 GC 根。适用正常的可达性规则。

从 GC 的角度来看,内部类的实例与其他类的实例没有什么不同。内部类实例中可以引用外部类实例,但这会导致正常的可达性。

总而言之,可达性存在一些意想不到的“原因”,但它们都是可达性定义的逻辑结果。

关于Java "dead"对象未被垃圾收集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5349924/

相关文章:

c - 我是否发现了 libxml2 错误(多线程解析中的内存泄漏)?

php - 垃圾回收在 PHP 中是如何工作的?即,如何清理局部函数变量?

.NET 版本的 Java 断言 : Unit tesing memory leaks

java - 在Java中查找值所在的范围

memory-leaks - 使用 Twisted + Cyclone + PyPy 处理 POST 请求会导致内存泄漏?

Java套接字服务器->只接受一个请求然后停止接受?

iphone - GeneralBlock-56 在 UIWebView 中调用 loadRequest 时内存泄漏

Java:您如何真正使用 JVMTI 的 ForceGargabeCollection 强制 GC?

java - getListCellRendererComponent 被调用了多少次?

java - 使用java更改unix文件的组