Memory usage keeps accumulating when set_attribute() is used?

16 views
Skip to first unread message

SQ3 W23

unread,
Aug 1, 2022, 2:02:21 PM8/1/22
to simmer-devel
Hello,

I'm using R 4.1.2 and simmer 4.4.5, I am working on a simulation that needs to replicated many times, and I noticed that the memory usage kept increasing. It seems to be because of set_attribute(), here is an example:

simulate <- function(time) {
    env <- simmer("Hospital process")
   
    patient <- trajectory("patient")  %>%
      set_attribute("rand", function() {rnorm(1)})  

    env <- env %>%
      add_generator("patient", patient, function() {1},mon=1) %>%
      run(time) %>%
      wrap()
    env
}

mem<-c(1:10)
for (x in 1:10) {
  for (i in 1:500) {
    env <- simulate(100)
  }
  mem[x]<-memory.size()
}


Memory usage when running 10 times:
mem
[1] 535.47 534.54 539.49 537.81 542.58 552.64 546.90 548.49 608.48 614.58

Normally, memory usage fluctuates around a value and doesn't keep increasing like this. Would it help to use rm() somewhere?

Thank you.

Iñaki Ucar

unread,
Aug 2, 2022, 3:42:09 PM8/2/22
to simmer...@googlegroups.com
Please try wrapping the environment into a new variable, then rm'ing everything else. Something like

out <- env %>%

  add_generator("patient", patient, function() {1},mon=1) %>%
  run(time) %>%
  wrap()
rm(env, patient)
return(out)

--
Iñaki Úcar
Reply all
Reply to author
Forward
0 new messages