[R] Memory not release when an environment is created

David Winsemius dwinsemius at comcast.net
Thu Sep 22 19:41:30 CEST 2016


> On Sep 22, 2016, at 8:41 AM, Olivier Merle <oliviermerle35 at gmail.com> wrote:
> 
> Dear,
> 
> When I use big data for a temporary use it seems that the memory is not
> released when a function/environement is created nearby.
> Here the reproducible exemple:
> 
> test<-function(){
> x=matrix(0,50000,10000)
> y=function(nb) nb^2
> return(y)
> }
> xx=test() # 3 Go of Ram is used
> gc() # Memory is not released !! even if x has been destroyed [look into
> software mem used]

Looking at this I would imagine that the unreleased memory is allocated within the `xx` objects environment (since functions in R are actually closures that carry along their environments of creation.


> format(object.size(xx),units="auto") # 1.4 KiB => R is worng on the size of
> the object
> rm(xx)
> gc() # Memory is released
> 
> ## Classic
> test2<-function(){
> x=matrix(0,50000,10000)
> y=1
> return(y)
> }
> xx=test2() # Memory is used
> gc() # => Memory is released
> 
> How can I release the data in test without destroying the xx object ? As x
> which is big object is destroyed, I though I could get my memory back but
> it seems that the function y is keeping the x object.

That's how I understand the semantics of R. You would need to replace the environment that is attached to `xx`. I madesomwaht smaller object:

test<-function(
x=matrix(0,500,
y=function(nb) 
return(y)
}
xx=test() 

> environment(xx)
<environment: 0x7fda21ba35f0>

> object.size(xx)
1384 bytes
> ls(x, envir=environment(xx) )
[1] "x" "y"
> ?get
> object.size( get("x", envir=environment(xx) ) )
400200 bytes

Even with that perspective in mind it still took me two tries to get rid of the xx object, since my abilities to "program on the language" are still fairly modest.:

> eval( x <- NULL, envir=environment(xx) )
NULL
> object.size( get("x", envir=environment(xx) ) )
400200 bytes
> eval( quote(x <- NULL), envir=environment(xx) )
> object.size( get("x", envir=environment(xx) ) )
0 bytes

So now I have learned that object.size does not include measurements of the size of function environments, a fact about which I was not aware.

Best;
David.

> 
> Best
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list