Christopher DuBois
Christopher DuBois

Reputation: 43440

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?

Upvotes: 28

Views: 16064

Answers (4)

Jacob Socolar
Jacob Socolar

Reputation: 1202

An old question, I realize, but I've found that (on OS Mojave), invoking pryr::mem_used() in the R session causes the activity monitor to immediately update the reported memory usage to reflect only the objects retained in the R environment.

Upvotes: 2

David Smith
David Smith

Reputation: 2166

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.

Upvotes: 21

Richie Cotton
Richie Cotton

Reputation: 121077

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.

Upvotes: 9

Argalatyr
Argalatyr

Reputation: 4659

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.

Upvotes: 3

Related Questions