Memory usage in module attributes

58 views
Skip to first unread message

Chris Jimison

unread,
Mar 16, 2017, 4:24:55 PM3/16/17
to elixir-lang-core
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

Chris Jimison

unread,
Mar 16, 2017, 4:32:06 PM3/16/17
to elixir-lang-core
CRAP.  I posted here not reading that the topic should go to the general form.  I will move it to there and I am very sorry to bother folks!

José Valim

unread,
Mar 16, 2017, 4:33:27 PM3/16/17
to elixir-l...@googlegroups.com
In any case, the answer is that literals in your code, such as module attributes, are stored in the literal pool. So it is only one copy until you modify it.



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/8759b998-e32a-4b9e-9e23-ab8c213c4524%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Jimison

unread,
Mar 16, 2017, 4:37:24 PM3/16/17
to elixir-lang-core, jose....@plataformatec.com.br
Thanks Jose you won me a beer :)  And once again I am very sorry for breaking form etiquette.

-Chris
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

Michał Muskała

unread,
Mar 16, 2017, 4:57:22 PM3/16/17
to elixir-l...@googlegroups.com
Additionally the literal pool is excluded from regular garbage collection (since it never changes), so that's an additional efficiency gain.

Michał.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4%2B_cr%3DqqL7A7RQE5nd-ebFORk_ocyGTT7FXrOwbXrN9bw%40mail.gmail.com.

Chris Jimison

unread,
Mar 16, 2017, 5:08:21 PM3/16/17
to elixir-lang-core
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/8759b998-e32a-4b9e-9e23-ab8c213c4524%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages