java.lang.OutOfMemoryError: Java heap space

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.


Setting JRUBY_OPTS is the solution that worked best for me. It was mentioned in Koray's answer.

set JRUBY_OPTS=-J-Xmx2g

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.