Search code examples
javaout-of-memoryrstudiorjava

RStudio: Error in .jarray(m) : java.lang.OutOfMemoryError: Java heap space


To be able to run the package extraTrees (which i need to make extreme randomised trees) I needed to install the Rjava package in my RStudio. The code below works perfectly when i use it in a limited amount of data (1000 observations..) However i need to be able to run the code on a dataset of 50 000-100 000 observations, an iterate it around 50 times...

library(rJava)
library(extraTrees)

et <- extraTrees(Input, Output, ntree = 300 ,nodesize=3, numRandomCuts=2, numThreads= 4)

When I try to run this I get the following error:

 Error in .jarray(m) : java.lang.OutOfMemoryError: Java heap space 

Does anyone know how to fix it? I have been looking for an answer but didn't find how to fit it in a RStudio environment.


Solution

  • The Xmx flag controls the size of the Java runtime heap. You can increase this to a larger value which may allow your R code to run without hitting the ceiling:

    > options(java.parameters = "-Xmx4g")     # or 8g, or larger than this, ...
    

    Note that this should increase the heap only for the Java process called by your R script. Outside R, whatever heap size was being used by your Java should remain the same. You could also change it externally if you wanted to.

    I know about Xmx but did not know how to do this from within the R console. For that, I found this useful blog post:

    http://www.bramschoenmakers.nl/en/node/726