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:

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

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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 records...
New

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31194 112
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
New

We're in Beta

About us Mission Statement