There's unix::rlimit_as()
that allows setting memory limits for a running R process using the same mechanism that is also used for ulimit
in the shell. Windows and macOS not supported.
In my .Rprofile
I have
unix::rlimit_as(1e12, 1e12)
to limit memory usage to ~12 GB.
Before that...
I had created a small R package, ulimit
with similar functionality.
Install it from GitHub using
devtools::install_github("krlmlr/ulimit")
To limit the memory available to R to 2000 MiB, call:
ulimit::memory_limit(2000)
Now:
> rep(0L, 1e9)
Error: cannot allocate vector of size 3.7 Gb
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…