mm.rst 4.0 KB

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