This is a question I asked on the elixir-lang-core list and it should have been posted here:
Silly Erlang/Elixir beam question.
If I have a module like so:
defmodule Foo do
@data %{:foo => "foo", :bar => "bar"}
def getData(), do: @data
end
spawn fn() ->
x = Foo.getData()
# Do something with x
:timer.sleep(100000)
end
spawn fn()->
x = Foo.getData()
# Do something with x
:timer.sleep(100000)
end
How many copies of
%{:foo => "foo", :bar => "bar"}
are there in memory? Does each process make a copy of the data in it’s stack or do both process point to the same copy in some code space, etc.
Thanks!
-Chris
From Jose:
literals in your code, such as module attributes, are stored in the literal pool. So it is only one copy until you modify it.
From Michał:
Additionally the literal pool is excluded from regular garbage collection (since it never changes), so that’s an additional efficiency gain