Running the following two lines of code (before any packages are loaded) worked for me on a Mac:
options(java.parameters = c("-XX:+UseConcMarkSweepGC", "-Xmx8192m"))
gc()
This essentially combines two proposals previously posted herein: Importantly, only running the first line alone (as suggested by drmariod) did not solve the problem in my case. However, when I was additionally executing gc()
just after the first line (as suggested by user2961057) the problem was solved.
Should it still not work, restart your R session, and then try (before any packages are loaded) instead options(java.parameters = "-Xmx8g")
and directly after that execute gc()
. Alternatively, try to further increase the RAM from "-Xmx8g"
to e.g. "-Xmx16g"
(provided that you have at least as much RAM).
EDIT: Further solutions: While I had to use the rJava for model estimations in R (explaining y from a large number of X), I kept receiving the above 'OutOfMemory' Errors even if I scaled up to "-Xmx60000m"
(the machine I am using has 64 GB RAM). The problem was that some model specifications were simply too big (and would have required even more RAM). One solution that may help in this case is scaling the size of the problem down (e.g. by reducing the number of X's in the model), or – if possible – splitting the problem into independent pieces, estimating each separately, and putting those pieces together again.
options(java.parameters = "-Xmx8g")
before starting up your Java instance. So start in a fresh R session with NO packages loaded. Run that command and THEN load all your packages and try again. You should be fine but it's possible the JVM needs a lot for other reasons. – Bandog