尝试创建不同大小的数组时,Java OutOfMemoryError消息会发生更改

Gor*_*don 3 java memory

在DKSRathore的问题中如何模拟Out of memory:请求的数组大小超过VM限制在创建数组时注意到一些奇怪的行为.

创建大小为Integer.MAX_VALUE的数组时,会java.lang.OutOfMemoryError Requested array size exceeds VM limit引发错误异常.

但是,如果创建的数组大小小于max但仍高于虚拟机内存限制,则会读取错误消息java.lang.OutOfMemoryError: Java heap space.

进一步测试我设法缩小错误消息更改的位置.

long[] l = new long[2147483645];
Run Code Online (Sandbox Code Playgroud)

异常消息显示"请求的数组大小超过VM限制"

long[] l = new long[2147483644];
Run Code Online (Sandbox Code Playgroud)

异常消息显示"Java堆空间错误"

我增加了我的虚拟机内存并仍然产生了相同的结果.

有谁知道为什么会这样?

一些额外的信息:

Integer.MAX_VALUE = 2147483647
Run Code Online (Sandbox Code Playgroud)

编辑:这是我用来查找值的代码,可能会有所帮助:

int max = Integer.MAX_VALUE;
boolean done = false;
while (!done) {
    try {
        max--;
        // Throws an error
        long[] l = new long[max];
        // Exit if an error is no longer thrown
        done = true;
    } catch (OutOfMemoryError e) {
        if (!e.getMessage().contains("Requested array size exceeds VM limit")) {
            System.out.println("Message changes at " + max);
            done = true;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Jef*_*ter 6

查看JDK 7源代码:

看看这里的代码:

if (length > arrayOopDesc::max_array_length(T_ARRAY)) {
  THROW_OOP_0(Universe::out_of_memory_error_array_size());
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以通过查看max_array_length 此处的定义来查看幻数的来源.

static int32_t max_array_length(BasicType type) {
  assert(type >= 0 && type < T_CONFLICT, "wrong type");
  assert(type2aelembytes[type] != 0, "wrong type");
  // We use max_jint, since object_size is internally represented by an 'int'
  // This gives us an upper bound of max_jint words for the size of the oop.
  int32_t max_words = (max_jint - header_size(type) - 2);
  int elembytes = (type == T_OBJECT) ? T_OBJECT_aelem_bytes : type2aelembytes[type];
  jlong len = ((jlong)max_words * HeapWordSize) / elembytes;
  return (len > max_jint) ? max_jint : (int32_t)len;
}
Run Code Online (Sandbox Code Playgroud)

所以幻数是int max - 数组的头大小 - 2.我想这意味着这个特定类型的header_size是1,给出幻数MAX_VALUE -3