Salut,
windshome wrote:
> In "Tricks and Tips with NIO part II: Why SelectionKey.attach() is evil", I
> read this text:
>
> "Boum...the little ... is where the devil is hiding! What you are attaching
> to the SelectionKey is very dangerous, because there is some probability
> that your SelectionKey might never return to a ready-operation state,
> leaving the SelectionKey and its evil attachment forever inside the Selector
> keys set."
>
> But can someone tell me why? what is the "some probability", some bug of JVM
> or OS or TCP?
An example is when you attach a complex object to a SelectionKey, (let's
use a Hashmap with full of big object). Let's say you register for
OP_READ (read operation) the SelectionKey to the main Selector. So the
Selector's keys queue will have your SelectionKey + BigHashMap. Now
assume the OP_READ event never happens (or takes a long time, like 5
minutes). Hence you might ends up with 16 000 SelectionKey + BigHashMap
that does nothing except eating the heap. In that case you will ends up
in a memory leak because the attachment cannot be reclaimed by the
garbage collection.
Hence you really need to be careful when attaching objects to the
SelectionKey. Nabble is down right now, but search the dev list with the
SelectionKey keyword...we once and a while discuss this issue. I will
post the link as soon as Nabble is back.
Does it makes sense?
Thanks
-- Jeanfrancois
I must have a valid reason tell to other developer.
>
> thanks.