Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 896
Christopher DuBois Avatar asked Jul 23 '09 03:07

Christopher DuBois


People also ask

How do I see memory usage in R?

Click the triangle drop-down and choose "Memory Usage Report" to see a breakdown of your memory usage in more detail. If your memory usage starts to approach your memory limit, then the indicator will turn from green to yellow, from yellow to orange, and eventually to red when you run out of memory.

Does R use a lot of memory?

R uses more memory probably because of some copying of objects. Although these temporary copies get deleted, R still occupies the space. To give this memory back to the OS you can call the gc function.

Does R have memory management?

R doesn't manage the memory of the machine.


2 Answers

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.

like image 144
David Smith Avatar answered Sep 23 '22 01:09

David Smith


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.

like image 27
Richie Cotton Avatar answered Sep 21 '22 01:09

Richie Cotton