Will ets table release memory after recreating with the same name?

I have genserver that aggregates events:

  def init(opts) do
    cache =, [:set])
    {:ok, cache}

  def handle_info(:autoflush, cache) do
    Logger.debug(fn -> "#{[:size]} events was aggregated. Sending to transport..." end)
    Process.send_after(self(), :autoflush, @flush_after)
    {:noreply,, [:set])}

  def handle_cast({:add_event, event}, cache) do
    :ets.insert(cache, {event})
    {:noreply, cache}

In init I create ets table. Add values in cast and flush data to external call in info cleaning memory of ets table(as I think).


  1. Is this implementation prone to memory leak? Is old ets table garbage collected?
  2. Aside question: Does this implementation look sanely at all?


  • When you are creating an ETS table without a registered name (check the option named_table) the table is created with a reference. No matter how many you create, the old ones are still there so, yes, you have a leak of memory if you are not deleting the old ones.

    One way to avoid this is by using the named_table option. This is crashing if the previous table exists. You can put the creation of the table with a catch or inside a try-catch block.