MemorySize (memory_size v0.1.0)

This module provides two utilities for understanding what is using the memory in a complex data structure. summary/2 provides information about what fields are using data in a struct tree, while term/2 aims to be a fast, approximate replacement for :erts_debug.flat_size.

Summary

Functions

Returns a list of tuples of the form {size, unit, path} summarizing the memory usage of the passed in value (if it's a struct, one tuple per recursive field will be returned. i.e. if state.guild.sessions is using 100 words, then one of the returned tuples would be {100, :word, [:guild, :sessions]}.

This estimates the size of an Erlang term. It intends to take a reasonable amount of time to complete, as opposed to :erts_debug.flat_size and :erts_debug.size. it does this by sampling large maps and lists. Note that it follows the convention of :erts_debug.size and does not include the 1 word for the term itself. This means that some of the calculations here will seem to be off from https://www.erlang.org/doc/efficiency_guide/advanced.html which includes this 1 word in the estimates it provides.

Functions

Link to this function

summary(value, options \\ [])

Returns a list of tuples of the form {size, unit, path} summarizing the memory usage of the passed in value (if it's a struct, one tuple per recursive field will be returned. i.e. if state.guild.sessions is using 100 words, then one of the returned tuples would be {100, :word, [:guild, :sessions]}.

Options available:

  • max_depth: maximum nesting level for returned tuples (0 means "just the top level", default is unlimited)
  • min_size: a threshold for dropping fields from the result that use less than some threshold amount of memory (default is to show all)
  • precision: How many digits should we keep after the decimal point (default is 2)
  • sample_size: control how many elements of large maps/lists will be sampled (default is 100)
  • sort_by_size: if true, will sort the results by size descending (otherwise will be sorted hierarchically)
  • unit: one of :word, :byte, :kilobyte, :megabyte, :gigabyte for scaling the results (default is word). Kilo means 1024 (in order to be consistent with :recon_alloc.set_unit)
Link to this function

term(value, sample_size \\ 100)

This estimates the size of an Erlang term. It intends to take a reasonable amount of time to complete, as opposed to :erts_debug.flat_size and :erts_debug.size. it does this by sampling large maps and lists. Note that it follows the convention of :erts_debug.size and does not include the 1 word for the term itself. This means that some of the calculations here will seem to be off from https://www.erlang.org/doc/efficiency_guide/advanced.html which includes this 1 word in the estimates it provides.