简体   繁体   English

初始化Array:OutOfMemoryError时出错

[英]Error while initializing Array:OutOfMemoryError

I have to allocate space to an array int input[] depending on the configuration parameters height and width. 我必须根据配置参数的高度和宽度为数组int input[]分配空间。

int input[]=new int[height * width]; //this is line no 538

One of the configurations has parameters height=8192 and width=8192 . 配置之一具有参数height=8192width=8192 So the size of the array becomes 67108864 . 因此,阵列的大小变为67108864 But when i do this i get OutOfMemoryError. 但是当我这样做时,我得到了OutOfMemoryError。

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
        at Test.main(Test.java:538)

I have ran this program on eclipse as well as on cygwin but i am facing the same problem. 我已经在eclipse和cygwin上运行了该程序,但是我面临着同样的问题。 I think this is not an error and not exception. 我认为这不是错误,也不是例外。 How can i rectify this? 我该如何纠正?

Since 8192 * 8192 * 4 = 256 M (integers are 4 bytes each), your matrix is using 256 MB of heap space by itself. 由于8192 * 8192 * 4 = 256 M(整数是4个字节),因此矩阵本身就使用256 MB的堆空间。

You can tell the JVM how much heap space should be available to your application. 您可以告诉JVM应用程序应使用多少堆空间。 From running man java and looking through the nonstandard options: 通过运行man java并查看非标准选项:

-Xmxn -Xmxn

  Specify the maximum size, in bytes, of the memory allocation pool. This value must a multiple of 1024 greater than 2MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is chosen at runtime based on system configuration. For more information, see HotSpot Ergonomics Examples: -Xmx83886080 -Xmx81920k -Xmx80m On Solaris 7 and Solaris 8 SPARC platforms, the upper limit for this value is approximately 4000m minus overhead amounts. On Solaris 2.6 and x86 platforms, the upper limit is approximately 2000m minus overhead amounts. On Linux platforms, the upper limit is approximately 2000m minus overhead amounts. 

To use this option, you would start your application with a command like 要使用此选项,您将使用以下命令启动应用程序

java -Xmxn1024m -jar foo.jar

In Eclipse, you can add command-line options as well. 在Eclipse中,您也可以添加命令行选项。 This page on eclipse.org describes how to add command-line arguments to a Java program. eclipse.org上的此页面描述了如何向Java程序添加命令行参数。 You should add the -Xmxn1024m (or some other sufficiently large heap specification) to the "VM arguments" section of the dialog shown on that site. 您应将-Xmxn1024m (或其他足够大的堆规范)添加到该站点上显示的对话框的“ VM arguments”部分。

You probably have too little heap space to hold an array of the size you are targeting. 您的堆空间可能太少,无法容纳要定位的大小的数组。 You can increase the size of your heap with command line switches. 您可以使用命令行开关来增加堆的大小。 For example, to set it to 256MB, include this switch: 例如,要将其设置为256MB,请包括以下开关:

-Xmx256m -Xmx256m

If you multiply height * width * 4 (4 is the storage in bytes for an int) you can get a rough gauge of the amount of heap you will need, assuming the rest of the program does not need a significant amount. 如果乘以height * width * 4 (4是一个int的字节存储量),则可以假定您程序的其余部分不需要很多,就可以大致了解所需的堆数量。 You will certainly need some more heap than that quick calculation suggests. 当然,您将需要比快速计算所建议的更多的堆。 Add maybe 20% extra, and try that out. 可能还要增加20%,然后尝试一下。

To get a better number than a rule-of-thumb calculation, you can look into heap profilers. 要获得比经验法则计算更好的数字,可以研究堆分析器。 There are several open source options: 有几个开源选项:

http://java-source.net/open-source/profilers http://java-source.net/open-source/profilers

See http://javarevisited.blogspot.com/2011/05/java-heap-space-memory-size-jvm.html for a good discussion of the heap in Java. 有关Java堆的详细讨论,请参见http://javarevisited.blogspot.com/2011/05/java-heap-space-memory-size-jvm.html

Increase your memory arguments for your Java process by adding this flag to increase the heap. 通过添加此标志来增加堆,可以增加Java进程的内存参数。 You might need to play around to get the optimal size for the heap. 您可能需要四处摸索以获得堆的最佳大小。 This will set the "max" heap size. 这将设置“最大”堆大小。 The default is probably really small. 默认值可能很小。 64M is a common max size for many Java EE containers. 64M是许多Java EE容器的常见最大大小。

*Note I'm not saying this is exactly the size you'll need. *请注意,我并不是说这正是您需要的尺寸。 Your unique case will dictate the size you'll need which you may need to experiment with. 您的情况会决定您需要的尺寸,您可能需要尝试使用该尺寸。

 -Xmx256M

memory is not enough for your program, may be memory leak there. 程序内存不足,可能是那里的内存泄漏。

you may try below,if not solve try to increase jmx value. 您可以尝试以下方法,如果无法解决,请尝试增加jmx值。

java -xmx1g -xms512m

Depends on how much heap the JVM has. 取决于JVM有多少堆。 If you run it on the command line try adding -Xmx512m. 如果您在命令行上运行它,请尝试添加-Xmx512m。 If you work in an IDE add it to the "Run" properties. 如果在IDE中工作,请将其添加到“运行”属性。

An int is 32 bits (ie 4 bytes). 一个int是32位(即4个字节)。 So your array requires 8192*8192*4 bytes. 因此,您的数组需要8192 * 8192 * 4个字节。 This comes out at 256MB. 出来的是256MB。

Java called with default arguments has only 64MB of heap space. 使用默认参数调用的Java只有64MB的堆空间。

To get a larger heap, call Java using the -Xmx argument (Maximum memory size). 要获得更大的堆,请使用-Xmx参数(最大内存大小)来调用Java。

eg java -Xmx300M 例如java -Xmx300M

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

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