为什么我无法创建大尺寸的数组? [英] Why I can't create an array with large size?

查看:148
本文介绍了为什么我无法创建大尺寸的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不可能创建一个max int size的数组?

Why it is impossible to create an array with max int size?

int i = 2147483647;
int[] array = new int[i];

我找到了这样的解释:


通过32位整数访问Java数组,最大理论数组大小为2147483647个元素。

Java arrays are accessed via 32-bit ints, resulting in a maximum theoretical array size of 2147483647 elements.

但是你可以看到我的代码不起作用。创建一个大小为

But as you can see my code doesn't work. It is also impossible to create an array with size

new int[Integer.MAX_VALUE - 5];



技术细节




  • 64位HotSpot JVM

  • OSX 10.10.4

  • PS

    为什么 -5 实际上?

    推荐答案

    理论



    有两种可能的例外情况:

    Theory

    There are two possible exceptions:


    • OutOfMemoryError:Java堆空间表示您的数组不适合java堆空间。为了解决这个问题,可以使用JVM选项 -Xmx 来增加最大堆大小。还要考虑到对象的最大大小不能大于最大的堆生成量

    • OutOfMemoryError:请求的数组大小超过VM限制表示超出了特定于平台的大小:


      • 上限是由用于描述数组中索引的大小类型的限制设置的,因此理论数组大小受限于 2 ^ 31-1 = 2147483647 elements。

      • 另一个限制是特定于JVM /平台。根据第10章:Java语言规范的数组,Java SE 7版对数组长度没有严格限制,因此可以在不违反JLS的情况下减少数组大小。

      • OutOfMemoryError: Java heap space means your array does not fit into java heap space. In order to solve you can increase the maximum heap size by using JVM option -Xmx. Also take into account that the maximum size of object cannot be larger than the largest heap generation.
      • OutOfMemoryError: Requested array size exceeds VM limit means platform-specific size was exceeded:
        • the upper bound limit is set by the restrictions of the size type used to describe an index in the array, so theoretical array size is limited by 2^31-1=2147483647 elements.
        • the other limit is JVM/platform specific. According to chapter 10: Arrays of The Java Language Specification, Java SE 7 Edition there is no strict limit on array length, thus array size may be reduced without violating JLS.

        在HotSpot中,JVM数组大小受内部表示的限制。在GC代码中,JVM以堆字的形式传递一个数组大小作为 int 然后从堆字转换回 jint 这可能会导致溢出。因此,为了避免崩溃和意外行为,最大数组长度受到(最大尺寸 - 标题大小)标题大小取决于用于构建正在运行的JVM的C / C ++编译器(用于linux的gcc,用于macos的clang)和运行时设置(如 UseCompressedClassPointers ) 。例如在我的linux上:

        In HotSpot JVM array size is limited by internal representation. In the GC code JVM passes around the size of an array in heap words as an int then converts back from heap words to jint this may cause an overflow. So in order to avoid crashes and unexpected behavior the maximum array length is limited by (max size - header size). Where header size depends on C/C++ compiler which was used to build the JVM you are running(gcc for linux, clang for macos), and runtime settings(like UseCompressedClassPointers). For example on my linux:


        • Java HotSpot(TM)64位服务器VM 1.6.0_45限制整数。 MAX_VALUE

        • Java HotSpot(TM)64位服务器VM 1.7.0_72限制 Integer.MAX_VALUE-1

        • Java HotSpot(TM)64位服务器VM 1.8.0_40限制 Integer.MAX_VALUE-2

        • Java HotSpot(TM) 64-Bit Server VM 1.6.0_45 limit Integer.MAX_VALUE
        • Java HotSpot(TM) 64-Bit Server VM 1.7.0_72 limit Integer.MAX_VALUE-1
        • Java HotSpot(TM) 64-Bit Server VM 1.8.0_40 limit Integer.MAX_VALUE-2
        • https://bugs.openjdk.java.net/browse/JDK-8059914
        • https://bugs.openjdk.java.net/browse/JDK-8029587

        这篇关于为什么我无法创建大尺寸的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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