I’m facing a weird problem and I have no clue where to start looking.
I’m running some economic simulations and since I made the agent creation type safe (eliminated the warning that the types of the agent were not defined) I can’t run the exact same simulation twice without the second run taking forever and gobbling up gigabytes of memory. The first run finishes in about 30 something seconds but the second run takes forever and memory use shoots over 50Gb. What is going on?
I have no idea how to replicate this in a short block of code so the only think I can do is point to where the problem occurs.
For those who want to help me figure this out, you need these two repositories:
The problem occurs when you run the following code block twice:
using EconoSim
using MoneySim
data = simulate_belgium()
Simulation time never seems to end and memory use just skyrockets.
I think it has something to do with the create_sumsy_model function in this file:
Since that is where I made the last changes. But to be honest I have no clue!
Any help or even an indication on where I need to start looking would be much appreciated.
I agree with stevengj, there are some globals in keynes_sim.jl, utils.jl and termination_handler.jl, and in belgium.jl. I don’t know if they’re changed anywhere. Anyway, it’s a good practice to make all globals const, this will make the compiler’s job much easier.
Entirely unrelated to that, in keynes_sim.jl there are a couple of method definitions:
RealFunc = Union{Real, Function}
value(rf::RealFunc) = rf isa Function ? rf() : rf
IntFunc = Union{Integer, Function}
value(intf::IntFunc) = intf isa Function ? intf() : intf
Although it works, it’s confusing, because IntFunc is a subtype of RealFunc, so the second method is more specific than the first. This means that the first method can never be called with a Function argument. (Unless invoke is used). It doesn’t really matter in this case, since both methods just evaluate the function argument. For clarity, It should perhaps be rewritten as:
Thank you so much! Without that hint I would have been searching for days or weeks!
The bug was in the initialize_population_model!() function in population.jl
There I inadvertently changed the population numbers from percentages to actual population numbers and reused that in the next run.