tags:

views:

658

answers:

4

while using Jruby, i get this message.

Complete Java stackTrace
java.lang.OutOfMemoryError: Java heap space

how to resolve ?

+1  A: 

you can set your max heap to a larger size on the command line:

java -Xmx512m MyClass
akf
+1  A: 

Either you're leaking memory and you need to find why or you need to give Java more heap space:

java -Xmx512m ...
cletus
+6  A: 

You can tune the JVM heap size using the -Xmx and -Xms JVM options: -Xmx for maximum heap size, and -Xms for initial heap size. E.g.:

java -Xms128m -Xmx256m BigApp

I usually use the same settings for the initial and max heap size.

In your case, it's really hard to say how to size the JVM without more information on what you're doing, when the problem occurs... Or maybe you just have a memory leak somewhere and increasing the heap size won't help, it will just make the problem occur later. In this case, the only solution is to fix the leak.

Last be not least, always keep in mind that the bigger the heap, the longer the major GC.

Pascal Thivent
+1  A: 

Increasing the heap dump size might only be a band-aid. You need to generate a heap dump by adding the proper proper argument:

java -XX:+HeapDumpOnOutOfMemoryError -mn256m -mx512

This will generate a file similar to java_*.hprof. You can then use a bevy of open source tools to analyze the heap dump. Java 1.6 ships with JHat, which is kind of buggy and doesn't analyze large heaps well. I use this awesome Eclipse plugin: Eclipse Memory Analyzer.

Once a report is generated from the heap dump, you can see which classes are taking up the most memory and you can use that as a starting point for debugging your code to find any memory leaks.

Droo