在`arrayBlockingQueue`和`linkedBlockingQueue`中的内存消耗

huangapple go评论90阅读模式
英文:

Memory consumption in arrayBlockingQueue and linkedBlockingQueue

问题

以下是翻译好的内容:

假设我将 arrayBlockingQueuelinkedBlockingQueue 的容量都设置为 100。我只在每个队列中添加了 10 个元素。

即使有 90 个空元素,array 是否会保持完整的容量?我的意思是它会有 10 个元素和 90 个 null 值吗?
另外,linked 在这种情况下会如何表现?它会有 10 个还是 100 个节点?会有 90 个“空值节点”吗?

有人可以解释一下这个问题吗?它们在这种情况下会如何表现?

英文:

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?

答案1

得分: 3

ArrayBlockingQueue的情况下,它将创建一个大小为100的数组。其中将包含您插入的10个元素,其余部分将是null

LinkedBlockingQueue的情况下,只会创建10个节点 - 这里的容量被用作限制,以创建所谓的有界队列,其具有最大条目限制(在多线程环境中,这可能很方便,以免出现内存不足的情况)。

英文:

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).

huangapple
  • 本文由 发表于 2020年8月27日 02:12:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63603467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定