R: Increase heap size for rJava
Tagged:  •    •    •  

Technically speaking, this is not strictly a Unix Revelation. Still, to me it's useful enough to note this down.

We all know Java runs in a virtual machine, and assigns itself in 80% of the cases too little memory to do some real work. Result? Well, this:

java.lang.OutOfMemoryError: Java heap space

Sun gave us the -Xmx flag to increase the heap size (and -Xms to increase the stack size).

This works nicely from the commandline, the next challenge was to do something similar from an R environment. Using RWeka, which in turn depends on rJava, I found myself hitting the limit every time (which defaults to 512 MB).

The solution is to supply this -Xmx parameter before the Java Virtual Machine is initialized. From the R shell, invoke this:

> options( java.parameters = "-Xmx4g" )
> library( "RWeka" )

This will override the default setting and assign 4 gigabytes of heap space to the Java environment. Of course, you still need to have at least that amount of RAM.