File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

WeakHashMap not releasing its Key

 
Qusay Jaafar
Ranch Hand
Posts: 127
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11831
181
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Qusay Jaafar
Ranch Hand
Posts: 127
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11831
181
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic