是否为arraylist连续分配内存? [英] is memory contiguous allocated for arraylist?

查看:909
本文介绍了是否为arraylist连续分配内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们使用新的ArrayList()时,是否连续分配内存? 如果我们调用list.add(e)十次,那么所有元素是否将按连续顺序连续存储在内存中,还是随机存储在内存中?

谢谢.

解决方案

首先,您需要了解ArrayList的工作方式.它将引用"或指针"存储到内部对象数组elementData中的实际存储中.这种引用数组很可能是连续的,但特定于JVM.添加的实际对象存储在堆中,尽管这是特定于JVM的,但几乎可以肯定不会是连续的.

elementData[0] ===> object 1
elementData[1] ===> object 2
elementData[2] ===> object 3
...

第二,您提到多次调用add()...当ArrayList内部elementData不再足够大时,它将其大小调整为更大的+ 50%IIRC,并复制所有引用到新的elementData时,实际对象不会移动...

最后连续的内存通常是高性能本机应用程序的关注点.在Java中,内存是由JVM管理的,并且是从基础OS借用的,依次从硬件甚至是虚拟化的硬件中借用的.

when we use new ArrayList() is memory contiguous allocated? if we call list.add(e) 10 times, will all elements contiguous stored in memory by add order, or it is randomly stored in memory?

thanks.

解决方案

Firstly you need to understand how ArrayList works. It stores "references" or "pointers" to actual storage in an internal object array elementData. This array of references may well be contiguous, but is JVM specific. The actual objects being added are stored on the heap, and almost certainly won't be contiguous, although this is JVM specific.

elementData[0] ===> object 1
elementData[1] ===> object 2
elementData[2] ===> object 3
...

Secondary, you mention calling add() multiple times... When ArrayList internal elementData is no longer big enough it resizes it to a bigger one, +50% IIRC, and copies all the references to the new elementData, the actual objects do not move...

Lastly contiguous memory is typically a concern of high-performance native applications. In Java memory is managed by the JVM and is borrowed from the underlying OS, in turn from the hardware, or even virtualized hardware...

这篇关于是否为arraylist连续分配内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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