Memory Usage in R

My impression is that multiple forms of gc() are tried before R reports failed memory allocation. I'm not aware of a solution for this at present, other than restarting R as you suggest. It appears that R does not defragment memory.


Memory for deleted objects is not released immediately. R uses a technique called "garbage collection" to reclaim memory for deleted objects. Periodically, it cycles through the list of accessible objects (basically, those that have names and have not been deleted and can therefore be accessed by the user), and "tags" them for retention. The memory for any untagged objects is returned to the operating system after the garbage-collection sweep.

Garbage collection happens automatically, and you don't have any direct control over this process. But you can force a sweep by calling the command gc() from the command line.

Even then, on some operating systems garbage collection might not reclaim memory (as reported by the OS). Older versions of Windows, for example, could increase but not decrease the memory footprint of R. Garbage collection would only make space for new objects in the future, but would not reduce the memory use of R.


On Windows, the technique you describe works for me. Try the following example.

Open the Windows Task Manager (CTRL+SHIFT+ESC).

Start RGui. RGui.exe mem usage is 27 460K.

Type

gcinfo(TRUE)
x <- rnorm(1e8)

RGui.exe mem usage is now 811 100K.

Type rm("x"). RGui.exe mem usage is still 811 100K.

Type gc(). RGui.exe mem usage is now 28 332K.

Note that gc shoud be called automatically if you have removed objects from your workspace, and then you try to allocate more memory to new variables.