Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing ConcurrentLinkedQueue<T> from outofmemory Exception

When working with ConcurrentLinkedQueue, how can I limit the size of the Queue. Is it possible that it will through outofmemory exception ?

like image 683
Youval Avatar asked Oct 26 '10 08:10

Youval


1 Answers

The size of a ConcurrentLinkedQueue is unbounded, so if producers are putting in items faster than consumers can remove them, eventually it will eat up your memory indeed. If you want to limit the size of the queue, try a blocking queue, such as LinkedBlockingQueue or ArrayBlockingQueue instead. The size of the first can be optionally bounded, while the second is always bounded.

like image 51
Péter Török Avatar answered Oct 20 '22 02:10

Péter Török