Memory {base} | R Documentation |
Use command line options to control the memory available for R.
R --min-vsize=vl --max-vsize=vu --min-nsize=nl --max-nsize=nu \ --max-ppsize=N mem.limits(nsize = NA, vsize = NA)
vl, vu, vsize |
Heap memory in bytes. |
nl, nu, nsize |
Number of cons cells. |
N |
Number of nested |
R has a variable-sized workspace. These limits are mainly
historical, and now used only exceptionally. They were provided both
as a way to control the overall memory usage (which can be done better
by operating-system facilities such as ulimit
or limit
in a Unix-alike shell or by using the command-line option
--max-mem-size on Windows), and since setting larger values
of the minimum sizes will make R slightly more efficient on large
tasks.
To understand the options, one needs to know that R maintains
separate areas for fixed and variable sized objects. The first of these
is allocated as an array of cons cells (Lisp programmers will
know what they are, others may think of them as the building blocks of
the language itself, parse trees, etc.), and the second are thrown on a
heap of ‘Vcells’ of 8 bytes each. Effectively,
the inputs vl
and vu
are rounded up to the next
multiple of 8.
Each cons cell occupies 28 bytes on a 32-bit build of R, (usually) 56 bytes on a 64-bit build.
The --*-nsize options can be used to specify the number of
cons cells and the --*-vsize options specify the size of the
vector heap in bytes. Both options must be integers or integers
followed by G
, M
, K
, or k
meaning
Giga (2^{30} = 1073741824) Mega (2^{20} =
1048576), (computer) Kilo (2^{10} = 1024), or regular
kilo (1000).
The --min-* options set the ‘minimal’ sizes for the number of cons cells and for the vector heap. These values are also the initial values, but thereafter R will grow or shrink the areas depending on usage, but never exceeding the limits set by the --max-* options nor decreasing below the initial values. Note that the areas are not actually allocated initially: rather these values are the sizes for triggering garbage collection.
The default values are currently minima of 350k cons cells, 6Mb of
vector heap and no maxima (other than machine resources). The maxima
can be increased during an R session by calling
mem.limits
. (If this is called with the default values,
it reports the current settings.) Setting values larger than the
maximum allowed value (e.g. Inf
) removes the corresponding limit.
You can find out the current memory consumption (the heap and cons
cells used as numbers and megabytes) by typing gc()
at the
R prompt. Note that following gcinfo(TRUE)
, automatic
garbage collection always prints memory use statistics. Maxima will
never be reduced below the current values for triggering garbage
collection, and attempts to do so will be silently ignored.
The command-line option --max-ppsize controls the maximum size of the pointer protection stack. This defaults to 50000, but can be increased to allow deep recursion or large and complicated calculations to be done. Note that parts of the garbage collection process goes through the full reserved pointer protection stack and hence becomes slower when the size is increased. Currently the maximum value accepted is 500000.
mem.limits()
returns a numeric vector giving the current
settings of the maxima, possibly NA
(for unlimited).
An Introduction to R for more command-line options.
Memory-limits
for the design limitations.
gc
for information on the garbage collector and total
memory usage, object.size(a)
for the (approximate)
size of R object a
. memory.profile
for
profiling the usage of cons cells.