This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes WeakHashMap not releasing its Key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "WeakHashMap not releasing its Key" Watch "WeakHashMap not releasing its Key" New topic
Author

WeakHashMap not releasing its Key

Qusay Jaafar
Ranch Hand

Joined: May 06, 2002
Posts: 127
The Senario of:
Client A has the lock of a record.
Client B trying to lock but he is in the waiting status because Client A locked.
Client A crashed.
Here, suppose the Map need to release the locking of Client A. That will be done in many different ways.
I used a WeakHashMap but it's never released the locking of crashed client.
That's because WeakHashMap release the weakReference only with a low memory.
So, what can we do here please?



Qusay
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11490
    
  95

Hi Qusay
Have you tried suggesting that the garbage collector run at regular intervals? Whenever the garbage collector runs, it should clear out weak references.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Qusay Jaafar
Ranch Hand

Joined: May 06, 2002
Posts: 127
I didn't try it, but I will.
What I did that I left the application working all the night. Morning it was the same situation when I left it.
Client A still locking.
other Clients were waiting.
So, if I try to run gc() into the server, shall I keep it as a part of the server? I don't think so...
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11490
    
  95

Hi Qusay
Remember that there is no notification that the item has been removed from the WeakHashMap - you will have to have some way of checking whether the lock has been silently removed.
The following scenario might work by accident:
  • A get's lock(5)
  • B attempts to get lock(5) - goes into wait
  • A crashes
  • some time later (at least 20 minutes?) lock for 5 silently removed
  • C gets lock(7)
  • C releases lock(7) - calls notifyAll()
  • B wakes up, realises that there is noone holding lock 5, continues processing.


  • As you can see, this relied on a third party to wake up client B.
    Regards, Andrew
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: WeakHashMap not releasing its Key