gc.rst 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. =======================
  2. Nim's Memory Management
  3. =======================
  4. :Author: Andreas Rumpf
  5. :Version: |nimversion|
  6. ..
  7. "The road to hell is paved with good intentions."
  8. Introduction
  9. ============
  10. This document describes how the multi-paradigm memory management strategies work.
  11. How to tune the garbage collectors for your needs, like (soft) `realtime systems`:idx:,
  12. and how the memory management strategies that are not garbage collectors work.
  13. Multi-paradigm Memory Management Strategies
  14. ===========================================
  15. To choose the memory management strategy use the ``--gc:`` switch.
  16. - ``--gc:refc``. This is the default GC. It's a
  17. deferred reference counting based garbage collector
  18. with a simple Mark&Sweep backup GC in order to collect cycles. Heaps are thread-local.
  19. - ``--gc:markAndSweep``. Simple Mark-And-Sweep based garbage collector. Heaps are thread-local.
  20. - ``--gc:boehm``. Boehm based garbage collector, it offers a shared heap.
  21. - ``--gc:go``. Go's garbage collector, useful for interoperability with Go. Offers a shared heap.
  22. - ``--gc:arc``. Plain reference counting with
  23. `move semantic optimizations <destructors.html#move-semantics>`_, offers a shared heap.
  24. It offers deterministic performance for `hard realtime`:idx: systems. Reference cycles
  25. cause memory leaks, beware.
  26. - ``--gc:orc``. Same as ``--gc:arc`` but adds a cycle collector based on "trial deletion".
  27. Unfortunately, that makes its performance profile hard to reason about so it is less
  28. useful for hard real-time systems.
  29. - ``--gc:none``. No memory management strategy nor a garbage collector. Allocated memory is
  30. simply never freed. You should use ``--gc:arc`` instead.
  31. ================== ======== ================= ============== ===================
  32. Memory Management Heap Reference Cycles Stop-The-World Command line switch
  33. ================== ======== ================= ============== ===================
  34. RefC Local Cycle Collector No ``--gc:refc``
  35. Mark & Sweep Local Cycle Collector No ``--gc:markAndSweep``
  36. ARC Shared Leak No ``--gc:arc``
  37. ORC Shared Cycle Collector No ``--gc:orc``
  38. Boehm Shared Cycle Collector Yes ``--gc:boehm``
  39. Go Shared Cycle Collector Yes ``--gc:go``
  40. None Manual Manual Manual ``--gc:none``
  41. ================== ======== ================= ============== ===================
  42. JavaScript's garbage collector is used for the `JavaScript and NodeJS
  43. <backends.html#backends-the-javascript-target>`_ compilation targets.
  44. The `NimScript <nims.html>`_ target uses the memory management strategy built into
  45. the Nim compiler.
  46. Tweaking the refc GC
  47. ====================
  48. Cycle collector
  49. ---------------
  50. The cycle collector can be en-/disabled independently from the other parts of
  51. the garbage collector with ``GC_enableMarkAndSweep`` and ``GC_disableMarkAndSweep``.
  52. Soft real-time support
  53. ---------------------
  54. To enable real-time support, the symbol `useRealtimeGC`:idx: needs to be
  55. defined via ``--define:useRealtimeGC`` (you can put this into your config
  56. file as well).
  57. With this switch the garbage collector supports the following operations:
  58. .. code-block:: nim
  59. proc GC_setMaxPause*(maxPauseInUs: int)
  60. proc GC_step*(us: int, strongAdvice = false, stackSize = -1)
  61. The unit of the parameters ``maxPauseInUs`` and ``us`` is microseconds.
  62. These two procs are the two modus operandi of the real-time garbage collector:
  63. (1) GC_SetMaxPause Mode
  64. You can call ``GC_SetMaxPause`` at program startup and then each triggered
  65. garbage collector run tries to not take longer than ``maxPause`` time. However, it is
  66. possible (and common) that the work is nevertheless not evenly distributed
  67. as each call to ``new`` can trigger the garbage collector and thus take ``maxPause``
  68. time.
  69. (2) GC_step Mode
  70. This allows the garbage collector to perform some work for up to ``us`` time.
  71. This is useful to call in the main loop to ensure the garbage collector can do its work.
  72. To bind all garbage collector activity to a ``GC_step`` call,
  73. deactivate the garbage collector with ``GC_disable`` at program startup.
  74. If ``strongAdvice`` is set to ``true``,
  75. then the garbage collector will be forced to perform the collection cycle.
  76. Otherwise, the garbage collector may decide not to do anything,
  77. if there is not much garbage to collect.
  78. You may also specify the current stack size via ``stackSize`` parameter.
  79. It can improve performance when you know that there are no unique Nim references
  80. below a certain point on the stack. Make sure the size you specify is greater
  81. than the potential worst-case size.
  82. It can improve performance when you know that there are no unique Nim
  83. references below a certain point on the stack. Make sure the size you specify
  84. is greater than the potential worst-case size.
  85. These procs provide a "best effort" real-time guarantee; in particular the
  86. cycle collector is not aware of deadlines. Deactivate it to get more
  87. predictable real-time behaviour. Tests show that a 1ms max pause
  88. time will be met in almost all cases on modern CPUs (with the cycle collector
  89. disabled).
  90. Time measurement with garbage collectors
  91. ----------------------------------------
  92. The garbage collectors' way of measuring time uses
  93. (see ``lib/system/timers.nim`` for the implementation):
  94. 1) ``QueryPerformanceCounter`` and ``QueryPerformanceFrequency`` on Windows.
  95. 2) ``mach_absolute_time`` on Mac OS X.
  96. 3) ``gettimeofday`` on Posix systems.
  97. As such it supports a resolution of nanoseconds internally; however, the API
  98. uses microseconds for convenience.
  99. Define the symbol ``reportMissedDeadlines`` to make the
  100. garbage collector output whenever it missed a deadline.
  101. The reporting will be enhanced and supported by the API in later versions of the collector.
  102. Tweaking the garbage collector
  103. ------------------------------
  104. The collector checks whether there is still time left for its work after
  105. every ``workPackage``'th iteration. This is currently set to 100 which means
  106. that up to 100 objects are traversed and freed before it checks again. Thus
  107. ``workPackage`` affects the timing granularity and may need to be tweaked in
  108. highly specialized environments or for older hardware.
  109. Keeping track of memory
  110. =======================
  111. If you need to pass around memory allocated by Nim to C, you can use the
  112. procs ``GC_ref`` and ``GC_unref`` to mark objects as referenced to avoid them
  113. being freed by the garbage collector.
  114. Other useful procs from `system <system.html>`_ you can use to keep track of memory are:
  115. * ``getTotalMem()`` Returns the amount of total memory managed by the garbage collector.
  116. * ``getOccupiedMem()`` Bytes reserved by the garbage collector and used by objects.
  117. * ``getFreeMem()`` Bytes reserved by the garbage collector and not in use.
  118. * ``GC_getStatistics()`` Garbage collector statistics as a human-readable string.
  119. These numbers are usually only for the running thread, not for the whole heap,
  120. with the exception of ``--gc:boehm`` and ``--gc:go``.
  121. In addition to ``GC_ref`` and ``GC_unref`` you can avoid the garbage collector by manually
  122. allocating memory with procs like ``alloc``, ``alloc0``, ``allocShared``, ``allocShared0`` or ``allocCStringArray``.
  123. The garbage collector won't try to free them, you need to call their respective *dealloc* pairs
  124. (``dealloc``, ``deallocShared``, ``deallocCStringArray``, etc)
  125. when you are done with them or they will leak.
  126. Heap dump
  127. =========
  128. The heap dump feature is still in its infancy, but it already proved
  129. useful for us, so it might be useful for you. To get a heap dump, compile
  130. with ``-d:nimTypeNames`` and call ``dumpNumberOfInstances`` at a strategic place in your program.
  131. This produces a list of the used types in your program and for every type
  132. the total amount of object instances for this type as well as the total
  133. amount of bytes these instances take up.
  134. The numbers count the number of objects in all garbage collector heaps, they refer to
  135. all running threads, not only to the current thread. (The current thread
  136. would be the thread that calls ``dumpNumberOfInstances``.) This might
  137. change in later versions.