[R] memory management

Florent D. flodel at gmail.com
Fri Feb 10 01:26:59 CET 2012


This should help:

> invisible(gc())
>
> m0 <- memory.size()
> mem.usage <- function(){invisible(gc()); memory.size() - m0}
> Mb.size  <- function(x)print(object.size(x), units="Mb")
>
> zz <- data.frame(a=runif(1000000), b=runif(1000000))
> mem.usage()
[1] 15.26
> Mb.size(zz)
15.3 Mb
> a <- zz$a
> mem.usage()
[1] 15.26
> Mb.size(a)
7.6 Mb
> a[2] <- 100
> mem.usage()
[1] 22.89
> Mb.size(a)
7.6 Mb

You can see that a <- zz$a really has no impact on your memory usage.
It is when you start modifying it that R needs to store a whole new
object in memory.



On Thu, Feb 9, 2012 at 5:17 PM, Sam Steingold <sds at gnu.org> wrote:
>> zz <- data.frame(a=c(1,2,3),b=c(4,5,6))
>> zz
>  a b
> 1 1 4
> 2 2 5
> 3 3 6
>> a <- zz$a
>> a
> [1] 1 2 3
>> a[2] <- 100
>> a
> [1]   1 100   3
>> zz
>  a b
> 1 1 4
> 2 2 5
> 3 3 6
>>
>
> clearly a is a _copy_ of its namesake column in zz.
>
> when was the copy made? when a was modified? at assignment?
>
> is there a way to find out how much memory an object takes?
>
> gc() appears not to reclaim all memory after rm() - anyone can confirm?
>
> thanks!
>
> --
> Sam Steingold (http://sds.podval.org/) on Ubuntu 11.10 (oneiric) X 11.0.11004000
> http://www.childpsy.net/ http://mideasttruth.com http://americancensorship.org
> http://www.memritv.org http://jihadwatch.org http://ffii.org
> C combines the power of assembler with the portability of assembler.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list