[concurrency-interest] LinkedBlockingQueue extract and memory "waste"

Francois Valdy francois.valdy at gmail.com
Tue Feb 10 16:26:58 EST 2009


I'm having memory issues with LinkedBlockingQueue (don't worry, I
won't come along saying there's a memory leak in the JDK).

The issue is that the method extract() doesn't "help" the GC by
dereferencing head.next prior to changing the head node (with
head.next = null;).

What comes then is that once a node has been put in old gen for
whatever reason, all generated nodes from this LinkedBlockingQueue
will be as well.
You'll tell me that memory will be recovered upon a full gc, but my
point is exactly to avoid full gc.

Easiest way to reproduce issue is for force a gc (System.gc(), or
JConsole/JVisualVM) during queue usage, as it'll promote the current
used nodes to old gen.
Prior to this step memory was recovered smoothly by minor gc's, after
this step you're good for a full gc of only LinkedBlockingQueue nodes
(16bytes in JRE32, 32bytes in JRE64).

Doug, I understand your attempt at removing extraneous code, but maybe
this one wasn't the best choice :)
LinkedList for instance doesn't follow the same strategy, and
explicitly dereferences next/previous (even if unnecessary from a
memory leak pov).

I'll now look for a workaround (might end up "recoding"
LinkedBlockingQueue with one added line :) )

Any thoughts ? (I won't consider moving to realtime java for just that :) )


(this might be the reason for all those unresolved threads about
LinkedBlockingQueue memory consumption, just a guess)

More information about the Concurrency-interest mailing list