while using Jruby, i get this message.
Complete Java stackTrace
java.lang.OutOfMemoryError: Java heap space
how to resolve ?
while using Jruby, i get this message.
Complete Java stackTrace
java.lang.OutOfMemoryError: Java heap space
how to resolve ?
TLDR: jruby -J-Xmx1024m script_you_want_to_run.rb
As others have mentioned, your program is trying to allocate more memory than the max size the JVM is allowed to allocate.
Also as others have mentioned, you can configure Java to allow more memory allocation by telling it via the command line with the argument -Xmx1024m (as an example).
-Xmx is the argument for max memory, and the 1024m would be the memory size (the final m for megabytes). I think JRuby starts the JVM with max memory set to 512m already, so you'll probably want to be going higher than that.
To send the arguments to the JVM from the command line using jruby, you'll need to add -J in front of the argument, so your command line will look like this:
jruby -J-Xmx1024m script_you_want_to_run.rb
I also agree with the memory leak sentiments: If you're not really dealing with a ton of objects with the expectation that you might be seeing this error, then you might want to look into the possibility that your program is having unintended side effects.
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.
Setting JRUBY_OPTS
is the solution that worked best for me. It was mentioned in Koray's answer.
set JRUBY_OPTS=-J-Xmx2g
check the JVM version of the jruby with this command
jruby -v
If it returns this string it means that you are using an 32bit JVM therefore you can not set max heap size to >=2gb
jruby 1.7.3 (1.9.3p385) 2013-02-21 dac429b on Java HotSpot(TM) Client VM 1.6.0_37-b06 [Windows 7-x86]
You need to set your JAVA_HOME to your 64bit version of Java. e.g.
set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_09
You will see following output if you rub jruby -v command again
jruby 1.7.3 (1.9.3p385) 2013-02-21 dac429b on Java HotSpot(TM) 64-Bit Server VM 1.7.0_09-b05 [Windows 7-amd64]
You can see that you are now using 64bit version of JVM. After that you can set either jruby opts environment parameter as follows
set JRUBY_OPTS=-J-Xmx2g
or you can run your program as follows
jruby -J-Xmx2g <your ruby program>
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.
you can set your max heap to a larger size on the command line:
java -Xmx512m MyClass
Either you're leaking memory and you need to find why or you need to give Java more heap space:
java -Xmx512m ...
The message "Java heap space" indicates that new objects could not be allocated in the Java Heap. The error does not necessarily imply application memory leak. The problem could be as simple as a configuration issue, where the specified heap size (or the default size, if not specified) is insufficient for the application.
Potential sources of this Error and solutions: http://www.blogsoncloud.com/jsp/techSols/java-lang-OutOfMemoryError-Java-heap-space.jsp
I encounter a similar problem and I cannot find a satisfactory solution after googling for a few hours. If all you need is to set the Xms and Xmx params, here is what works for me:
ps aux | grep java
Find out what is actually running by jruby. Kill it (kill -9 process_id). Re-run the command with the native Java options on the command line.
Hope it helps.
By the way, I am using puma server with Jruby on Rails 4.0, and if anyone knows a more elegant solution, please let me know. I have tried everything (after_use_hook, .rvmrc, .bashrc, etc. etc.)
Thanks.