[R] Memory leak in nleqslv()

Andrew Leach aleach at ualberta.ca
Sun Jun 11 23:16:40 CEST 2017


Hello all,

I am relatively new to R, but enjoying it very much.  I am hoping that
someone on this list can help me with an issue I am having.

I am having issues with iterations over nleqslv, in that the solver
does not appear to clean up memory used in previous iterations. I
believe I've isolated the/my issue in a small sample of code:

library(nleqslv)

cons_ext_test <- function(x){
rows_x <- length(x)/2
x_1 <- x[1:rows_x]
x_2 <- x[(rows_x+1):(rows_x*2)]
eq1<- x_1-100
eq2<-x_2*10-40
return(c(eq1,eq2))
}

model_test <- function()
{
reserves<-(c(0:200)/200)^(2)*2000
lambda <- numeric(NROW(reserves))+5
res_ext <- pmin((reserves*.5),5)
x_test <- c(res_ext,lambda)
#print(x_test)
for(test_iter in c(1:1000))
   nleqslv(x_test,cons_ext_test,jacobian=NULL)
i<- sort( sapply(ls(),function(x){object.size(get(x))}))
print(i[(NROW(i)-5):NROW(i)])
}

model_test()

When I run this over 1000 iterations, memory use ramps up to over 2.4 GB

While running it with 10 iterations uses far less memory, only 95MB:

Running it once has my rsession with 62Mb of use, so growth in memory
allocation scales with iterations.

Even after 1000 iterations, with 2+ GB of memory used by the R
session, no large-sized objects are listed, although mem_use() shows
2+ GB of memory used.

test_iter    lambda   res_ext  reserves    x_test
       48      1648     1648      1648      3256

I've replicated this on OS-X and in Windows both on a desktop and a
Surface Pro, however colleagues have run this on their machines and
not found the same result.  gc() does not rectify the issue, although
re-starting R does.

Any help would be much appreciated.

AJL


-- 
Andrew Leach
Associate Professor of Natural Resources, Energy and Environment (NREE)
Academic Director, Energy Programs
Alberta School of Business| 3-20D Business Building
University of Alberta |Edmonton, AB  T6G 2R6 | Canada
T. 780.492.8489
E. andrew.leach at ualberta.ca
www.business.ualberta.ca

Follow me on Twitter at @andrew_leach



More information about the R-help mailing list