HOWTO · R

How to Increase the Memory for R Processes

This tutorial demonstrates how to increase memory in R.

On this page

When R reports that it cannot allocate memory, first distinguish the memory available to the computer from limits on the R process. R cannot use more physical memory than the system can provide, and an operating system or container may impose its own resource limits. A setting for R can change a process limit where that setting is supported; it does not add RAM or guarantee that a particular analysis will fit.

The available controls depend on the R version, operating system, and how R is launched. Check the startup documentation for the installed version before applying a setting. Record the exact error and identify the largest objects and operations in the workload; that information helps determine whether configuration, data size, or an intermediate copy is the main constraint.

Understand what limits R memory

An R process uses memory for objects, temporary results, packages, and runtime overhead. Operations such as joining tables, reshaping data, or converting types can need additional memory while the original objects remain live. A dataset that fits on disk may therefore require substantially more memory during a calculation.

There is no single “R memory” value that describes every limit. Physical RAM, available swap or virtual memory, process address space, and an R-specific heap limit are different constraints. The operating system and R build determine which constraints apply. Raising an R-specific limit cannot overcome a system limit, and a larger limit can allow a process to consume resources needed by other programs.

Increase memory for R on Windows

Do not apply the older Windows shortcut instructions from earlier R releases without checking that they match your installed version. In the R console, inspect R.version.string; then consult that version’s startup help for supported options. The R command-line documentation describes startup options; if the documentation for your installation supports --max-vsize, add the documented value to the command used to launch R. In a shortcut, this means appending the option after the quoted path to Rgui.exe, with a space between the closing quote and the option. Keep the existing executable path and any other required arguments intact.

The Windows Properties and Target workflow changes how that shortcut starts R; it does not increase the computer’s physical memory. If you use RStudio, a shortcut change for Rgui.exe does not necessarily configure the R session started by RStudio. Configure the launcher that actually starts the affected session, then restart that session for startup settings to take effect. Do not assume administrator privileges make an unsupported option effective.

Set supported limits when launching R

Use only a startup option or environment variable documented for your R version and platform. The R administration manual’s environment-variable reference documents R_MAX_VSIZE; consult it alongside the R command-line documentation for startup options. If the installed version documents --max-vsize and R_MAX_VSIZE, use them to configure the vector heap; otherwise, do not assume those names are supported. The value is a size, and the accepted syntax and units should come from that documentation. Set a startup variable before launching R; changing it inside an existing session does not reconfigure that session.

For a one-time POSIX shell launch, the pattern is R_MAX_VSIZE=value R, where value must use syntax documented for your installation. This is a pattern, not a configured limit to copy verbatim. On Windows, set a documented variable in the environment used to launch R, or pass the documented option to the relevant launcher. Avoid assuming that R_MAX_MEM_SIZE, --max-mem-size, or ulimit -v controls R’s vector heap: their support and effects differ by platform and release, and an operating-system limit can instead restrict the process.

Reclaim unused objects with gc()

R can reclaim memory occupied by objects that are no longer reachable. Remove references to large objects when they are no longer needed; gc() requests garbage collection and reports memory statistics. It cannot collect an object that is still referenced, raise a process limit, or guarantee that memory returned by R will immediately appear as free memory to the operating system.

Run this example:

values <- seq_len(100000)
total <- sum(values)
rm(values)
gc()
print(total)
         used (Mb) gc trigger (Mb) max used (Mb)
Ncells 273719 14.7     660948 35.3   452023 24.2
Vcells 459142  3.6    8388608 64.0  1800334 13.8
[1] 5000050000

The garbage-collection statistics vary by R build and session; the stable result of this example is the printed total. In normal scripts, R triggers garbage collection as needed; calling gc() after every operation is rarely useful and may add work. Keep the rm() call only when the object is no longer required elsewhere.

Verify the change and diagnose remaining out-of-memory errors

Start a fresh R process after changing a startup option or environment variable. Check the installed version with R.version.string and inspect gc() for current heap statistics. Compare the observed setting or statistics with the documentation for that version; a successful launch alone does not prove that a requested limit was accepted. Monitor system memory as well, because the operating system can still prevent allocation.

memory.limit() is a legacy Windows-specific function. The current R-devel utils memory-limits documentation marks it defunct; do not use it to increase memory when that status applies to your release. If working with an older release, check that release’s help before relying on it; the function does not apply as a general cross-platform setting. Running RStudio or R with elevated privileges does not restore support where the function is defunct.

If the error remains, note the full message, R version, platform, launcher, and the operation that failed. Look for a large temporary copy, remove objects that are no longer needed, process data in chunks, or use a representation that avoids loading all data at once. Consider increasing a supported vector-heap limit only when the system has sufficient resources and the relevant documentation confirms the option. No limit setting can guarantee that an out-of-memory error will disappear for every workload.