Skip to content

How to free up system memory?

5 messages · Ben Haller, Carl Witthoft, Sean Davis

#
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?

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.

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" .

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)?

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

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

            
Unlikely.  Much more likely, garbage collection simply hasn't cleaned up the memory yet.  Read "?gc".
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.
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.
Call gc() if you want.  Or just relax.  :->
No worries, it confused me when I started, too.

Ben Haller
McGill University

http://biology.mcgill.ca/grad/ben/
#
Excellent --  gc() is exactly what i was looking for.

thanks
Carl
On 5/2/11 7:36 PM, Ben Haller wrote: