views:

354

answers:

3
+3  Q: 

Memory Usage in R

After creating large objects and running out of RAM, I will try and delete the objects in my current environment using

rm(list=ls())

When I check my RAM usage, nothing has changed. Even after calling gc() nothing has changed. I can only replenish my RAM by quitting R.

Anybody have advice for dealing with memory-intensive objects within R?

A: 

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.

Argalatyr
+2  A: 

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.

Richie Cotton
This is how it behaves on my end. I'm running R 2.8.1 on Windows XP.
ars
+2  A: 

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.

David Smith