Java的:ArrayList中如何管理内存 [英] Java: How ArrayList manages memory

查看:151
本文介绍了Java的:ArrayList中如何管理内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据结构类我们研究了Java ArrayList类,以及它是如何生长的底层数组当用户添加更多的元素。该被理解。但是,我无法弄清楚究竟是如何当很多元素都从列表中删除这个类释放内存。查看源,有三种方法删除元素:

In my Data Structures class we have studies the Java ArrayList class, and how it grows the underlying array when a user adds more elements. That is understood. However, I cannot figure out how exactly this class frees up memory when lots of elements are removed from the list. Looking at the source, there are three methods that remove elements:

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
}

他们没有减少数据存储阵列。我甚至开始质疑,如果内存腾出不断发生,但实证检验表明它。所以必须有一些其他的方式,它已完成,但在哪里以及如何?我没有成功检查父类为好。

None of them reduce the datastore array. I even started questioning if memory free up ever happens, but empirical tests show that it does. So there must be some other way it is done, but where and how? I checked the parent classes as well with no success.

推荐答案

他们不减少潜在阵列。他们简单地减小尺寸。这样做的理由是,如果你有一个数组1000个元素,并删除1,为什么重新分配并复制数组?它是非常小的收益巨大浪费。

They don't reduce the underlying array. They simply decrement the size. The reasoning for this is that if you have 1000 elements in an array and delete 1, why reallocate and copy the array? It's hugely wasteful for very little gain.

Java的基本的ArrayList 活动的两个重要的属性,并了解他们不同的是很重要的:

Basically Java ArrayLists have two important properties and it's important to understand they are different:


  • 尺寸:多少元素的名义上在列表 的;以及

  • size: how many elements are notionally in the List; and

容量:的元素数量的可以的配合底层阵列中的

capacity: how many elements can fit in the underlying array.

当一个的ArrayList 扩大,它的增长了约50%的规模,即使你只需要添加一个元素。这是反向类似的原理。基本上它归结到这一点:重新分配阵列和复制的值是(相对)昂贵。正因如此,要尽量减少它的发生。只要名义大小是用工厂的数组的大小约为2,它只是不值得担心。

When an ArrayList expands, it grows by about 50% in size even if you're only adding one element. This is a similar principle in reverse. Basically it comes down to this: reallocating the array and copying the values is (relatively) expensive. So much so that you want to minimize it happening. As long as the notional size is with a factory of about 2 of the array size, it's just not worth worrying about.

这篇关于Java的:ArrayList中如何管理内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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