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

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

问题描述

在我的数据结构类中,我们研究了 Java ArrayList 类,以及当用户添加更多元素时它如何扩展底层数组.这是明白的.但是,当从列表中删除大量元素时,我无法弄清楚这个类究竟是如何释放内存的.看源码,删除元素的方法有3种:

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:

  • 大小:List中有多少名义上的元素;和

容量:在底层数组中可以容纳多少元素.

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天全站免登陆