Concurrent Data Processing in Elixir: JobSupervisor does not go down (page 54)

Thanks for your question @kunna. This is indeed the expected behaviour. The idle supervisor processes are harmless, and they don’t take up much memory. It’s usually not an issue in practice.

However, you can shutdown these processes if you want to. Something like this would work:

    Jobber.JobRunner
    |> DynamicSupervisor.which_children()
    |> Enum.each(fn {_, pid, _, _} ->
      if Supervisor.count_children(pid).active == 0 do
        Supervisor.stop(pid)
      end
    end)

I haven’t tested this function, but the idea is to check if the are any running processes under each JobSupervisor, and if there are not, call Supervisor.stop/1. You can look up the which_children/1 and count_children/1 functions for more information. This code can run periodically in a GenServer using Process.send_after/2, which is the technique covered in section “Notifying the Process of Events”.

I hope this helps :slight_smile: I will also try to clarify this in the book for the next beta version.