Search code examples
javaarraylistlinked-listqueueblockingqueue

Memory consumption in arrayBlockingQueue and linkedBlockingQueue


Say I make capacity of arrayBlockingQueue and linkedBlockingQueue both to 100. I add only 10 elements in each of them.

Will array hold full capacity even though 90 elements are empty? I mean would it have 10 elements and 90 nulls? Another thing, how would linked behave in this case? Would it have 10 or 100 nodes? And would there be 90 'null value nodes'?

Can someone explain me this? How do they behave in this case?


Solution

  • In case of ArrayBlockingQueue it will create an array of size 100. It will contain 10 elements that you inserted and the rest will be null.

    In case of LinkedBlockingQueue only 10 nodes will be created - the capacity here is used as a limitation to create so called bounded queue with maximum entries limit (in multithreaded environment it might be handy to not get out of memory).