[R-SIG-Mac] How to free up system memory?

Ben Haller rsigmac at sticksoftware.com
Tue May 3 01:36:31 CEST 2011


On May 2, 2011, at 7:28 PM, Carl Witthoft wrote:

> I'm not sure exactly what's happening, so I'll try to describe the steps in detail.  I get the same behavior with R 2.12.1 and R 2.13, OSX 10.6.6 or 10.6.4 .
> 
> First, I boot up Activity Monitor and set it to show memory usage.
> 
> I create a large object (in the R Console of R.app GUI) , such as
> 
> foo<-runif(1e8)
> 
> and the "real memory" in the table and "active memory" in the pie chart go up.  No surprise.
> 
> Then I get rid of foo,
> 
> rm(foo)
> 
> But the memory allocations don't change.   Looks like a memory leak?

  Unlikely.  Much more likely, garbage collection simply hasn't cleaned up the memory yet.  Read "?gc".

> Two other things I found:
> 
> 1) If I write a function that does nothing but create some big objects like that, and then deletes them,  ActivityMonitor shows memory allocated to R and then freed up.  This is what I'd expect from the R.app GUI as well.

  For whatever reason, gc() happens earlier in one case than in the other.  This is all internal implementation detail; it's nothing to worry about.  If you want to force memory to be cleaned up earlier than R would do it on its own, you can always call gc() yourself.

> 2) If I boot several other applications, using up all my RAM, at some point the operating system moves some of R's active memory into virtual memory.  Now the funny thing:  if I make R the foreground app, or if I simply quit all the other apps,  R does not get its active memory allocation back.  The 'pie chart' shows memory has been freed, not just "inactive" .

  Perhaps the memory that got purged was available for garbage collection, and got collected.  Or perhaps it was caches that got cleared.  Who knows.  Unless something is actually malfunctioning, it is not worth worry about memory usage like this.  There are many reasons why you might see these kinds of patterns, and they are all implementation details.

> Did I set some bad parameter in one of my config files?  Is there some way within R to force all that memory to be freed up (other than quitting and restarting R)?

  Call gc() if you want.  Or just relax.  :->

> Thanks for your help -- I'm not the least put out if someone wants to point out what a dummy I'm being.

  No worries, it confused me when I started, too.

Ben Haller
McGill University

http://biology.mcgill.ca/grad/ben/



More information about the R-SIG-Mac mailing list