How can I release the memory of Mnesia transactions occupied after transaction is done?

Viewed 166

I am using Mnesia to build a caching system, Encountered a problem:

Every time I use :mnesia.transaction, it will bring little memory consumption. Almost 10,000 :mnesia.transaction calls will consume 1MB of memory.

After some work, I found that the memory occupied by ets’s table: mnesia_transient_decision will continue to increasing. Every call to :mnesia.transaction will insert a record into table mnesia_transient_decision(call stack: mnesia_tm:execute_outer -> execute_transaction -> apply_fun -> t_commit -> multi_commit -> mnesia_recover:note_decision), and even if I run :mnesia_recover.allow_garb and :erlang.garbage_collect, the memory will not decrease. And this table is an unnamed table, i can't delete it by :ets.delete :mnesia_transient_decision.

Any help is greatly appreciated.

testing code:

defmodule TestTransaction do
  alias :mnesia, as: Mnesia
  def init_mnesia do
    Mnesia.start()
    Mnesia.create_table(Person, attributes: [:age, :username], disc_copies: [node()])
  end
  def case_trans_read do
    f = fn ->
      Mnesia.read(Person, 2)
    end
    Mnesia.transaction(f)
  end

  def print_memory_info do
    IO.puts("total: #{:erlang.memory[:total]}, ets: #{:erlang.memory[:ets]}")
  end

  def benchmarking(_, 0), do: nil

  def benchmarking(f, n) do
    apply(TestTransaction, f, [])
    benchmarking(f, n-1)
  end

end

TestTransaction.init_mnesia

TestTransaction.benchmarking :case_trans_read, 10000

observer_cli: memory usage of ets table: memory usage of ets table

0 Answers
Related