25

I'm aware that there has already been a similar question here, but the answer is out-of-date. The information I've found in the internet refers to RStudio Server rather than Desktop.

I have limited resources on my Windows 7 x64 PC. I've set the environment variable R_MAX_MEM_SIZE and this is accepted by the RGui (as seen when typing memory_limit()). However, RStudio (Version 0.98.507) ignores this setting and still uses the entire memory of my computer which sometimes leads to crashes of the whole system. How can I limit the amount of memory used by R when running it in RStudio Desktop?

Community
  • 1
  • 1
AnjaM
  • 2,941
  • 8
  • 39
  • 62
  • Right click on properties of the Shortcut and Select Shortcut. Then in Targed field add in after rstudio.exe --max-mem-size=4GB E.g. \RStudio\bin\rstudio.exe --max-mem-size=4GB For limit of 4GB – Pork Chop May 30 '14 at 08:55
  • @pops This doesn't change the output of `memory.limit()`. I guess the reason is that it applies to RStudio itself rather than to the `R` process? So in that case if the process uses more than 4GB, RStudio would crash? – AnjaM May 30 '14 at 09:02
  • You might have a quick scan here http://cran.r-project.org/bin/windows/base/rw-FAQ.html The reason I don't post my comments as an answer is because Im not sure how it will handle the processes pass the limit you set up. My first guess is going to be that its going to assign all the consecutive processes onto hard disc, but I dont know for sure as you still have some RAM available for your processes. You're going to get an error like ' error that R cannot allocate a vector of length x' Look into this thread too – Pork Chop May 30 '14 at 09:12
  • Heres the thread http://stackoverflow.com/questions/1395229/increasing-the-memory-available-to-r-processes – Pork Chop May 30 '14 at 09:13
  • 1
    you can use cmd and then go to R bin directory C:\Program Files\RStudio\bin then start rstudio.exe --max-mem-size=4GB – rischan May 30 '14 at 09:19
  • @pops That kind of error would be perfectly fine. It also appears in `RGui` when the memory limit I have set within the environment variable is reached and that's exactly what I want to get: An error message instead of a complete crash. – AnjaM May 30 '14 at 09:21
  • 1
    @rischan Isn't it the same as what pops suggested except that I would need to do this each time I start RStudio? And would this just lead to a crash/error of the R session or of whole RStudio? – AnjaM May 30 '14 at 09:23
  • What do you mean crash? R/Rstudio shouldn't crash just because it runs out of memory. – hadley May 30 '14 at 14:29
  • 2
    @hadley I had several OS crashes in the last few days because R occupied the whole RAM when I run particular pieces of code in RStudio. After setting the environment variable and executing the same code in RGui, R stopped with the error message "cannot allocate a vector...". However, if I run exactly the same code in RStudio, this message doesn't appear but instead the OS stops reacting. – AnjaM May 30 '14 at 18:02
  • @AnjaM please file a bug report at https://support.rstudio.com/hc/en-us. – hadley Jun 02 '14 at 22:28
  • 2
    @hadley Done: https://support.rstudio.com/hc/communities/public/questions/202154107-Bug-Report-RStudio-Desktop-ignores-environment-variable-R-MAX-MEM-SIZE – AnjaM Jun 03 '14 at 06:49
  • Is it just me or is it extreme arrogance that R will not let me reduce the memory allocation limit. Messages such as this "In memory.limit(3) : cannot decrease memory limit: ignored" are asinine. – Chris Dec 16 '15 at 23:43
  • 1
    Just to let you know, support.rstudio.com on 2014/06/10: "We've got it on our list of things to investigate and hope to have a solution soon" - 2016/08/01, RStudio is still not picking up R_MAX_MEM_SIZE. – m-dz Aug 01 '16 at 09:03
  • I wonder if the inference that "the OS stopped working" might be a misinterpretation of the OS paging out to virtual memory? – IRTFM Apr 16 '17 at 23:07
  • You said your PC is x64bits but is your R version x64bits? – Nakx Jul 06 '18 at 00:56

3 Answers3

4

If your question is how to prevent R from crashing when it reaches the memory limit rather than figuring out why memory_limit() does not work, here are a few options.

If memory_limit() does not allow you to limit the memory on Windows:

  • Check that your version of Windows matches your version of R (not Rstudio). I.e., if you have a windows x64bits, check that Rstudio is running on R x64bits. This can be done using Sys.getenv("R_ARCH").
  • Clean your environment with rm(list=ls()) to get rid of previously stored datasets and functions.
  • Close Rstudio and R. Press WINDOWS + R, this opens a Run window. Write cmd and press enter. Navigate to this directory C:\Program Files\RStudio\bin then start rstudio.exe using cd. You may need to adapt this depending on where your RStudio folder is located on your computer. Then write --max-mem-size=4GB and press enter. You will need to repeat this every time you want to start an R session. This may not work on every computer.
  • Most computers can handle more memory than what is previously installed. Check what is the maximum memory capacity that your computer can handle, and consider buying more physical RAM.
  • Press Ctrl+Alt+Del and select task manager. Under Processes, check which program is using significant amounts of memory and whether you can end them safely. Windows users tend to accumulate unnecessary programs.
Tim Bainbridge
  • 177
  • 2
  • 9
Nakx
  • 1,460
  • 1
  • 23
  • 32
4

This works for my PC :

 # Check memory limit
   memory.limit()
 # Change memory limit
   memory.limit(size = 15000)
adjustedR2
  • 161
  • 4
0

If I'm not mistaken, by default the limit on Windows is 4GB. You cans use the cmd proposed by @rischan but with minor modification : rstudio.exe --max-mem-size=8GB.

For information do not forget to clean the cache when starting the execution of a new script. For this use the following cmd :

    rm(list=ls())  --> for clean your environment
    gc() --> for launch the ''garbage collection''
Nivek
  • 55
  • 9