Java8构造函数引用的可怕性能和大堆内存占用?

我只是有一个相当不愉快的经验,在我们的生产环境,造成 OutOfMemoryErrors: heapspace..

我将问题追溯到我在函数中使用的 ArrayList::new

为了通过声明的构造函数(t -> new ArrayList<>())验证这实际上比正常的创建执行得更差,我编写了以下小方法:

public class TestMain {
public static void main(String[] args) {
boolean newMethod = false;
Map<Integer,List<Integer>> map = new HashMap<>();
int index = 0;


while(true){
if (newMethod) {
map.computeIfAbsent(index, ArrayList::new).add(index);
} else {
map.computeIfAbsent(index, i->new ArrayList<>()).add(index);
}
if (index++ % 100 == 0) {
System.out.println("Reached index "+index);
}
}
}
}

使用 newMethod=true;运行该方法将导致使用 OutOfMemoryError的方法在索引达到30k 之后失败。使用 newMethod=false;时,程序不会失败,但是会不断冲击,直到被杀死(指数很容易达到150万)。

为什么 ArrayList::new在堆上创建如此多的 Object[]元素,以至于它能如此快地创建 OutOfMemoryError

(顺便说一句,当集合类型为 HashSet时也会发生这种情况。)

4256 次浏览

In the first case (ArrayList::new) you are using the constructor which takes an initial capacity argument, in the second case you are not. A large initial capacity (index in your code) causes a large Object[] to be allocated, resulting in your OutOfMemoryErrors.

Here are the two constructors' current implementations:

public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}

Something similar happens in HashSet, except the array is not allocated until add is called.

The computeIfAbsent signature is the following:

V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)

So the mappingFunction is the function which receives one argument. In your case K = Integer and V = List<Integer>, so the signature becomes (omitting PECS):

Function<Integer, List<Integer>> mappingFunction

When you write ArrayList::new in the place where Function<Integer, List<Integer>> is necessary, compiler looks for the suitable constructor which is:

public ArrayList(int initialCapacity)

So essentially your code is equivalent to

map.computeIfAbsent(index, i->new ArrayList<>(i)).add(index);

And your keys are treated as initialCapacity values which leads to pre-allocation of arrays of ever increasing size, which, of course, quite fast leads to OutOfMemoryError.

In this particular case constructor references are not suitable. Use lambdas instead. Were the Supplier<? extends V> used in computeIfAbsent, then ArrayList::new would be appropriate.