[concurrency-interest] Unbounded blocking queues - memory consumption

Doug Lea dl at cs.oswego.edu
Fri Oct 7 09:23:06 EDT 2005

Calum MacLean wrote:
> Hi
> My application is producing a large unbounded quantity of objects which are 
> being put into a BlockingQueue for consumption by some other thread.
> I'm currently using LinkedBlockingQueue, as it's unbounded.
> However, I'm seeing that the LinkedBlockingQueue.Node objects take up a fair 
> bit of memory - around about 16 bytes each according to my profiler.
> So, while I'm not 100% sure, I'm a bit worried about the memory consumption, 
> as it's a large number of objects which are being produced.
> Are there any other alternatives for unbounded BlockingQueue implementations 
> which maybe take up less memory than LinkedBlockingQueue?

I don't believe there is any way to reduce overhead for any
linked structure down and further -- the nodes only have item
and next fields; plus the usual Java per-Object header etc.
If you really need the space and are in full control of the
kinds of elements, you might be able to make a custom version.
If for example, each element is of class Element, you can add
a "next" link to the Element class, used only by the queue, and
then copy-paste-hack LinkedBlockingQueue to directly use it
rather than wrapping each item in a Node. This is not recommended
unless you are desparate though.


More information about the Concurrency-interest mailing list