hariharasudhan94

hariharasudhan94

How Elixir Manages Memory?

As we all know elixir is an immutable language, for example consider tha below statement

statement1:

map = %{ “name” => “hari”, “age” => 22 }

statement2:
if i want update map with new key, value, i write a statement as follows

map = Map.put_new(map, “gender”, “male”)

if i update a value like in statement2, it copies the whole map(statement1) onto another variable, then how does it manage memory efficiently?

Most Liked

NobbZ

NobbZ

Yes. map gets copied. But its keys and values are not directly encoded in the map, but stored as references. So there are only references copied which point to the same locations as they did in the old map.

The BEAM is very simple in this regard. If you change something, necessary parts will be copied. To make it a bit more efficient, containers store their content as references.

Though not answering your question directly, there can be a lot of insight into how Erlangs and Elixir memory management works when reading the article about OTP 19 new Garbage Collector:

https://www.erlang-solutions.com/blog/erlang-19-0-garbage-collector.html

svarlet

svarlet

Hi @hariharasudhan94,

You might want to watch this:

Yes it is a video about Clojure but it answers your question. The video starts at the right time.

OvermindDL1

OvermindDL1

To be more specific, imagine a map as a property list (not accurate, but it will make this easy to represent), and let’s start with:

iex(27)> m = [a: 1, b: 2] # %{a: 1, b: 2}
[a: 1, b: 2]

Let’s update :b to be 42:

iex(28)> m = [b: 42]++m # Map.put(m, :b, 42)
[b: 42, a: 1, b: 2]
iex(29)> m[:b]
42

And so forth.

This is not how maps actually work (internally they are an…interesting structure that changes form based on its size) but updating a value does not necessarily get rid of the old one (at least until all references to the old structure are GC’d) but any requests on it only get the new.

Map’s are actually fairly easy to make immutable and efficient (see OCaml’s Map for a spectacular and quite readable implementation (not the ‘best’, but those are significantly more unreadable, kind of like the BEAM’s Map, which is even better but its C source is quite unreadable ^.^)), what is not is an Array, yet the BEAM has those too (implemented entirely in erlang, not C, these are fully and entirely functional):

iex(31)> a = :array.new(30)
{:array, 30, 0, :undefined, 100}

So an array is an opaque record of type :array, I’ve set this one to 30 elements (there is a dynamically sized access type as well), 0 are currently defined, with a ‘space’ of 100 allocated, yet you don’t see the space here at all, it is the :undefined, they all start as :undefined by default (unless you set a default value), however since all the values are the same it just represents that value straight. To see how it stores it internally let’s set a value:

iex(33)> a = :array.set(12, "Hello", a)
{:array, 30, 0, :undefined,
 {10,
  {:undefined, :undefined, "Hello", :undefined, :undefined, :undefined,
   :undefined, :undefined, :undefined, :undefined}, 10, 10, 10, 10, 10, 10, 10,
  10, 10}}

Well this looks interesting. What it is doing is it split the array up into a nested tuples that contains nested tuples (potentially multiple layers deep) with a tuple-size based on a heuristic they calculated long ago (which could be wrong now, but good enough regardless), but you see that the ‘storage’ size of 100 is now the tuple, split in to 10 10’s, the first 10 is just ‘10’ meaning this location represents 10 entries of the default value (of `:undefined), but since we set 12 then the indices of 10-19 have to be a 10-tuple, all of which are the default value except the single place we set.

If you allocate a huge array it will only allocate few tuples, and even if you have every single element set to something unique, like:

iex(36)> a = 0..29 |> Enum.reduce(a, &:array.set(&1, to_string(&1), &2))
{:array, 30, 0, :undefined,
 {{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"},
  {"10", "11", "12", "13", "14", "15", "16", "17", "18", "19"},
  {"20", "21", "22", "23", "24", "25", "26", "27", "28", "29"}, 10, 10, 10, 10,
  10, 10, 10, 10}}

So you see the areas we set them all, nice and easy, updating a value at this size of array (up to size 100) only involves setting 3 tuples, which on the BEAM is FAST rather than needing to copy everything. :slight_smile:

And yes, if you make an unsized array the ‘size’ element in the tuple is 0, and if you try to set a value out of range you get an ArgumentError. :slight_smile:

It is just changing how you think to optimize the structure for its use-case. :slight_smile:

Where Next?

Popular in Questions Top

tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "eq...
New
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I'm a nov...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42842 311
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement