# Dynamically Create and Destroy Modules (to avoid memory leaks in program synthesis)

**URL:** <https://discourse.julialang.org/t/dynamically-create-and-destroy-modules-to-avoid-memory-leaks-in-program-synthesis/39729>\
**Category:** General Usage\
**Tags:** module, memory, eval\
**Created:** [May 19, 2020, 12:55am UTC](https://discourse.julialang.org/t/dynamically-create-and-destroy-modules-to-avoid-memory-leaks-in-program-synthesis/39729 "2020-05-19T00:55:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zennatavares](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zennatavares/32/6205_2.png) [@zennatavares](https://discourse.julialang.org/u/zennatavares)\
**Post date:** [May 19, 2020, 12:55am UTC](https://discourse.julialang.org/t/dynamically-create-and-destroy-modules-to-avoid-memory-leaks-in-program-synthesis/39729/1 "2020-05-19T00:55:43Z")

</div>

I’m trying to do some program synthesis in Julia. This means generating a lot of programs (Julia `Expr`s) evaluating them, and scoring them. These Expressions can create new types, structs, functions, etc. The most reasonable way to do this seems to be to dynamically create a new module. As far as I can tell, creating a module is permanent. Hence it seems like this will inevitably produce memory leaks. Is this true? If so, is there a way around this? Is there a better alternative? Below is a toy example to demonstrate what I mean, which does indeed accumulate increasing amounts of memory:

```julia
function genprog(n = 10)
  for i = 1:n
    y = rand()
    mdname = gensym()
    expr = :(module $mdname
        f(x) = 2*x + $y
        end
    )
    @show expr
    m = @show eval(expr)
    @show Base.invokelatest(m.f, 3))
  end
end

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 19, 2020, 1:05am UTC](https://discourse.julialang.org/t/dynamically-create-and-destroy-modules-to-avoid-memory-leaks-in-program-synthesis/39729/2 "2020-05-19T01:05:14Z")

</div>

To try to avoid the [XY Problem](https://en.wikipedia.org/wiki/XY_problem), could you elaborate a bit on what you are generating all this code for? Is it some sort of genetic algorithm? Without knowing any of this, it seems like you might get better results from building and evaluating data structures rather than generating code.

---

<div class="post-metadata">

**Author:** ![zennatavares](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zennatavares/32/6205_2.png) [@zennatavares](https://discourse.julialang.org/u/zennatavares)\
**Post date:** [May 19, 2020, 1:55am UTC](https://discourse.julialang.org/t/dynamically-create-and-destroy-modules-to-avoid-memory-leaks-in-program-synthesis/39729/3 "2020-05-19T01:55:26Z")

</div>

I am doing Bayesian program synthesis, which means to generate programs that are likely to have to have generate data. This is not quite the same as genetic programming but there are many similarities. I have an internal DSL that I compile to Julia. I could of course write an interpreter for my DSL, but I am hoping to make use of Julia’s compiler instead.

---

<div class="post-metadata">

**Author:** ![Jovian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jovian/32/30595_2.png) [@Jovian](https://discourse.julialang.org/u/Jovian)\
**Post date:** [November 8, 2021, 5:36am UTC](https://discourse.julialang.org/t/dynamically-create-and-destroy-modules-to-avoid-memory-leaks-in-program-synthesis/39729/4 "2021-11-08T05:36:31Z")

</div>

Hey, can you update on how you proceeded with this? I’m in a similar situation.

---

<div class="post-metadata">

**Author:** ![zennatavares](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zennatavares/32/6205_2.png) [@zennatavares](https://discourse.julialang.org/u/zennatavares)\
**Post date:** [November 8, 2021, 11:36am UTC](https://discourse.julialang.org/t/dynamically-create-and-destroy-modules-to-avoid-memory-leaks-in-program-synthesis/39729/5 "2021-11-08T11:36:34Z")

</div>

I didn’t find a very good solution. I ended up building an interpreter.

One solution is to use Julia’s parallelism features to execute the module in a new Julia instance and then kill it.

There’s also [https://github.com/MasonProtter/StaticModules.jl](https://github.com/MasonProtter/StaticModules.jl), which might help but when I looked into it it didn’t seem like it quite had what I needed.

Another solution might be to use Julia’s interpreter

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 8, 2021, 11:51am UTC](https://discourse.julialang.org/t/dynamically-create-and-destroy-modules-to-avoid-memory-leaks-in-program-synthesis/39729/6 "2021-11-08T11:51:31Z")

</div>

See [https://github.com/JuliaLang/julia/issues/14495](https://github.com/JuliaLang/julia/issues/14495) for a Julia issue about this.

---

<div class="post-metadata">

**Author:** ![FireCrumb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/firecrumb/32/35370_2.png) [@FireCrumb](https://discourse.julialang.org/u/FireCrumb)\
**Post date:** [May 5, 2022, 2:52pm UTC](https://discourse.julialang.org/t/dynamically-create-and-destroy-modules-to-avoid-memory-leaks-in-program-synthesis/39729/7 "2022-05-05T14:52:17Z")

</div>

Hello @zennatavares, how did you end up solving this?  
I have exactly the same challenge myself now, which is how I searched and found this thread.
