United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-7166055 Javadoc for WeakHashMap contains misleading advice
JDK-7166055 : Javadoc for WeakHashMap contains misleading advice

Details
Type:
Enhancement
Submit Date:
2012-05-03
Status:
Closed
Updated Date:
2012-09-27
Project Name:
JDK
Resolved Date:
2012-09-05
Component:
core-libs
OS:
linux
Sub-Component:
java.util
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
7
Fixed Versions:

Related Reports
Backport:

Sub Tasks

Description
A DESCRIPTION OF THE PROBLEM :
In the "Implementation note:" the last sentence states:
"One way to deal with this is to wrap values themselves within WeakReferences before inserting, as in: m.put(key, new WeakReference(value)), and then unwrapping upon each get."
However, it misleads the user because the value can be GC'd if there are no strong reference to the value. We think a couple more sentences should be added for this scenario.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
One way to deal with this is to wrap values themselves within WeakReferences before inserting, as in: m.put(key, new WeakReference(value)), and then unwrapping upon each get. However, as the use of WeakReference in this manner will not prevent value objects from being GC'd, this approach is only useful when entries in the map are not relied upon for keeping the underlying value objects "live".
ACTUAL -
One way to deal with this is to wrap values themselves within WeakReferences before inserting, as in: m.put(key, new WeakReference(value)), and then unwrapping upon each get.

URL OF FAULTY DOCUMENTATION :
http://docs.oracle.com/javase/7/docs/api/java/util/WeakHashMap.html

                                    

Comments
EVALUATION

http://hg.openjdk.java.net/jdk8/tl/jdk/rev/237e27c7ddc3
                                     
2012-06-04



Hardware and Software, Engineered to Work Together