下面接着上一篇介绍第2阶段和第3阶段的处理逻辑。

2、process_phase2()

第2个阶段移除所有的referent还存活的Reference,也就是从refs_list中移除Reference。process_phase2()方法的实现如下:

// Phase2: remove all those references whose referents are reachable.
inline void process_phase2(DiscoveredList& refs_list,
BoolObjectClosure* is_alive,
OopClosure* keep_alive,
VoidClosure* complete_gc) {
// complete_gc is ignored in this case for this phase
pp2_work(refs_list, is_alive, keep_alive);
// ...
}

在第2阶段移除所有的对象可达的Reference,因为不需要进行垃圾回收。

// Traverse the list and remove any Refs that are not active, or
// whose referents are either alive or NULL.
void ReferenceProcessor::pp2_work(
DiscoveredList& refs_list,
BoolObjectClosure* is_alive,
OopClosure* keep_alive
) {
DiscoveredListIterator iter(refs_list, keep_alive, is_alive);
while (iter.has_next()) {
iter.load_ptrs(); if (iter.is_referent_alive()) { // The referent is reachable after all.
// Remove Reference object from list.
iter.remove();
// Update the referent pointer as necessary: Note that this
// should not entail any recursive marking because the
// referent must already have been traversed.
iter.make_referent_alive();
iter.move_to_next();
} else {
iter.next();
}
}
}

调用iter.is_referent_alive()方法判断,如果referent还存活,那么从refs_list中删除对应的Reference对象即可。这个操作对于所有的引用类型(软引用、弱引用、幻像引用和最终引用)都一样。

3、process_phase3()

第3阶段在处理referent时,这个referent可以确保是存活状态,不过还需要做一些其它操作。process_phase3()方法的实现如下:

// Traverse the list and process the referents, by either
// clearing them or keeping them (and their reachable
// closure) alive.
void ReferenceProcessor::process_phase3(
DiscoveredList& refs_list,
bool clear_referent,
BoolObjectClosure* is_alive,
OopClosure* keep_alive,
VoidClosure* complete_gc
) {
ResourceMark rm;
DiscoveredListIterator iter(refs_list, keep_alive, is_alive);
while (iter.has_next()) {
iter.update_discovered();
iter.load_ptrs(DEBUG_ONLY(false /* allow_null_referent */));
if (clear_referent) {
// NULL out referent pointer
// 将Reference的referent字段置为null,之后会被GC回收
iter.clear_referent();
} else {
// keep the referent around
// 标记引用的对象为存活,该对象在这次GC将不会被回收
iter.make_referent_alive();
} iter.next();
}
// Remember to update the next pointer of the last ref.
iter.update_discovered();
// Close the reachable set
complete_gc->do_void();
}

对于软引用和弱引用来说,参数clear_referent的值为true,也就是当对象不可达后,引用字段就会被置为null,然后对象就会被回收(对于软引用来说,如果内存足够的话,在Phase 1,相关的引用就会从_refs_list中移除);对于最终引用和幻像引用来说, 参数clear_referent的值为false,也就意味着被这两种引用类型引用的对象,如果没有其他额外处理,只要Reference对象还存活,那引用的对象是不会被回收的。  

 

相关文章的链接如下:

1、在Ubuntu 16.04上编译OpenJDK8的源代码

2、调试HotSpot源代码

3、HotSpot项目结构 

4、HotSpot的启动过程

5、HotSpot二分模型(1)

6、HotSpot的类模型(2)

7、HotSpot的类模型(3)

8、HotSpot的类模型(4)

9、HotSpot的对象模型(5)

10、HotSpot的对象模型(6)

11、操作句柄Handle(7)

12、句柄Handle的释放(8)

13、类加载器

14、类的双亲委派机制

15、核心类的预装载

16、Java主类的装载

17、触发类的装载

18、类文件介绍

19、文件流

20、解析Class文件

21、常量池解析(1)

22、常量池解析(2)

23、字段解析(1)

24、字段解析之伪共享(2)

25、字段解析(3)

26、字段解析之OopMapBlock(4)

27、方法解析之Method与ConstMethod介绍

28、方法解析

29、klassVtable与klassItable类的介绍

30、计算vtable的大小

31、计算itable的大小

32、解析Class文件之创建InstanceKlass对象

33、字段解析之字段注入

34、类的连接

35、类的连接之验证

36、类的连接之重写(1)

37、类的连接之重写(2)

38、方法的连接

39、初始化vtable

40、初始化itable

41、类的初始化

42、对象的创建

43、Java引用类型

44、Java引用类型之软引用(1)

45、Java引用类型之软引用(2)

46、Java引用类型之弱引用与幻像引用

作者持续维护的个人博客  classloading.com

关注公众号,有HotSpot源码剖析系列文章!

   

参考文章:

(1)你不可不知的Java引用类型之——SoftReference源码详解

(2)深入理解JDK中的Reference原理和源码实现

  

最新文章

  1. IOS FMDB 获取数据库表和表中的数据
  2. jquery——左右按钮点击切换一组图片功能
  3. Ubuntu下su被拒绝
  4. sparksql udf的运用----scala及python版(2016年7月17日前完成)
  5. Java反射机制专题
  6. 关于git托管的一些心得
  7. html,body最顶层元素.
  8. android的布局管理器
  9. asp.net 通过ajax方式调用webmethod方法使用自定义类传参及获取返回参数
  10. free() 是如何释放不同内存区块大小的指针?
  11. 网站发布在另外一个网站下面配置伪静态之后图片样式和JS丢失
  12. poj 1204
  13. 设计模式之十:观察者模式(Observer)
  14. 很好的容斥思想 HDU 5514
  15. centos6.7搭建DHCP服务器
  16. 二十五、Hadoop学记笔记————Hive复习与深入
  17. Unable to start Ocelot because either a ReRoute or GlobalConfiguration
  18. 基于 LWIP 建立 TCP Server 与主机通信实验
  19. Quartz动态添加定时任务执行sql(服务启动添加+手动添加)
  20. D: Starry的神奇魔法(矩阵快速幂)

热门文章

  1. Apache Tomcat目录结构与版本升级
  2. 题解 CF938G 【Shortest Path Queries】
  3. 题解 CF13E 【Holes】
  4. HTTP request smuggling CL.TE
  5. MySQL 8.0.20 安装教程图文详解(windows 64位)
  6. MongoDB副本集部署
  7. win10在html上运行java的applet程序
  8. PHP date_create_from_format() 函数
  9. PHP is_dir() 函数
  10. PHP ord() 函数