0

I have to allocate space to an array int input[] depending on the configuration parameters height and width.

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

One of the configurations has parameters height=8192 and width=8192. So the size of the array becomes 67108864. But when i do this i get 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. I think this is not an error and not exception. How can i rectify this?

Rog Matthews
  • 3,147
  • 16
  • 37
  • 56

6 Answers6

5

Since 8192 * 8192 * 4 = 256 M (integers are 4 bytes each), your matrix is using 256 MB of heap space by itself.

You can tell the JVM how much heap space should be available to your application. From running man java and looking through the nonstandard options:

-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. This page on eclipse.org describes how to add command-line arguments to a Java program. You should add the -Xmxn1024m (or some other sufficiently large heap specification) to the "VM arguments" section of the dialog shown on that site.

Adam Mihalcin
  • 14,242
  • 4
  • 36
  • 52
  • Maybe another helpful [answer](http://stackoverflow.com/questions/37335/how-to-deal-with-java-lang-outofmemoryerror-java-heap-space-error-64mb-heap) – Markus Mar 30 '12 at 05:04
1

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:

-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. You will certainly need some more heap than that quick calculation suggests. Add maybe 20% extra, and try that out.

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

See http://javarevisited.blogspot.com/2011/05/java-heap-space-memory-size-jvm.html for a good discussion of the heap in Java.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
1

Increase your memory arguments for your Java process by adding this flag to increase the heap. 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.

*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
Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
DavidB
  • 2,064
  • 3
  • 17
  • 17
  • 1
    He would need a little more than that. The 67108864 `int`s take up 256M by themselves. – trutheality Mar 30 '12 at 05:06
  • I clarified my answer with an edit. A "definite" answer can't be given because we only know one array he needs memory for. Unless his program only uses that one array, he'll need to figure out on his own what size he needs. – DavidB Mar 30 '12 at 05:18
1

memory is not enough for your program, may be memory leak there.

you may try below,if not solve try to increase jmx value.

java -xmx1g -xms512m
Balaswamy Vaddeman
  • 8,360
  • 3
  • 30
  • 40
1

Depends on how much heap the JVM has. If you run it on the command line try adding -Xmx512m. If you work in an IDE add it to the "Run" properties.

moodywoody
  • 2,149
  • 1
  • 17
  • 21
1

An int is 32 bits (i.e. 4 bytes). So your array requires 8192*8192*4 bytes. This comes out at 256MB.

Java called with default arguments has only 64MB of heap space.

To get a larger heap, call Java using the -Xmx argument (Maximum memory size).

e.g. java -Xmx300M

awmross
  • 3,789
  • 3
  • 38
  • 51