Graeme Frost
Graeme Frost

Reputation: 2643

Error: vector memory exhausted (limit reached?) R 3.5.0 macOS

I've been working for a while with a number of large files containing gene expression data, and I've recently run into an issue with loading that data into R, after upgrading to R 3.5.0. After using about 8GB of memory (my mac has 16GB of RAM), if I try to read in another file, I get the following error:

Error: vector memory exhausted (limit reached?)

I found a previous post (Error: vector memory exhausted (limit reached?)) suggesting I try to set the environmental variable R_MAX_VSIZE to a higher value, so I tried the following:

Sys.setenv(R_MAX_VSIZE = 16e9)

However, I still got the same error. Am I not setting the environmental Variable correctly? is there something that I'm missing?

Session info:

R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.5

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages: [1] data.table_1.11.4

loaded via a namespace (and not attached):
[1] compiler_3.5.0 tools_3.5.0   

Upvotes: 34

Views: 72782

Answers (6)

Graeme Frost
Graeme Frost

Reputation: 2643

For those using Rstudio, I've found that setting Sys.setenv('R_MAX_VSIZE'=32000000000), as has been suggested on multiple StackOverflow posts, 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 

Step 4: Close RStudio and reopen

Note: This limit includes both physical and virtual memory; so setting R_MAX_VSIZE to 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.

Upvotes: 175

Berent Lunde
Berent Lunde

Reputation: 79

I had this problem when running Rcpp::sourceCpp("my_cpp_file.cpp"), resulting in

Error: vector memory exhausted (limit reached?)

changing the Makevars file solved it for me. Currently it looks like

CC=gcc
CXX=g++
CXX11=g++
CXX14=g++
cxx18=g++
cxx1X=g++
LDFLAGS=-L/usr/lib 

Upvotes: 0

Purrsia
Purrsia

Reputation: 896

This can be done through R studio as well.

library(usethis) 
usethis::edit_r_environ()

when the tab opens up in R studio, add this to the 1st line: R_MAX_VSIZE=100Gb (or whatever memory you wish to allocate).

Re-start R and/or restart computer and run the R command again that gave you the memory error.

Upvotes: 49

Ömer An
Ömer An

Reputation: 664

I had the same problem, increasing the "R_MAX_VSIZE" did not help in my case, instead cleaning the variables no longer needed solved the problem. Hope this helps those who are struggling here.

rm(large_df, large_list, large_vector, temp_variables)

Upvotes: 19

Mir Henglin
Mir Henglin

Reputation: 669

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.

Upvotes: 11

Connor Dibble
Connor Dibble

Reputation: 595

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')

Upvotes: 15

Related Questions