How to set memory limit in RStudio (desktop version)?

r, rstudio

Solution

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.

Problem

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?

Original source

Related problems