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

推荐订阅源

S
Schneier on Security
The Register - Security
The Register - Security
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The GitHub Blog
The GitHub Blog
博客园 - 司徒正美
罗磊的独立博客
U
Unit 42
S
SegmentFault 最新的问题
Y
Y Combinator Blog
博客园_首页
Hugging Face - Blog
Hugging Face - Blog
J
Java Code Geeks
Schneier on Security
Schneier on Security
Know Your Adversary
Know Your Adversary
C
Check Point Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
V
Vulnerabilities – Threatpost
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
The Hacker News
The Hacker News
博客园 - 叶小钗
C
Cybersecurity and Infrastructure Security Agency CISA
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Latest news
Latest news
L
Lohrmann on Cybersecurity
A
About on SuperTechFans
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
S
Securelist
A
Arctic Wolf
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Threatpost
Scott Helme
Scott Helme
博客园 - 聂微东
博客园 - 【当耐特】
T
Tenable Blog
I
Intezer
D
DataBreaches.Net
B
Blog RSS Feed
Security Latest
Security Latest
C
Cisco Blogs
T
Tor Project blog
N
Netflix TechBlog - Medium

博客园 - 马森

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