RStan, error: cannot allocate vector of size 17075.9 Gb

Today I run a STAN model from R and I have no problems as usual; after some hours I run again the same model but it doesen’t work (all chains remain stopped at first iteration) so I run the school.stan example code and I have that problem that I’ve never seen.

This is my simple code:

> library(rstan)
Carico il pacchetto richiesto: StanHeaders
rstan (Version 2.21.2, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
Do not specify '-march=native' in 'LOCAL_CPPFLAGS' or a Makevars file

Attaching package: ‘rstan’

The following object is masked from ‘package:tidyr’:

    extract

> 
> options(mc.cores = parallel::detectCores())
> rstan_options(auto_write = TRUE)
> 
> schools_dat <- list(J = 8,
+                    y = c(28,  8, -3,  7, -1,  1, 18, 12),
+                    sigma = c(15, 10, 16, 11,  9, 11, 10, 18))
> fit <- stan(file = 'STAN/schools.stan', data = schools_dat,
+             chains = 3)
Errore: cannot allocate vector of size 17075.9 Gb
Inoltre: Warning message:
In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found

Please, has someone any help or any ideas to solve the problem?

Can you post the output from:

Sys.getenv("BINPREF")
Sys.getenv("PATH")
readLines("~/.Rprofile")
readLines("~/.Renviron")
readLines("~/.R/Makevars.win")

Also, are you using R3.x or R4.x?

This is the output of these lines of code:

> Sys.getenv("BINPREF")
[1] ""
> Sys.getenv("PATH")
[1] "C:\\rtools40\\usr\\bin;C:\\Program Files\\R\\R-4.0.3\\bin\\x64;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Program Files\\Git\\cmd;C:\\Users\\basil\\AppData\\Local\\Microsoft\\WindowsApps"
> readLines("~/.Rprofile")
Error in file(con, "r") : non posso aprire questa connessione
Inoltre: Warning message:
In file(con, "r") :
  cannot open file 'C:/Users/basil/Documents/.Rprofile': No such file or directory
> readLines("~/.Renviron")
[1] "PATH=\"${RTOOLS40_HOME}\\usr\\bin;${PATH}\""
> readLines("~/.R/Makevars.win")
[1] ""                                                                                    
[2] " CXX14FLAGS += -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2"

i tried to reinstall Rcpp and Rstan but nothing change

This is my R version:

> version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          4                           
minor          0.3                         
year           2020                        
month          10                          
day            10                          
svn rev        79318                       
language       R                           
version.string R version 4.0.3 (2020-10-10)
nickname       Bunny-Wunnies Freak Out    

Have you recently updated from R3.x?

What output do you get from:

devtools::session_info("rstan")

No, the first version of R I intalled on this pc was the 4.0.3 because I switch on this machine the first time 2 month ago.

This is the output of devtools::session_info(“rstan”)

> devtools::session_info("rstan")
- Session info -------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 4.0.3 (2020-10-10)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       RStudio                     
 language (EN)                        
 collate  Italian_Italy.1252          
 ctype    Italian_Italy.1252          
 tz       Europe/Berlin               
 date     2021-03-07                  

- Packages -----------------------------------------------------------------------------------------------------------
 ! package      * version   date       lib source        
   assertthat     0.2.1     2019-03-21 [1] CRAN (R 4.0.3)
   backports      1.2.0     2020-11-02 [1] CRAN (R 4.0.3)
   BH             1.72.0-3  2020-01-08 [1] CRAN (R 4.0.3)
   brio           1.1.0     2020-08-31 [1] CRAN (R 4.0.3)
   callr          3.5.1     2020-10-13 [1] CRAN (R 4.0.3)
   checkmate      2.0.0     2020-02-06 [1] CRAN (R 4.0.3)
   cli            2.1.0     2020-10-12 [1] CRAN (R 4.0.3)
   colorspace     2.0-0     2020-11-11 [1] CRAN (R 4.0.3)
   crayon         1.3.4     2017-09-16 [1] CRAN (R 4.0.3)
   curl           4.3       2019-12-02 [1] CRAN (R 4.0.3)
   desc           1.2.0     2018-05-01 [1] CRAN (R 4.0.3)
   diffobj        0.3.2     2020-10-05 [1] CRAN (R 4.0.3)
   digest         0.6.27    2020-10-24 [1] CRAN (R 4.0.3)
   ellipsis       0.3.1     2020-05-15 [1] CRAN (R 4.0.3)
   evaluate       0.14      2019-05-28 [1] CRAN (R 4.0.3)
   fansi          0.4.1     2020-01-08 [1] CRAN (R 4.0.3)
   farver         2.0.3     2020-01-16 [1] CRAN (R 4.0.3)
   ggplot2        3.3.2     2020-06-19 [1] CRAN (R 4.0.3)
   glue           1.4.2     2020-08-27 [1] CRAN (R 4.0.3)
   gridExtra      2.3       2017-09-09 [1] CRAN (R 4.0.3)
   gtable         0.3.0     2019-03-25 [1] CRAN (R 4.0.3)
   inline         0.3.16    2020-09-06 [1] CRAN (R 4.0.3)
   isoband        0.2.2     2020-06-20 [1] CRAN (R 4.0.3)
   jsonlite       1.7.2     2020-12-09 [1] CRAN (R 4.0.3)
   labeling       0.4.2     2020-10-20 [1] CRAN (R 4.0.3)
   lattice        0.20-41   2020-04-02 [2] CRAN (R 4.0.3)
   lifecycle      0.2.0     2020-03-06 [1] CRAN (R 4.0.3)
   loo            2.3.1     2020-07-14 [1] CRAN (R 4.0.3)
   magrittr       1.5       2014-11-22 [1] CRAN (R 4.0.3)
   MASS           7.3-53    2020-09-09 [2] CRAN (R 4.0.3)
   Matrix         1.2-18    2019-11-27 [2] CRAN (R 4.0.3)
   matrixStats    0.57.0    2020-09-25 [1] CRAN (R 4.0.3)
   mgcv           1.8-33    2020-08-27 [2] CRAN (R 4.0.3)
   munsell        0.5.0     2018-06-12 [1] CRAN (R 4.0.3)
   nlme           3.1-149   2020-08-23 [2] CRAN (R 4.0.3)
   pillar         1.4.6     2020-07-10 [1] CRAN (R 4.0.3)
   pkgbuild       1.1.0     2020-07-13 [1] CRAN (R 4.0.3)
   pkgconfig      2.0.3     2019-09-22 [1] CRAN (R 4.0.3)
   pkgload        1.1.0     2020-05-29 [1] CRAN (R 4.0.3)
   praise         1.0.0     2015-08-11 [1] CRAN (R 4.0.3)
   prettyunits    1.1.1     2020-01-24 [1] CRAN (R 4.0.3)
   processx       3.4.4     2020-09-03 [1] CRAN (R 4.0.3)
   ps             1.4.0     2020-10-07 [1] CRAN (R 4.0.3)
   R6             2.5.0     2020-10-28 [1] CRAN (R 4.0.3)
   RColorBrewer   1.1-2     2014-12-07 [1] CRAN (R 4.0.3)
   Rcpp           1.0.6     2021-01-15 [1] CRAN (R 4.0.4)
   RcppEigen      0.3.3.9.1 2020-12-17 [1] CRAN (R 4.0.3)
 D RcppParallel   5.0.2     2020-06-24 [1] CRAN (R 4.0.3)
   rematch2       2.1.2     2020-05-01 [1] CRAN (R 4.0.3)
   rlang          0.4.10    2020-12-30 [1] CRAN (R 4.0.4)
   rprojroot      1.3-2     2018-01-03 [1] CRAN (R 4.0.3)
   rstan          2.21.2    2020-07-27 [1] CRAN (R 4.0.4)
   rstudioapi     0.13      2020-11-12 [1] CRAN (R 4.0.3)
   scales         1.1.1     2020-05-11 [1] CRAN (R 4.0.3)
   StanHeaders    2.21.0-7  2020-12-17 [1] CRAN (R 4.0.3)
   testthat       3.0.0     2020-10-31 [1] CRAN (R 4.0.3)
   tibble         3.0.4     2020-10-12 [1] CRAN (R 4.0.3)
   utf8           1.1.4     2018-05-24 [1] CRAN (R 4.0.3)
   V8             3.4.0     2020-11-04 [1] CRAN (R 4.0.3)
   vctrs          0.3.4     2020-08-29 [1] CRAN (R 4.0.3)
   viridisLite    0.3.0     2018-02-01 [1] CRAN (R 4.0.3)
   waldo          0.2.3     2020-11-09 [1] CRAN (R 4.0.3)
   withr          2.3.0     2020-09-22 [1] CRAN (R 4.0.3)

[1] C:/Users/basil/Documents/R/win-library/4.0
[2] C:/Program Files/R/R-4.0.3/library

 D -- DLL MD5 mismatch, broken installation.

There is a problem in RcppParallel, might be usefull to reinstall?

That RcppParallel is normal, safe to ignore. What happens when you run the rstan example model:

example(stan_model,package="rstan",run.dontrun=T)

It gives me a compilation error

> example(stan_model,package="rstan",run.dontrun=T)
Loading required package: StanHeaders
Loading required package: ggplot2
rstan (Version 2.21.2, GitRev: 2e1f913d3ca3)
For execution on a local, multicore CPU with excess RAM we recommend calling
options(mc.cores = parallel::detectCores()).
To avoid recompilation of unchanged Stan programs, we recommend calling
rstan_options(auto_write = TRUE)
Do not specify '-march=native' in 'LOCAL_CPPFLAGS' or a Makevars file

stn_md> stancode <- 'data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}'

stn_md> mod <- stan_model(model_code = stancode, verbose = TRUE)

TRANSLATING MODEL '73fc79f8b1915e8208c736914c86d1a1' FROM Stan CODE TO C++ CODE NOW.
successful in parsing the Stan model '73fc79f8b1915e8208c736914c86d1a1'.
COMPILING THE C++ CODE FOR MODEL '73fc79f8b1915e8208c736914c86d1a1' NOW.
OS: x86_64, mingw32; rstan: 2.21.2; Rcpp: 1.0.6; inline: 0.3.16 
 >> setting environment variables: 
LOCAL_LIBS =  "C:/Users/basil/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a" -L"C:/Users/basil/Documents/R/win-library/4.0/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/basil/Documents/R/win-library/4.0/RcppParallel/lib/x64" -ltbb
PKG_CPPFLAGS =   -I"C:/Users/basil/Documents/R/win-library/4.0/Rcpp/include/"  -I"C:/Users/basil/Documents/R/win-library/4.0/RcppEigen/include/"  -I"C:/Users/basil/Documents/R/win-library/4.0/RcppEigen/include/unsupported"  -I"C:/Users/basil/Documents/R/win-library/4.0/BH/include" -I"C:/Users/basil/Documents/R/win-library/4.0/StanHeaders/include/src/"  -I"C:/Users/basil/Documents/R/win-library/4.0/StanHeaders/include/"  -I"C:/Users/basil/Documents/R/win-library/4.0/RcppParallel/include/"  -I"C:/Users/basil/Documents/R/win-library/4.0/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include "C:/Users/basil/Documents/R/win-library/4.0/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp"  -std=c++1y

 >> Program source :
....

make cmd is
  make -f "C:/PROGRA~1/R/R-40~1.3/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-40~1.3/share/make/winshlib.mk" -f "C:/Users/basil/Documents/.R/Makevars.win" CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB="file3b987a8828c8.dll" WIN=64 TCLBIN=64 OBJECTS="file3b987a8828c8.o"

make would use
"C:/rtools40/mingw64/bin/"g++  -std=gnu++14 -I"C:/PROGRA~1/R/R-40~1.3/include" -DNDEBUG   -I"C:/Users/basil/Documents/R/win-library/4.0/Rcpp/include/"  -I"C:/Users/basil/Documents/R/win-library/4.0/RcppEigen/include/"  -I"C:/Users/basil/Documents/R/win-library/4.0/RcppEigen/include/unsupported"  -I"C:/Users/basil/Documents/R/win-library/4.0/BH/include" -I"C:/Users/basil/Documents/R/win-library/4.0/StanHeaders/include/src/"  -I"C:/Users/basil/Documents/R/win-library/4.0/StanHeaders/include/"  -I"C:/Users/basil/Documents/R/win-library/4.0/RcppParallel/include/"  -I"C:/Users/basil/Documents/R/win-library/4.0/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DBOOST_NO_AUTO_PTR  -include "C:/Users/basil/Documents/R/win-library/4.0/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp"  -std=c++1y         -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -c file3b987a8828c8.cpp -o file3b987a8828c8.o
if test "zfile3b987a8828c8.o" != "z"; then \
  if test -e "file3b987a8828c8-win.def"; then \
    echo "C:/rtools40/mingw64/bin/"g++  -shared -s -static-libgcc -o file3b987a8828c8.dll file3b987a8828c8-win.def file3b987a8828c8.o   "C:/Users/basil/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a" -L"C:/Users/basil/Documents/R/win-library/4.0/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/basil/Documents/R/win-library/4.0/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-40~1.3/bin/x64" -lR ; \
    "C:/rtools40/mingw64/bin/"g++  -shared -s -static-libgcc -o file3b987a8828c8.dll file3b987a8828c8-win.def file3b987a8828c8.o   "C:/Users/basil/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a" -L"C:/Users/basil/Documents/R/win-library/4.0/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/basil/Documents/R/win-library/4.0/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-40~1.3/bin/x64" -lR ; \
  else \
    echo EXPORTS > tmp.def; \
    "C:/rtools40/mingw64/bin/"nm file3b987a8828c8.o | sed -n 's/^.* [BCDRT] / /p' | sed -e '/[.]refptr[.]/d' -e '/[.]weak[.]/d' | sed 's/[^ ][^ ]*/"&"/g'  >> tmp.def; \
    echo "C:/rtools40/mingw64/bin/"g++  -shared -s -static-libgcc -o file3b987a8828c8.dll tmp.def file3b987a8828c8.o   "C:/Users/basil/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a" -L"C:/Users/basil/Documents/R/win-library/4.0/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/basil/Documents/R/win-library/4.0/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-40~1.3/bin/x64" -lR ; \
    "C:/rtools40/mingw64/bin/"g++  -shared -s -static-libgcc -o file3b987a8828c8.dll tmp.def file3b987a8828c8.o   "C:/Users/basil/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a" -L"C:/Users/basil/Documents/R/win-library/4.0/StanHeaders/libs/x64" -lStanHeaders -L"C:/Users/basil/Documents/R/win-library/4.0/RcppParallel/lib/x64" -ltbb  -L"C:/PROGRA~1/R/R-40~1.3/bin/x64" -lR ; \
    rm -f tmp.def; \
  fi \
fi

ERROR(s) during compilation: source code errors or compiler configuration errors!

 >> Program source :
...

Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) : 
  C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/basil/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a(stan_fit.o):stan_fit.cpp:(.rdata$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1]+0x0): multiple definition of `boost::math::detail::bessel_j0<long double>(long double)::P1'; file3b987a8828c8.o:file3b987a8828c8.cpp:(.data$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2P1]+0x0): first defined hereC:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/basil/Documents/R/win-library/4.0/rstan/lib/x64/libStanServices.a(stan_fit.o):stan_fit.cpp:(.rdata$_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2QC[_ZZN5boost4math6detail9bessel_j0IeEET_S3_E2QC]+0x0): multiple definition of `boost::math::detail::bessel_j0<long double>(long double)::QC'; file3b987a8828c8.o:file3b987a88
Inoltre: Warning messages:
1: package ‘rstan’ was built under R version 4.0.4 
2: In system(paste(CXX, ARGS), ignore.stdout = TRUE, ignore.stderr = TRUE) :
  'C:/rtools40/usr/mingw_/bin/g++' not found

Alright, so there are two issues here. The Unable to allocate vector... error was (I’m guessing) caused by a .rds file leftover from a previous rstan install. If you look in the folder where the schools.stan file is stored, there should also be a file called schools.rds. Delete that .rds file.

For the compilation errors in the example model, you need to restart your R session (making sure that rstan is not loaded) and then reinstall rstan and StanHeaders from source:

# Compile packages using all cores
Sys.setenv(MAKEFLAGS = paste0("-j",parallel::detectCores()))

install.packages(c("StanHeaders","rstan"),type="source")
1 Like

Thanks! I finally solve the problem: I delete all *.rds file reinstall from source rstan and StanHeaders and now it works perfectly as before!

2 Likes