Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i get R to release and stop holding memory even after all objects have been removed and cleared?

i am trying to get some code working on computers with less than 4GB of RAM. i am using the 32-bit version of R to enforce that memory ceiling. i'm hitting a wall near the end of the script when i'm trying to run a memory-hogging command and everything breaks. but the memory-hogging task by itself requires less than 4GB. i've narrowed down the problem to the fact that - despite clearing all objects from memory in the current session - the R console is still holding 1.9GB of RAM. the screenshot below highlights exactly where i'm hitting the problem: note there are zero objects in memory and yet task manager says this instance of R has 1.8578GB of RAM held.

if i clear all objects from memory and then run gc() that still does not clear all memory held (as you can see in my screenshot).

is it possible to clear this memory held somehow?

if it's of any use, you can reproduce this up to the point of the crash by running this script

thank you!

enter image description here

edit: at the end of the script i get

[1] "current designing ./2011/bst.rda"
Error: cannot allocate vector of size 434.7 Mb
In addition: There were 50 or more warnings (use warnings() to see the first 50)
> gc(verbose=T)
Garbage collection 27232 = 15350+4362+7520 (level 2) ... 
31.5 Mbytes of cons cells used (49%)
450.6 Mbytes of vectors used (21%)
           used  (Mb) gc trigger   (Mb)  max used   (Mb)
Ncells  1175911  31.5    2421436   64.7   1770749   47.3
Vcells 59048650 450.6  278146328 2122.1 461815808 3523.4
> rm(list=ls(all=TRUE))
> gc(verbose=T)
Garbage collection 27233 = 15350+4362+7521 (level 2) ... 
11.1 Mbytes of cons cells used (21%)
7.1 Mbytes of vectors used (0%)
         used (Mb) gc trigger   (Mb)  max used   (Mb)
Ncells 414283 11.1    1937148   51.8   1770749   47.3
Vcells 920035  7.1  222517062 1697.7 461815808 3523.4
> 
like image 754
Anthony Damico Avatar asked Aug 20 '15 04:08

Anthony Damico


People also ask

How do I free up unused R memory?

You can force R to perform this check, and free the memory right away, by running the gc() command in R or going to Tools -> Memory -> Free Unused R Memory.

Why is R taking up so much 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. However, when the memory is needed, gc is called automatically.

Can R run out of memory?

Windows users may get the error that R has run out of memory. If you have R already installed and subsequently install more RAM, you may have to reinstall R in order to take advantage of the additional capacity.

What does gc () do in R?

R uses an alternative approach: garbage collection (or GC for short). GC automatically releases memory when an object is no longer used. It does this by tracking how many names point to each object, and when there are no names pointing to an object, it deletes that object.

How to clear memory in R with RM?

The command rm (list=ls ()) is expected to release the memory used by all objects, but what it really does is to destroy the pointers to the used memory chunks. The problem is those memory chunks are not immediately freed-up for use by new tasks. Clear Memory in R With the gc Function

Why is my R memory not freed-up after restarting?

Even if you restart your R environment, it can happen that the memory isn’t freed-up. The command rm (list=ls ()) is expected to release the memory used by all objects, but what it really does is to destroy the pointers to the used memory chunks. The problem is those memory chunks are not immediately freed-up for use by new tasks.

Is it possible to use RStudio to clean up the memory management?

No, having RStudio meddle with R's internal memory accounting would cause instability. You can configure R's memory manager to collect garbage more aggressively and frequently if you need this sort of behavior. Read Memory Management in R for more details.

How to clear memory and boost RAM Windows 7?

7 Ways to Clear Memory and Boost RAM on Windows. 1 1. RAM Hogs: Low Hanging Fruit. The fastest and easiest way to clear up memory that’s being used is to make sure there are no system processes ... 2 2. Clean Up Startup Programs. 3 3. Clear Page File at Shutdown. 4 4. Check for Device Driver Issues. 5 5. Reduce Windows Visual Effects. More items


1 Answers

This is not only for R but for Windows in general. Normally, if you have removed a variable/ object in R. The process does release the memory to OS, but due to working of Windows. That memory is not released totally, it's kept in case the process request memory again so you will see as if R is still holding all that memory.

So please don't worry, that is kept for reuse :)

like image 127
suhao399 Avatar answered Sep 30 '22 11:09

suhao399