ArrayList的VS LinkedList的从内存分配的角度 [英] ArrayList vs LinkedList from memory allocation perspective

查看:130
本文介绍了ArrayList的VS LinkedList的从内存分配的角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要存储大量的信息,例如说名字在Java列表。项的数目可以改变(或者在短我不能predefine大小)。我认为从内存分配的角度LinkedList的会比ArrayList的一个更好的选择,因为一次最大规模的ArrayList的到达,会自动将内存分配双打,因此总会有比被分配更多的内存的机会需要什么。

I need to store a large amount of information, say for example 'names' in a java List. The number of items can change (or in short I cannot predefine the size). I am of the opinion that from a memory allocation perspective LinkedList would be a better option than ArrayList, as for an ArrayList once the max size is reached, automatically the memory allocation doubles and hence there would always be a chance of more memory being allocated than what is needed.

我从其他的帖子明白这里提到存储在LinkedList的各个元素需要超过一个ArrayList更多空间的LinkedList也需要存储节点的信息,但我还在猜测,因为我已经定义LinkedList的可能是一个更好的选择方案。另外,我并不想进入性能方面(取,删除等),尽可能多的已经上讨论。

I understand from other posts here that individual elements stored in a LinkedList takes more space than an ArrayList as LinkedList also needs to store the node information, but I am still guessing for the scenario I have defined LinkedList might be a better option. Also, I do not want to get into the performance aspect (fetching, deleting etc) , as much has already been discussed on it.

推荐答案

的LinkedList 可能分配较少的条目,但这些条目比天文数字般的他们会更昂贵的ArrayList - 足够,即使最坏的情况的ArrayList 更便宜,只要内存而言

LinkedList might allocate fewer entries, but those entries are astronomically more expensive than they'd be for ArrayList -- enough that even the worst-case ArrayList is cheaper as far as memory is concerned.

(仅供参考,我想你弄错了。的ArrayList 增长1.5倍时,它的全部,而不是2倍)

(FYI, I think you've got it wrong; ArrayList grows by 1.5x when it's full, not 2x.)

请参阅例如<一href=\"http://$c$c.google.com/p/memory-measurer/wiki/ElementCostInDataStructures\">http://$c$c.google.com/p/memory-measurer/wiki/ElementCostInDataStructures :的LinkedList 消耗每件24字节,而的ArrayList 消耗在每个单元最好的情况下4个字节,在最坏的案例每个单元6​​个字节。 (结果会因32位与64位JVM有所不同,和COM pressed对象指针选项,但在那些比较的LinkedList 至少要花36个字节/元和的ArrayList 充其量是8,最差12)。

See e.g. http://code.google.com/p/memory-measurer/wiki/ElementCostInDataStructures : LinkedList consumes 24 bytes per element, while ArrayList consumes in the best case 4 bytes per element, and in the worst case 6 bytes per element. (Results may vary depending on 32-bit versus 64-bit JVMs, and compressed object pointer options, but in those comparisons LinkedList costs at least 36 bytes/element, and ArrayList is at best 8 and at worst 12.)

更新:

我从其他的帖子明白这里提到存储在LinkedList的各个元素需要超过一个ArrayList更多空间的LinkedList也需要存储节点的信息,但我还在猜测,因为我已经定义LinkedList的可能是一个更好的选择方案。另外,我并不想进入性能方面(取,删除等),尽可能多的已经上讨论。

I understand from other posts here that individual elements stored in a LinkedList takes more space than an ArrayList as LinkedList also needs to store the node information, but I am still guessing for the scenario I have defined LinkedList might be a better option. Also, I do not want to get into the performance aspect (fetching, deleting etc) , as much has already been discussed on it.

需要明确的是,即使在最坏的情况下的ArrayList 比4倍小的LinkedList 与相同的元素。唯一可能的途径,使的LinkedList 胜利是打电话来故意整比较的ensureCapacity 以故意夸大值或从去除大量的值的ArrayList 他们已被添加之后。

To be clear, even in the worst case, ArrayList is 4x smaller than a LinkedList with the same elements. The only possible way to make LinkedList win is to deliberately fix the comparison by calling ensureCapacity with a deliberately inflated value, or to remove lots of values from the ArrayList after they've been added.

总之,这基本上是不可能让的LinkedList 赢得内存比较,如果你关心的空间,然后调用 trimToSize()的ArrayList 将即刻让的ArrayList 巨大的优势再次获胜。认真。 的ArrayList 赢了。

In short, it's basically impossible to make LinkedList win the memory comparison, and if you care about space, then calling trimToSize() on the ArrayList will instantly make ArrayList win again by a huge margin. Seriously. ArrayList wins.

这篇关于ArrayList的VS LinkedList的从内存分配的角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆