# Weird problem. Two runs of the same code and the second run takes forever and gobbles up memory

**URL:** https://discourse.julialang.org/t/weird-problem-two-runs-of-the-same-code-and-the-second-run-takes-forever-and-gobbles-up-memory/124654
**Category:** General Usage
**Created:** [January 10, 2025, 6:51pm UTC](https://discourse.julialang.org/t/weird-problem-two-runs-of-the-same-code-and-the-second-run-takes-forever-and-gobbles-up-memory/124654 "2025-01-10T18:51:34Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)
#### Post date: [January 10, 2025, 6:51pm UTC](https://discourse.julialang.org/t/weird-problem-two-runs-of-the-same-code-and-the-second-run-takes-forever-and-gobbles-up-memory/124654/1 "2025-01-10T18:51:34Z")

</div>

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:

> **[GitHub - HapponomyOrg/EconoSim.jl: Economic simulation tools](https://github.com/HapponomyOrg/EconoSim.jl)**
>
> Economic simulation tools

> **[GitHub - HapponomyOrg/MoneySim.jl](https://github.com/HapponomyOrg/MoneySim.jl)**
>
> Contribute to HapponomyOrg/MoneySim.jl development by creating an account on GitHub.

The problem occurs when you run the following code block twice:

```julia
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:

> <https://github.com/HapponomyOrg/EconoSim.jl/blob/main/src/models/sumsy_model.jl>

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.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 10, 2025, 7:36pm UTC](https://discourse.julialang.org/t/weird-problem-two-runs-of-the-same-code-and-the-second-run-takes-forever-and-gobbles-up-memory/124654/2 "2025-01-10T19:36:03Z")

</div>

> [@stefkuypers](#):
>
> Any help or even an indication on where I need to start looking would be much appreciated.

I would look for global variables that are being set in the first run and are inadvertently affecting the second run.

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [January 10, 2025, 8:46pm UTC](https://discourse.julialang.org/t/weird-problem-two-runs-of-the-same-code-and-the-second-run-takes-forever-and-gobbles-up-memory/124654/3 "2025-01-10T20:46:12Z")

</div>

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:

```julia
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:

```julia
const RealFunc = Union{Real, Function}
const IntFunc = Union{Integer, Function}

value(f::Function) = f()
value(x::Real) = x

```

or maybe just:

```julia
value(x) = x
value(f::Function) = f()

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 10, 2025, 9:45pm UTC](https://discourse.julialang.org/t/weird-problem-two-runs-of-the-same-code-and-the-second-run-takes-forever-and-gobbles-up-memory/124654/4 "2025-01-10T21:45:33Z")

</div>

> [@sgaure](#):
>
> Anyway, it’s a good practice to make all globals `const`,

(This doesn’t prevent them from being mutated and affecting subsequent runs, e.g. if they are arrays.)

---

<div class="post-metadata">

### Author: ![stefkuypers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefkuypers/32/19185_2.png) [@stefkuypers](https://discourse.julialang.org/u/stefkuypers)
#### Post date: [January 11, 2025, 7:07am UTC](https://discourse.julialang.org/t/weird-problem-two-runs-of-the-same-code-and-the-second-run-takes-forever-and-gobbles-up-memory/124654/5 "2025-01-11T07:07:58Z")

</div>

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.
