简体   繁体   English

Java数组的最大限制

[英]maximum limit on Java array

I am trying to create 2D array in Java as follows: 我试图在Java中创建2D数组,如下所示:

int[][] adjecancy = new int[96295][96295];

but it is failing with the following error: 但它失败了,出现以下错误:

JVMDUMP039I Processing dump event "systhrow", detail "java/lang/OutOfMemoryError" at 2017/04/07 11:58:55 - please wait.
JVMDUMP032I JVM requested System dump using 'C:\eclipse\workspaces\TryJavaProj\core.20170407.115855.7840.0001.dmp' in response to an event
JVMDUMP010I System dump written to C:\eclipse\workspaces\TryJavaProj\core.20170407.115855.7840.0001.dmp
JVMDUMP032I JVM requested Heap dump using 'C:\eclipse\workspaces\TryJavaProj\heapdump.20170407.115855.7840.0002.phd' in response to an event
JVMDUMP010I Heap dump written to C:\eclipse\workspaces\TryJavaProj\heapdump.20170407.115855.7840.0002.phd

A way to solve this is by increasing the JVM memory but I am trying to submit the code for an online coding challenge. 解决这个问题的方法是增加JVM内存,但我正在尝试提交在线编码挑战的代码。 There it is also failing and I will not be able to change the settings there. 它也失败了,我将无法改变那里的设置。

Is there any standard limit or guidance for creating large arrays which one should not exceed? 是否有任何标准限制或指导用于创建不应超过的大型阵列?

int[][] adjecancy = new int[96295][96295];

When you do that you are trying to allocate 96525*96525*32 bits which is nearly 37091 MB which is nearly 37 gigs. 当你这样做时,你试图分配96525*96525*32位,接近37091 MB,接近37演出。 That is highly impossible to get the memory from a PC for Java alone. 仅仅从Java获取内存是非常不可能的。

I don't think you need that much data in your hand on initialization of your program. 我认为你的程序初始化不需要那么多数据。 Probably you have to look at ArrayList which gives you dynamic allocation of size and then keep on freeing up at runtime is a key to consider. 可能你必须看看ArrayList,它给你动态的大小分配,然后在运行时继续释放是一个需要考虑的关键。

There is no limit or restriction to create an array. 创建数组没有限制或限制。 As long as you have memory, you can use it. 只要你有记忆,就可以使用它。 But keep in mind that you should not hold a block of memory which makes JVM life hectic. 但请记住,你不应该持有一块内存,这会让JVM变得忙碌。

Array must obviously fit into memory. 数组显然必须适合内存。 If it does not, the typical solutions are: 如果没有,典型的解决方案是:

  • Do you really need int (max value 2,147,483,647)? 你真的需要int (最大值2,147,483,647)吗? Maybe byte (max value 127) or short is good enough? 也许byte (最大值127)或short是足够好? byte is 8 times smaller than int . byteint小8倍。
  • Do you have really many identical values in array (like zeros)? 你在数组中有很多相同的值(比如零)吗? Try to use sparse arrays. 尝试使用稀疏数组。

for instance: 例如:

Map<Integer, Map<Integer, Integer>> map = new HashMap<>();
map.put(27, new HashMap<Integer, Integer>()); // row 27 exists
map.get(27).put(54, 1); // row 27, column 54 has value 1.

They need more memory per value stored, but have basically no limits on the array space (you can use Long rather than Integer as index to make them really huge). 它们每个存储的值需要更多的内存,但对数组空间基本没有限制(你可以使用Long而不是Integer作为索引来使它们真的很大)。

  • Maybe you just do not know how long the array should be? 也许你只是不知道阵列应该有多长? Try ArrayList , it self-resizes. 尝试ArrayList ,它自我调整大小。 Use ArrayList of ArrayLists for 2D array. ArrayListArrayLists用于2D数组。

  • If nothing else is helpful, use RandomAccessFile to store your overgrown data into the filesystem. 如果没有其他帮助,请使用RandomAccessFile将过度生长的数据存储到文件系统中。 100 Gb or about are not a problem in these times on a good workstation, you just need to compute the required offset in the file. 在良好的工作站上,这些时间100 Gb或大约不是问题,您只需要在文件中计算所需的偏移量。 The filesystem is obviously much slower than RAM but with good SSD drive may be bearable. 文件系统显然比RAM慢得多,但具有良好的SSD驱动器可能是可以忍受的。

It is recommended to allocate Maximum Heap Size that can be allocated is 1/4th of the Machine RAM Size. 建议分配可以分配的最大堆大小是机器RAM大小的1/4。

1 int in Java takes 4 bytes and your array allocation needs approximately 37.09GB of Memory. Java中的1个int占用4个字节,并且您的阵列分配需要大约37.09GB的内存。

In that case even if I assume you are allocating Full Heap to just an Array your machine should be around 148GB RAM. 在这种情况下,即使我假设您正在为一个阵列分配Full Heap,您的机器也应该在148GB RAM左右。 That is huge. 这是巨大的。

Have a look at below. 看看下面。

Ref: http://docs.oracle.com/javase/8/docs/technotes/guides/vm/gc-ergonomics.html 参考: http//docs.oracle.com/javase/8/docs/technotes/guides/vm/gc-ergonomics.html

Hope this helps. 希望这可以帮助。

It depends on maximum memory available to your JVM and the content type of the array. 它取决于JVM可用的最大内存和阵列的内容类型。 For int we have 4 bytes of memory. 对于int,我们有4个字节的内存。 Now if 1 MB of memory is available on your machine , it can hold maximum of 1024 * 256 integers(1 MB = 1024 * 1024 bytes). 现在,如果您的计算机上有1 MB内存可用,它最多可以保存1024 * 256个整数(1 MB = 1024 * 1024字节)。 Keeping that in mind you can create your 2D array accordingly. 记住这一点,您可以相应地创建您的2D阵列。

Array that you can create depends upon JVM heap size. 您可以创建的数组取决于JVM堆大小。

96295*96295*4(bytes per number) = 37,090,908,100 bytes = ~34.54 GBytes. 96295 * 96295 * 4(每个字节的字节数)= 37,090,908,100字节= ~34.54 GBytes。 Most JVMs in competitive code judges don't have that much memory. 竞争法典判断中的大多数JVM都没有那么多内存。 Hence the error. 因此错误。

To get a good idea of what array size you can use for given heap size - Run this code snippet with different -Xmx settings: 要了解可用于给定堆大小的数组大小 - 使用不同的-Xmx设置运行此代码段:

    Scanner scanner = new Scanner(System.in);
    while(true){
        System.out.println("Enter 2-D array of size: ");
        size = scanner.nextInt();

        int [][]numbers = new int[size][size];
        numbers = null;
    }

eg with -Xmx 512M -> 2-D array of ~10k+ elements. 例如,使用-Xmx 512M - > ~10k +元素的2-D阵列。

Generally most of online judges have ~1.5-2GB heap while evaluating submissions. 通常,大多数在线评委在评估提交时都有大约1.5-2GB的堆积

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM