惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
量子位
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
爱范儿
爱范儿
博客园 - 【当耐特】
Vercel News
Vercel News
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
F
Fortinet All Blogs
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
博客园 - 叶小钗
博客园_首页
V
Visual Studio Blog
宝玉的分享
宝玉的分享
B
Blog
MyScale Blog
MyScale Blog
C
Check Point Blog
博客园 - 三生石上(FineUI控件)
L
LangChain Blog
V
V2EX

博客园 - 马森

Java IO测试样例-字节流-字符流 java中的io系统详解 MongoDB资料汇总 字符编码笔记:ASCII,Unicode和UTF-8 jQuery和ExtJS的timeOut超时设置和event事件处理 Sybase字符串函数-数学函数-系统函数 inux 设置系统时间和硬件时间 Sybase采用固定表+存储过程实现分页 Wordpress XML-RPC协议说明 struts2 action 配置方法 &&struts2的配置文件 理解 SET CHAINED command not allowed within multi-statement transaction. sybase性能优化 - 马森 - 博客园 [整理]centos下配置和安装 jstl字符串处理 [原]动态打jar包程序,可用于手机图片音乐游戏的动态打包 Java,Tomcat,Mysql乱码总结 [原]output parameters have not yet been processed源自返回了多个数据集 [转]MS SQL Server数据库事务锁机制分析 tomcat支持ssl时Keystore was tampered with, or password was incorrect
Java引用对象SoftReference WeakReference PhantomReference...
马森 · 2011-01-30 · via 博客园 - 马森

四、Java对引用的分类

级别

什么时候被垃圾回收

用途

生存时间

从来不会

对象的一般状态

JVM停止运行时终止

在内存不足时

对象简单?缓存

内存不足时终止

在垃圾回收时

对象缓存

gc运行后终止

假象

Unknown

Unknown

Unknown

1、强引用:

public static void main(String[] args) {

        MyDate date = new MyDate();

        System.gc();

}

解释:即使显式调用了垃圾回收,但是用于date是强引用,date没有被回收

2、软引用:

public static void main(String[] args) {

        SoftReference ref = new SoftReference(new MyDate());

        drainMemory(); // 让软引用工作

}

解释:在内存不足时,软引用被终止,等同于:

MyDate date = new MyDate();

//-------------------JVM决定运行-----------------

If(JVM.内存不足()) {

         date = null;

         System.gc();

}

//-------------------------------------------------------------

3、弱引用:

public static void main(String[] args) {

        WeakReference ref = new WeakReference(new MyDate());

        System.gc(); // 让弱引用工作

}

解释:在JVM垃圾回收运行时,弱引用被终止,等同于:

MyDate date = new MyDate();

//------------------垃圾回收运行------------------

public void WeakSystem.gc() {

         date = null;

         System.gc();

}

4、假象引用:

public static void main(String[] args) {

        ReferenceQueue queue = new ReferenceQueue();

        PhantomReference ref = new PhantomReference(new MyDate(), queue);

        System.gc(); // 让假象引用工作

}

解释:假象引用,在实例化后,就被终止了,等同于:

MyDate date = new MyDate();

date = null;

//-------终止点,在实例化后,不是在gc时,也不是在内存不足时--------

http://hi.baidu.com/mynetbeans/blog/item/d72208fa8d63ac1ba9d31160.html