JVM-是否在次要GC中收集了WeakReferences? [英] JVM - Are WeakReferences collected in minor GC?

查看:75
本文介绍了JVM-是否在次要GC中收集了WeakReferences?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此很纳闷,因为这会使它们的使用率降低.如果是这样,是否有一种方法可以使内存在主要GC上仅弱引用垃圾"?

解决方案

javadoc并未具体说明清除/破坏WeakReference的时间表".这将回答您的问题(至少在理论上)它取决于实现".实际上,JLS规范和javadocs甚至没有提到主要集合与次要集合.整个主题位于实施细节"类别中.

如果您确实想要对GC敏感的引用,那么也许应该使用

在其他地方,软引用被描述为强于弱引用.这意味着它不太可能被破坏.例如一个急切的垃圾收集器.但是请注意,这些都没有谈论主要垃圾收集和次要垃圾收集.


更新我在 https://stackoverflow.com/中研究了以下(非常合理!)声明. Java 11源代码树中的a/16977182/139985 :

次要收藏将收集年轻空间中的任何对象.年轻空间中对象的WeakReference将在次要GC上收集.

本地参考处理代码很复杂.有一个通用的ReferenceProcessor类可以执行以下操作:

  • 它有选择地记录GC遇到的Reference个对象. GC代码调用ReferenceProcessor::discover_reference来实现这一目标.
  • 迭代发现的Reference对象,以确定是否破坏引用.相关的Reference对象将添加到它们各自的参考队列中.

并发症如下:

  • GC可以调用或可以不调用ReferenceProcessor::discover_reference.据我所知,大多数(如果不是全部)GC确实会调用它,但是很难确定.

  • ReferenceProcessor具有不同的策略,用于处理引用对象和引用对象处于不同世代(或跨度)的情况. (如果不处理引用,则该引用将被视为当前集合中难以访问的对象.)

简而言之,我可以确认Reference对象将通常在次要GC中进行处理.但是,特定Reference 的实际行为可能取决于生成/跨度问题.

(应该可以从GC日志中观察特定GC的一般行为.查找参考处理阶段的统计信息或时间安排.)


1-在注释中使用"span"一词.我认为这与将旧一代划分为多个单独收集的区域(跨度)的收集器(例如G1)有关.

I was wondering about that since that would make them less useful. If so, is there a way to make memory weakly referenced only "garbage" on major GC?

解决方案

The javadoc does not specifically state what the "timescales" are for clearing / breaking WeakReferences. That would make the answer to your question (at least in theory) "it is implementation dependent". Indeed, the JLS spec and javadocs don't even mention major versus minor collections. The whole topic comes is in the "implementation details" category.

If you do want references that are GC sensitive, then maybe you should use a SoftReference instead. That is described as follows:

"All soft references to softly-reachable objects are guaranteed to have been cleared before the virtual machine throws an OutOfMemoryError. Otherwise no constraints are placed upon the time at which a soft reference will be cleared or the order in which a set of such references to different objects will be cleared. Virtual machine implementations are, however, encouraged to bias against clearing recently-created or recently-used soft references."

Elsewhere, a soft reference is described as stronger than a weak reference. The implication is that it is less likely to be broken; e.g. by an over-eager garbage collector. But note that none of this talks about major vs minor garbage collections.


UPDATE I researched the following (highly plausible!) claim in https://stackoverflow.com/a/16977182/139985 in the Java 11 source tree:

Minor collections will collect any object in the young space. A WeakReference to an object in the young space will be collected on a minor GC.

The native Reference handling code is complicated. There is a general ReferenceProcessor class that does the following:

  • It selectively records Reference objects that a GC encounters. The GC code calls ReferenceProcessor::discover_reference to make this happen.
  • It iterates the discovered Reference objects to determine whether or not to break the references. The relevant Reference objects are added to their respective reference queues.

The complications are as follows:

  • A GC may or may not call ReferenceProcessor::discover_reference. From what I can tell, most (if not all) GCs do call it, but it is hard to be sure.

  • The ReferenceProcessor has different policies for dealing with the case where the reference and referent are in different generations (or spans). (If a reference is not going to processed, the referent will be treated as hard reachable for the current collection.)

In short, I can confirm that Reference objects will typically be processed in a minor GC. However, the actual behavior a particular Reference may depend on generation / span issues.

(It should be possible to observe the general behavior for a particular GC from the GC logs. Look for the stats or timings for reference processing phases.)


1 - The "span" term is used in comments. I think it relates to collectors (e.g. G1) that break the old generation into a number of regions (spans) that are collected separately.

这篇关于JVM-是否在次要GC中收集了WeakReferences?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆