This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Behaviour of WeakHashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Behaviour of WeakHashMap" Watch "Behaviour of WeakHashMap" New topic
Author

Behaviour of WeakHashMap

Gopal Shah
Ranch Hand

Joined: May 17, 2003
Posts: 65
Hi Friends,
Suppose we r running low of memory, then what happens to the mappings of WeakHashMap object. Are all the keys (mappings) removed or only few keys (equivalent to the amount of memory req.) are GC.
If later one is true, then on what basis the object for GC is chosen ? FIFO, LIFO or some other algo.
Thanks in advance.
Gopal Shah
Sumitro Palit
Ranch Hand

Joined: Dec 13, 2003
Posts: 37
I think when garbage collection kicks in, its going to clear as many weak references (keys) it can, normally won't do it selectively like using a FIFO/LIFO algorithm. So if you are implementating a caching mechanism using a WeakHashMap dont rely on such behavior.
Checkout this article for details:
http://www.onjava.com/pub/a/onjava/2001/07/09/optimization.html
Cheers,
-ortimus
 
Don't get me started about those stupid light bulbs.
 
subject: Behaviour of WeakHashMap
 
Similar Threads
A canonicalizing WeakHashMap
Collection: Understanding WeakHashMap
Collection Interface
What is Weak Reference
java.util package