I am attempting to wrap a simmer DES (in this case, a psychiatric hospital with a waiting list) inside a function which would pass various parameters (in this case, total number of incoming admissions, number of beds, average length of stay, and replications) to the simulation for modeling, and return the 5th, 50th and 95th percentile for results of interest.
The simulation runs for 5 "years", but I truncate the last year of data, bind the list of simulation results together, and process the results in the function itself, so the actual result of the function is only a small data table.
The end goal is to use mapply to start with a data table of these starting parameters, and build a data table of results from those parameters automatically.
Everything seems to work fine, but I've noticed that RAM consistently creeps upward -- to the point where, if I have enough parameters and replications, my 32 GB machine is completely consumed and the simulation aborts due to lack of memory.
I'm suspecting that there is some kind of reference leak in the simulation environment. Definitely not an R expert, so I'm struggling to figure out how to clean these up -- i.e., presumably deleting the simulation results in the environment they are accumulating.
For simplicity, I am not using lapply or mclapply in the function itself -- just a for loop. Simplified code is attached to demonstrate the issue.
Thanks for your time and consideration.