Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The exact workings of the garbage collection and memory allocation is only known to a few. Thankfully, they are very happy to share their knowledge and the following is based on what I have learnt from the erlang-questions mailing list and by discussing with OTP developers.</p> <p>When messaging between processes, the content is always copied as there is no shared heap between processes. The only exception is binaries bigger than 64 bytes, where only a reference is copied.</p> <p>When executing code in one process, only parts are updated. Let's analyze tuples, as that is the example you provided.</p> <p>A tuple is actually a structure that keeps references to the actual data somewhere on the heap (except for small integers and maybe one more data type which I can't remember). When you update a tuple, using for example <code>setelement/3</code>, a new tuple is created with the given element replaced, however for all other elements only the reference is copied. There is <a href="http://www.erlang.org/doc/efficiency_guide/commoncaveats.html#id62422" rel="noreferrer">one exception</a> which I have never been able to take advantage of.</p> <p>The garbage collector keeps track of each tuple and understands when it is safe to reclaim any tuple that is no longer used. It might be that the data referenced by the tuple is still in use, in which case the data itself is not collected.</p> <p>As always, Erlang gives you some tools to understand exactly what is going on. <a href="http://www.erlang.org/doc/efficiency_guide/processes.html#id67714" rel="noreferrer">The efficiency guide</a> details how to use <code>erts_debug:size/1</code> and <code>erts_debug:flat_size/1</code> to understand the size of the data structure when used internally in a process and when copied. The trace tools also allows you to understand when, what and how much was garbage collected.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload