Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX:WeakHashMap, simple question

 
Peter Kovgan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to use WeakHashMap in my clients tracking.
My client takes a reference to SomeRemoteAdapter on remote side.
Each SomeRemoteAdapter instance has as Data instance(one instance of Data to every instance of SomeRemoteAdapter) as member:
client--->SomeRemoteAdapter --->Data
Suppouse I create WeakHashMap for to store Data instances as key, and record numbers as values.
1.The scene:
Client crashed => SomeRemoteAdapter already not referenced by client.
(How much time takes to achieve this information about crashed reference?)
My question: Does Data instance not referenced too?
Or Data instance referenced by live SomeRemoteAdapter instance and so
Data instance leaves into WeakHashMap ?

2.If I implement Unreferenced, this takes about 10 minutes(I read) to unreferenced server object to achieve the information.
How much time takes to WeakHashMap to "know" about unreferenced object?
Garbage collector knows ?

Please, help.
Regards.Peter.
P.S.
Interesting fact from:web page

Unlike many other collections, WeakHashMap cannot maintain a count of elements, since keys can be cleared at any time by the garbage collector without immediately notifying the WeakHashMap. This means that seemingly simple methods such as isEmpty() and size() have more complicated implementations than for most collections. Specifically, size() actually iterates thorugh the keys, counting those that have not been cleared. Consequently size() is an operation that takes time proportional to the size of the WeakHashMap. Similarly, isEmpty() iterates through the collection looking for a non-null key. This produces the perverse result that a WeakHashMap which is empty, due to having all its keys cleared, requires more time for isEmpty() to return than a similar WeakHashMap that is not empty

[ November 08, 2003: Message edited by: Peter Kovgan ]
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
Originally posted by Peter Kovgan:
client--->SomeRemoteAdapter --->Data
Client crashed => SomeRemoteAdapter already not referenced by client.
(How much time takes to achieve this information about crashed reference?)
My question: Does Data instance not referenced too?
Or Data instance referenced by live SomeRemoteAdapter instance and so
Data instance leaves into WeakHashMap ?

If each SomeRemoteAdapter has it's own instance of Data, then sometime after the client crashes the instance of SomeRemoteData will be garbage collected, which will result in no objects having a strong reference to the Data instance, which will result in the instance of Data being garbage collected and your keys being removed from the WeakHashMap.
Originally posted by Peter Kovgan:
2.If I implement Unreferenced, this takes about 10 minutes(I read) to unreferenced server object to achieve the information.
How much time takes to WeakHashMap to "know" about unreferenced object?
Garbage collector knows ?

It should take roughly the same amount of time for Unreferenced to be called or for the keys in a WeakHashMap to be cleared. They both rely on the Distributed Garbage Collector (dgc) determining that the client is not connected - from which point Unreferenced can be called and / or standard garbage collection can be run.
The 10 minutes you mentioned is not guaranteed (in fact, the default value is supposed to be 20 minutes). Sometime after the lease has expired the dgc should call Unreferenced and / or run standard garbage collection, but as with standard garbage collection there is no guarantee as to when (or even if) this will happen. (And the lease value may be changed programattically or from the command line, so you cannot rely on 20 minutes (I typically set the lease value to 15 seconds during my testing because I am impatient )).
To give you some idea of what is happening behind the scenes: When the lease has half expired (so 10 minutes) the client side of RMI is supposed to send a dirty() call to your RMI server to let it know that it is still alive (this is done automatically by RMI - you don't have to do anything to get this). If your RMI server does not receive this dirty() call before the lease has expired, it can call Unreferenced and / or run garbage collection.
Regards, Andrew
 
Peter Kovgan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Andrew.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic