理解弱引用 : 弱哈希映射 « 集合 « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » 集合 » 弱哈希映射 
9. 30. 2. 理解弱引用
  1. Reference class is defined in the java.lang.ref package.
  2. Instead of providing variables that directly reference your memory, you create a reference object that indirectly holds a reference to the object.
  3. The reference objects are then maintained in a reference queue (ReferenceQueue), which monitors the references for reachability by the garbage collector.

There are four types of references to objects.

  1. Direct references like you normally use, as in Integer i = new Integer(13), are called strong references and have no special class. The remaining three are soft references (SoftReference), weak references (WeakReference), and phantom references (PhantomReference).
  2. Soft references are like a cache. When memory is low, the garbage collector can arbitrarily free up soft references if there are no strong references to the object. If you are using soft references, the garbage collector is required to free them all before throwing an OutOfMemoryException.
  3. Weak references are weaker than soft references. If the only references to an object are weak references, the garbage collector can reclaim the object's memory at any time-it doesn't have to wait until the system runs out of memory. Usually, it will be freed the next time the garbage collector runs.
  4. Phantom references are special. They allow you to be notified before the garbage collector performs finalization and frees the object. Think of it as a mechanism to perform cleanup.

(Referend from Ivor Horton's Beginning Java 2, JDK 5 Edition by Ivor Horton Wrox Press 2005)

9. 30. 弱哈希映射
9. 30. 1. WeakHashMap类
9. 30. 2. 理解弱引用
9. 30. 3. 演示WeakHashMap
9. 30. 4. 创建一个WeakHashMap
9. 30. 5. To enable automatically release of the value, the value must be wrapped in a WeakReference object
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.