Delete a module

Is it possible to delete a module so it can be GCed?

As far as I know, no you cannot. However, you could go into a module and set all it’s non-constant bindings to nothing which will at least allow those bindings to be GC’d

julia> module Foo
       a = [1,2,3]
       b = "hi"
       f(x, y) = x + 1
       const c = 1
       d = 100
       end
Main.Foo

julia> getproperty.((Foo,), names(Foo, all=true))
11-element Vector{Any}:
    typeof(Main.Foo.eval)
    typeof(Main.Foo.f)
    typeof(Main.Foo.include)
    Main.Foo
    [1, 2, 3]
    "hi"
   1
 100
    eval (generic function with 1 method)
    f (generic function with 1 method)
    include (generic function with 2 methods)

julia> function clear!(M::Module)
           for name ∈ names(M, all=true)
               if !isconst(M, name)
                   @eval M $name = $nothing
               end
           end
       end
clear! (generic function with 1 method)

julia> clear!(Foo)

julia> getproperty.((Foo,), names(Foo, all=true))
11-element Vector{Any}:
  typeof(Main.Foo.eval)
  typeof(Main.Foo.f)
  typeof(Main.Foo.include)
  Main.Foo
  nothing
  nothing
 1
  nothing
  eval (generic function with 1 method)
  f (generic function with 1 method)
  include (generic function with 2 methods)
1 Like

Maybe one further enhancement would be to have a branch like

if isconst(M, name) && applicable(empty!, getproperty(M, name))
    empty!(getproperty(M, name))
end

this way, if you have something like a global constant dictionary or array, it’ll get emptied.

1 Like