gpt4 book ai didi

java - GC 什么时候删除只有弱引用的对象?

转载 作者:搜寻专家 更新时间:2023-11-01 03:19:56 25 4
gpt4 key购买 nike

我有一个“有历史记录”的 Java 应用程序,它使用 Wea​​kReferences 进行缓存。我制作了几个 heapdump,发现它们都包含很多具有弱引用的对象(堆大小的 10%-15%,~1.2GB)。

  • 这是否意味着弱引用会对 JVM 产生内存压力?
  • 并强制执行具有停止世界暂停的 FullGC?

附言我知道 WeakReference 会对 CMS 的 GC 产生性能损失,因为它会使算法更难。但是有人看到过有关它的论文或一些官方信息吗?我只找到这个 SO post .

最佳答案

Object Computing, Inc. 就此做了一个演示,我过去发现它很有用。以下是摘录:

Kinds of Object References

  • Strong references
  • SoftReference
  • GC’ed any time after there are no strong references to the referent, but is typically retained until memory is low
  • can be used to implement caches of objects that can be recreated if needed

WeakRefernence

  • GC’ed any time after there are no strong or soft references to the referent
  • often used for “canonical mappings” where each object has a unique identifier (one-to-one), and in collections of “listeners”

"For soft and weak references, the get returns null method when the referent object has been GC’ed."

来源:http://java.ociweb.com/mark/other-presentations/JavaGC.pdf

这似乎表明 SoftReference 是缓存对象的首选。

在实践中,我使用了 Guava 缓存 API 并让它管理细节: https://github.com/google/guava/wiki/CachesExplained

关于java - GC 什么时候删除只有弱引用的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33678319/

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