by davidw · 4y ago · 0 comments
Hard to say without knowing much about the data in question, but my recollection is that large Erlang/Elixir/BEAM "binaries" are actually not copied around. That might be a strategy for sharing larger things in some cases.
Marshalling data is pretty easy in Erlang:
2> Bin = erlang:term_to_binary([1, 2, 3]).
<<131,107,0,3,1,2,3>>
3> erlang:binary_to_term(Bin).
[1,2,3]iex>term = for _ <- 0..1000000, into: [], do: :rand.uniform(0x1111111)
iex>bin = :erlang.term_to_binary(term)
iex>for _ <- 0..1000, do: spawn(fn -> x = bin; :timer.sleep(1000000) end)
# Memory usage exploded in line below
iex>for _ <- 0..1000, do: spawn(fn -> x = term; :timer.sleep(1000000) end)