English 中文(简体)
Java:ArrayList如何管理记忆
原标题:Java: How ArrayList manages memory

在我的数据结构类别中,我们研究了Java ArrayList阶级,以及当用户增加更多内容时,它如何发展基础阵列。 这一点得到了理解。 然而,我无法指出,当从名单上删除许多内容时,这一类别如何节省记忆。 审视来文方,有三种方法删除了内容:

public E remove(int index) {
 RangeCheck(index);

 modCount++;
 E oldValue = (E) elementData[index];

 int numMoved = size - index - 1;
 if (numMoved > 0)
     System.arraycopy(elementData, index+1, elementData, index,
        numMoved);
 elementData[--size] = null; // Let gc do its work

 return oldValue;
}

public boolean remove(Object o) {
 if (o == null) {
            for (int index = 0; index < size; index++)
  if (elementData[index] == null) {
      fastRemove(index);
      return true;
  }
 } else {
     for (int index = 0; index < size; index++)
  if (o.equals(elementData[index])) {
      fastRemove(index);
      return true;
  }
        }
 return false;
}


private void fastRemove(int index) {
        modCount++;
        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index, 
                             numMoved);
        elementData[--size] = null; // Let gc do its work
}

其中没有一个减少数据储存阵列。 我甚至开始问,如果记忆已经消失,但经验测试表明,它确实如此。 因此,必须采取其他办法,但在哪里和如何? 我检查了家长班子,但没有成功。

问题回答

它们并没有减少基本阵列。 它们只会缩小规模。 这样做的理由是,如果你在阵列中有1,000个要素,删除1,为什么重新分配和复制阵列? 它非常浪费,几乎没有收获。

基本术语:JavaArrayList有两个重要特性,必须理解:

  • http://www.un.org/Depts/DGACM/index_french.htm

  • <>载体> 有多少元素(can)符合基本阵列。

传真:<0> 扩大后,即使你只增加一个部分,其规模也增加了约50%。 这是一种相反的类似原则。 从根本上说,重新定位阵列和复制价值(相对而言)是昂贵的。 如此之多,你想要尽量减少这种情况。 只要名义面积为大约2个阵列规模的工厂,就不值得担心。

只要知道,ArrayList公司就自动减少。 然而,你可以这样说:

ArrayList al = new ArrayList();

// fill the list for demo s sake
for (int i = 0; i < 1000000; ++i)
{
    al.add(i);
}

// now remove all but one element
al.removeRange(1, al.size());

// this should shrink the array back to a decent size
al.trimToSize();

值得注意的是,在GC运行期间,可动用的记忆量可能回升。

我必须再看一下ArrayList的源代码,但remove从阵列中删除该物体,然后如果该物体没有被任何其他物体提及,GC可删除该物体。 但阵列的规模并没有缩小。

阵列的规模从未自动缩小。 实际有一个清单,先填满大量内容,然后予以豁免,但仍被搁置。 并且铭记,必须有足够的记忆来保存清单(仅包括参考文献)及其要素,否则空名单所消耗的记忆就不大可能成为一个问题。

如果你真的碰到算法,这就成为一个问题,那么你仍然可以通过打电话trim ToSize(<>>a>。

转播ArrayList内部阵列,即使你利用ArrayList持有大物体,也没有什么好处。

List<LargeObject> list = new ArrayList<LargetObject>();

清单仅提及大型物体,而不自称大型物体。

参考文献确实消耗了大量空间。 (认为C点名)





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签