Understanding Weak References : WeakHashMap « Collections « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Collections » WeakHashMap 
9. 31. 2. Understanding Weak References
  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. 31. WeakHashMap
9. 31. 1. WeakHashMap Class
9. 31. 2. Understanding Weak References
9. 31. 3. Demonstrating the WeakHashMap
9. 31. 4. Create a WeakHashMap with a single element in it
9. 31. 5. To enable automatically release of the value, the value must be wrapped in a WeakReference object
9. 31. 6. Implements a combination of WeakHashMap and IdentityHashMap
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.