Error: vector memory exhausted (limit reached?) R 3.5.0 macOS
For those using Rstudio, I've found that setting Sys.setenv('R_MAX_VSIZE'=32000000000)
only works on the command line, and that setting that parameter while using Rstudio does not prevent this error:
Error: vector memory exhausted (limit reached?)
After doing some more reading, I found this thread, which clarifies the problem with Rstudio, and identifies a solution, shown below:
Step 1: Open terminal,
Step 2:
cd ~
touch .Renviron
open .Renviron
Step 3: Save the following as the first line of .Renviron
:
R_MAX_VSIZE=100Gb
Note: This limit includes both physical and virtual memory; so setting _MAX_VSIZE=16Gb on a machine with 16Gb of physical memory may not prevent this error. You may have to play with this parameter, depending on the specs of your machine
A solution for those who might be unfamiliar with the command line can be found here:
In short, the solution is to use the usethis
package.
usethis::edit_r_environ()
will open the .Renviron which is in your home directory. This .Renviron affects all Rstudio work
usethis::edit_r_environ("project")
will open an .Renviron local to your project. Changes made to this file only affect work done in that particular Rstudio project.
Once open, the R_MAX_VSIZE
var can be set.
The linked page also links to this blog which describes R's startup process in great detail.
R 3.5 has a new system limit on for memory allocation. From the release notes:
The environment variable R_MAX_VSIZE can now be used to specify the maximal vector heap size. On macOS, unless specified by this environment variable, the maximal vector heap size is set to the maximum of 16GB and the available physical memory. This is to avoid having the R process killed when macOS over-commits memory.
You can override this. You risk overallocating and killing the process, but that is probably what was happening if you hit a hard wall with R 3.4.4 or whatever you were using before.
Execute the following in Terminal to create a temporary environmental variable R_MAX_VSIZE
with value 32GB (change to suit):
export R_MAX_VSIZE=32000000000
Or if you don't want to open Terminal and run that every time you want to start an R session, you can append the same line to your bash profile. Open Terminal and find your bash profile open .bash_profile
and, in a text editor, add the line from above.
You will still have to open Terminal and start R from there. You can run R in the terminal by just executing R
or you can open the GUI open -n /Applications/R.app
.
To make this change in an R session use Sys.setenv('R_MAX_VSIZE'=32000000000)
and to check the value use Sys.getenv('R_MAX_VSIZE')