如何界定的ArrayList容量的概念? [英] How to define the concept of capacity in ArrayLists?

查看:112
本文介绍了如何界定的ArrayList容量的概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,容量是在一个ArrayList元件或可用空间,可以或可以不持有的值引用的对象的数目。我想了解更多关于能力的概念。

所以,我有三个问题:

1)有什么好的方法来定义什么身份重新presents从存储的角度来看?

...分配到ArrayList的(连续的?)内存?

...在的ArrayList对内存占用(堆?)?

2)然后如果上述属实,不断变化的容量要求的存储管理开销某种方式?

3)任何人有一个例子,#2,或可能是一个性能问题?除了有可能他们的能力不断调整,一大批大型的ArrayList的?


解决方案

  1. ,因为它是基于阵列上的类名为ArrayList中。容量是数组,这需要连续的堆内存块的大小。但是,请注意数组本身只包含的引用的的元素,这是堆独立的对象。

  2. 增加容量,需要分配一个新的,更大的阵列和复制从旧的阵列到新的,在此之后,旧的就符合垃圾收集的所有引用。

  3. 您已经列举的主要情况下的表现可能是一个问题。在实践中,我从来没有见过它实际上成为一个问题,因为该元素对象通常占用比列表更多的内存(也可能是CPU时间)。

I understand that capacity is the number of elements or available spaces in an ArrayList that may or may not hold a value referencing an object. I am trying to understand more about the concept of capacity.

So I have three questions:

1) What are some good ways to define what capacity represents from a memory standpoint?

...the (contiguous?) memory allocated to the ArrayList?

...the ArrayLists’s memory footprint on the (heap?)?

2) Then if the above is true, changing capacity requires some manner of memory management overhead?

3) Anyone have an example where #2 was or could be a performance concern? Aside from maybe a large number of large ArrayLists having their capacities continually adjusted?

解决方案

  1. The class is called ArrayList because it's based on an array. The capacity is the size of the array, which requires a block of contiguous heap memory. However, note that the array itself contains only references to the elements, which are separate objects on the heap.
  2. Increasing the capacity requires allocating a new, larger array and copying all the references from the old array to the new one, after which the old one becomes eligible for garbage collection.
  3. You've cited the main case where performance could be a concern. In practice, I've never seen it actually become a problem, since the element objects usually take up much more memory (and possibly CPU time) than the list.

这篇关于如何界定的ArrayList容量的概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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