巨大的阵列引发内存不足,尽管有足够的可用内存 [英] Huge arrays throws out of memory despite enough memory available

查看:149
本文介绍了巨大的阵列引发内存不足,尽管有足够的可用内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 -Xmx1G 标记提供一千兆字节的堆,如预期以下工作:

Using the -Xmx1G flag to provide a heap of one gigabyte, the following works as expected:

public class Biggy {
    public static void main(String[] args) {
        int[] array = new int[150 * 1000 * 1000];
    }
}

数组应重新present大约600 MB。

The array should represent around 600 MB.

但是,下面将引发的OutOfMemoryError:

However, the following throws OutOfMemoryError:

public class Biggy {
    public static void main(String[] args) {
        int[] array = new int[200 * 1000 * 1000];
    }
}

尽管阵列应该重新present大约800 MB,因此很容易适应在内存中。

Despite the array should represent around 800 MB and therefore easily fit in memory.

在哪儿丢失的记忆消失了?

Where's the missing memory gone?

推荐答案

在Java中,你通常在堆多个区域(和分区域)。你有一个年轻的终身地区与大多数收藏家。大型阵列添加到直线距离不过根据你的最大内存大小年老区,一些空间将被保留给年轻的空间。如果您分配内存慢慢地,这些地区将调整不过当你看到这样的大块可以简单地失败。

In Java you typically have multiple regions (and sub regions) in the heap. You have a young and tenured region with most collectors. Large arrays are added to the tenured area straight away however based on your maximum memory size, some space will be reserved for the young space. If you allocate memory slowly these regions will resize however a large block like this can simply fail as you have seen.

由于内存通常比较便宜(并非总是如此),我只是想增加最大的地步,你会想,如果它曾经使用的应用程序失败的多。

Given memory is usually relatively cheap (not always the case) I would just increase the maximum to the point where you would want the application fail if it ever used that much.

BTW:如果你有一个大的结构是这样,你可能会考虑使用直接内存

BTW: If you have a large structure like this you might consider using direct memory.

IntBuffer array = ByteBuffer.allocateDirect(200*1000*1000*4)
                            .order(ByteOrder.nativeOrder()).asIntBuffer();

int a = array.get(n);
array.put(n, a+1);

它有点乏味写,但是有一个很大的优势,它使用几乎没有堆。 (有小于1的KB头顶)

Its a bit tedious to write but has one big advantage, it uses almost no heap. (there is less than 1 KB over head)

这篇关于巨大的阵列引发内存不足,尽管有足够的可用内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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