|
| 1 | +======================= |
| 2 | +Nim's Memory Management |
| 3 | +======================= |
| 4 | + |
| 5 | +.. default-role:: code |
| 6 | +.. include:: rstcommon.rst |
| 7 | + |
| 8 | +:Author: Andreas Rumpf |
| 9 | +:Version: |nimversion| |
| 10 | + |
| 11 | +.. |
| 12 | +
|
| 13 | + |
| 14 | + "The road to hell is paved with good intentions." |
| 15 | + |
| 16 | + |
| 17 | +Multi-paradigm Memory Management Strategies |
| 18 | +=========================================== |
| 19 | + |
| 20 | +.. default-role:: option |
| 21 | + |
| 22 | +Nim offers multiple different memory management strategies. |
| 23 | +To choose the memory management strategy use the `--mm:` switch. |
| 24 | + |
| 25 | +**The recommended switch for newly written Nim code is `--mm:orc`.** |
| 26 | + |
| 27 | + |
| 28 | +ARC/ORC |
| 29 | +------- |
| 30 | + |
| 31 | +`--mm:orc` is a memory management mode primarily based on reference counting. Cycles |
| 32 | +in the object graph are handled by a "cycle collector" which is based on "trial deletion". |
| 33 | +Since algorithms based on "tracing" are not used, the runtime behavior is oblivious to |
| 34 | +the involved heap sizes. |
| 35 | + |
| 36 | +The reference counting operations (= "RC ops") do not use atomic instructions and do not have to -- |
| 37 | +instead entire subgraphs are *moved* between threads. The Nim compiler also aggressively |
| 38 | +optimizes away RC ops and exploits `move semantics <destructors.html#move-semantics>`_. |
| 39 | + |
| 40 | +Nim performs a fair share of optimizations for ARC/ORC; you can inspect what it did |
| 41 | +to your time critical function via `--expandArc:functionName`. |
| 42 | + |
| 43 | +`--mm:arc` uses the same mechanism as `--mm:orc`, but it leaves out the cycle collector. |
| 44 | +Both ARC and ORC offer deterministic performance for `hard realtime`:idx: systems, but |
| 45 | +ARC can be easier to reason about for people coming from Ada/C++/C -- roughly speaking |
| 46 | +the memory for a variable is freed when it goes "out of scope". |
| 47 | + |
| 48 | +We generally advise you to use the `acyclic` annotation in order to optimize away the |
| 49 | +cycle collector's overhead |
| 50 | +but `--mm:orc` also produces more machine code than `--mm:arc`, so if you're on a target |
| 51 | +where code size matters and you know that your code does not produce cycles, you can |
| 52 | +use `--mm:arc`. Notice that the default `async`:idx: implementation produces cycles |
| 53 | +and leaks memory with `--mm:arc`, in other words, for `async` you need to use `--mm:orc`. |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +Other MM modes |
| 58 | +-------------- |
| 59 | + |
| 60 | +.. note:: The default `refc` GC is incremental, thread-local and not "stop-the-world". |
| 61 | + |
| 62 | +--mm:refc This is the default memory management strategy. It's a |
| 63 | + deferred reference counting based garbage collector |
| 64 | + with a simple Mark&Sweep backup GC in order to collect cycles. Heaps are thread-local. |
| 65 | + `This document <refc.html>`_ contains further information. |
| 66 | +--mm:markAndSweep Simple Mark-And-Sweep based garbage collector. |
| 67 | + Heaps are thread-local. |
| 68 | +--mm:boehm Boehm based garbage collector, it offers a shared heap. |
| 69 | +--mm:go Go's garbage collector, useful for interoperability with Go. |
| 70 | + Offers a shared heap. |
| 71 | + |
| 72 | +--mm:none No memory management strategy nor a garbage collector. Allocated memory is |
| 73 | + simply never freed. You should use `--mm:arc` instead. |
| 74 | + |
| 75 | +Here is a comparison of the different memory management modes: |
| 76 | + |
| 77 | +================== ======== ================= ============== =================== |
| 78 | +Memory Management Heap Reference Cycles Stop-The-World Command line switch |
| 79 | +================== ======== ================= ============== =================== |
| 80 | +ORC Shared Cycle Collector No `--mm:orc` |
| 81 | +ARC Shared Leak No `--mm:arc` |
| 82 | +RefC Local Cycle Collector No `--mm:refc` |
| 83 | +Mark & Sweep Local Cycle Collector No `--mm:markAndSweep` |
| 84 | +Boehm Shared Cycle Collector Yes `--mm:boehm` |
| 85 | +Go Shared Cycle Collector Yes `--mm:go` |
| 86 | +None Manual Manual Manual `--mm:none` |
| 87 | +================== ======== ================= ============== =================== |
| 88 | + |
| 89 | +.. default-role:: code |
| 90 | +.. include:: rstcommon.rst |
| 91 | + |
| 92 | +JavaScript's garbage collector is used for the `JavaScript and NodeJS |
| 93 | +<backends.html#backends-the-javascript-target>`_ compilation targets. |
| 94 | +The `NimScript <nims.html>`_ target uses the memory management strategy built into |
| 95 | +the Nim compiler. |
0 commit comments