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

推荐订阅源

Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
小众软件
小众软件
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
博客园_首页
T
Tailwind CSS Blog
The Cloudflare Blog
J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
腾讯CDC
P
Proofpoint News Feed
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements
T
The Blog of Author Tim Ferriss
D
Docker
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 马森

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