[R] R-memory: clearing memory within a function?

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Jun 4 07:33:15 CEST 2014


On 04/06/2014 01:35, Ista Zahn wrote:
> Hi Frank,
>
> I don't think it is possible to state a general rule about which will
> be faster. For example this
>
> system.time({
>      for(i in 1:10000) {
>          x <- matrix(rnorm(10), ncol = 10)
>          y <- mean(x)
>          #rm(x)
>          z <- matrix(runif(10), ncol = 100)
>          #rm(z)
>      }
> })
>
> gets a lot slower if I uncomment the "rm()" lines, but this
>
> system.time({
>      for(i in 1:5) {
>          x <- matrix(rnorm(10000000), ncol = 10)
>          y <- mean(x)
>          rm(x)
>          z <- matrix(runif(10000000), ncol = 100)
>          rm(z)
>      }
> })
>
> is slightly faster than it would be without the rm() lines. I think
> you'll have to run a smaller version of the simulation both ways and
> see which is faster.

Unfortunately, you cannot generalize from smaller examples.  One issue 
is that rm(x) does not actually remove the object: that is done at the 
next garbage collection and the frequency of GCs varies with the object 
sizes.

The one place I have encountered benefits is on the benighted 32-bit OS 
with a 2GB per-process address space.  There fragmentation of the 
address space can be a serious issue with tasks using at least a third 
of the address space, leading to frequent GCs and slow allocation (as 
the allocator searches for a large enough contiguous block).

But then we were not told the 'at a minimum' information requested by 
the posting guide.

>
> Best,
> Ista
>
> On Tue, Jun 3, 2014 at 10:05 AM, Frank van Berkum
> <frankieboytje at hotmail.com> wrote:
>> Dear R-users,
>>
>> I'm working on a project in which many simulations have to be performed within functions. The simulations are quite time consuming. I thought that in general an empty memory is better for speed performance than a full memory.
>>
>> If I call a function which performs simulations within the function, than the memory will temporarily increase (while the function is executed and objects are created within the function), but as soon as the function is finished, temporarily objects are flushed. It seems as if it might be beneficial for speed performance to clear objects from the memory within the function if they are no longer needed in the remainder of the function. Does anyone know whether this is actually the case?
>>
>> Thanks in advance!
>>
>> Frank

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list