[R] How to free memory used by R.

Thomas W Blackwell tblackw at umich.edu
Mon Sep 1 19:38:24 CEST 2003

On Mon, 1 Sep 2003, Lariani,Sofiane,LAUSANNE,NRC/BAS wrote:

> I want to free memory used by R. The usage of rm and gc give no result. I'm
> running  an algorithm consuming a huge memory and I need to recover the
> memory used by R between 2 call of my algorithm.
> Thank you in advance for your help.
> e-mail: sofiane.lariani at rdls.nestle.com
> Sofiane Lariani

What operating system and what version of R, please ?

Other people will have a much better understanding of the
details here than I have, but here goes.

The fact that you ask this question suggests that you are
running the unnamed, memory intensive algorithm inside a loop.
It's my understanding that  gc()  is called automatically
every time control comes back to the command line prompt.
So, gc() is only an issue inside a loop.  You might TRY
writing a wrapper function:

my.algorithm <- function(...) {
   gc()           }

and call the wrapper rather than  algorithm()  directly,
inside the loop.  Chances are this won't help, because
the loop will try to keep intermediate results.
(At least it used to do this in old Splus prior to 1990.)

If nothing else works, do as we all used to do prior to 1990.
Write a batch script which does one iteration of the algorithm
and saves just the intermediate results you want.  Then write
a shell script (I assume you're using a version of unix) that
simply runs  R BATCH scriptname logfile  again and again, one
call for each iteration of the loop.  This guarantees that
nothing is saved except what you specify.  (It's clunky, yes,
but this works when nothing else will.)  Alternatively, you
may have access to a machine with more memory.

-  tom blackwell  -  u michigan medical school  -  ann arbor  -

More information about the R-help mailing list