r-simmer / simmer

Discrete-Event Simulation for R
https://r-simmer.org
GNU General Public License v2.0
222 stars 42 forks source link

Dangling environment prevents object destruction #278

Closed Enchufa2 closed 2 years ago

Enchufa2 commented 2 years ago

This works fine:

library(simmer)

simulate <- function() {
  simmer(mon=monitor_csv()) %>%
    add_generator("dummy", trajectory(), function() c(1, -1)) %>%
    run()
}

for (i in 1:2000)
  env <- simulate()

But this quickly ends up in "too many open files".

library(simmer)

simulate <- function() {
  env <- simmer(mon=monitor_csv()) %>%
    add_generator("dummy", trajectory(), function() c(1, -1)) %>%
    run()
}

for (i in 1:2000)
  env <- simulate()

so it seems that there's still some dangling environment around that prevents objects from being destroyed.

Enchufa2 commented 2 years ago

Not a bug after all, it's just that we need to keep sure that we destroy the named environments properly (by overwriting or rm'ing them) before returning from a function. E.g., this works just fine:

library(simmer)

simulate <- function() {
  env <- simmer(mon=monitor_csv())
  env <- env %>%
    add_generator("dummy", trajectory(), function() c(1, -1)) %>%
    run() %>%
    wrap()
  env
}

for (i in 1:2000)
  env <- simulate()