erlangelixirmnesia

Get or calculate Mnesia total size


I want to find the total size of a mnesia database. I have only one node.

Can I get the size of mnesia from some function or can I calculate it somehow?

I have looked in the documentation http://erlang.org/doc/man/mnesia.html, but I cannot find a fucntion to get such information for the whole database.

Do I need to calculate it per table using table_info/2? And if so how?

NOTE: I don't know how to do that with the current data points, the size is 2 (for testing I have only 2 entries) and memory is 348.


Solution

  • You need to iterate over all the tables with mnesia:system_info(tables) and read each table memory with mnesia:table_info(Table, memory) to obtain the number of words occupied by your table. To transform that value to bytes, you can first use erlang:system_info(wordsize) to get the word size in bytes for your machine architecture(on a 32-bit system a word is 4 bytes and 64 bits it's 8 bytes) and multiply it by your Mnesia table memory. A rough implementation:

    %% Obtain the memory in of bytes of all the mnesia tables.
    -spec get_mnesia_memory() -> MemInBytes :: number().
    get_mnesia_memory() ->
      WordSize = erlang:system_info(wordsize),
      CollectMem = fun(Tbl, Acc) ->
       Mem = mnesia:table_info(Tbl, memory) * WordSize,
       Acc + Memory
      end,
      lists:foldl(CollectMem, 0, mnesia:system_info(tables)).