在向量化一些仿真代码的过程中,我遇到了内存问题。我正在Windows XP下使用32位R版本2.15.0(通过RStudio版本0.96.122)。我的机器有3.46 GB的RAM。
> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] Matrix_1.0-6 lattice_0.20-6 MASS_7.3-18
loaded via a namespace (and not attached):
[1] grid_2.15.0 tools_2.15.0
这是问题的最小示例:
> memory.limit(3000)
[1] 3000
> rm(list = ls())
> gc()
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 1069761 28.6 1710298 45.7 1710298 45.7
Vcells 901466 6.9 21692001 165.5 173386187 1322.9
> N <- 894993
> library(MASS)
> sims <- mvrnorm(n = N, mu = rep(0, 11), Sigma = diag(nrow = 11))
> sims <- mvrnorm(n = N + 1, mu = rep(0, 11), Sigma = diag(nrow = 11))
Error: cannot allocate vector of size 75.1 Mb
(在我的应用程序中,协方差矩阵Sigma不是对角线,但是无论哪种方式,我都会得到相同的误差。)
我花了整个下午阅读有关R中的内存分配问题的信息(包括here,here和here)。从我读过的书中,我得到的印象是,这与RAM本身无关,而与连续地址空间有关。不过,对我来说75.1Mb似乎很小。
如果您有任何想法或建议,我将不胜感激。
r 内存管理
月关宝盒