refc.rst 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. Tweaking the refc GC
  2. ====================
  3. Cycle collector
  4. ---------------
  5. The cycle collector can be en-/disabled independently from the other parts of
  6. the garbage collector with `GC_enableMarkAndSweep` and `GC_disableMarkAndSweep`.
  7. Soft real-time support
  8. ----------------------
  9. To enable real-time support, the symbol `useRealtimeGC`:idx: needs to be
  10. defined via `--define:useRealtimeGC`:option: (you can put this into your config
  11. file as well).
  12. With this switch the garbage collector supports the following operations:
  13. .. code-block:: nim
  14. proc GC_setMaxPause*(maxPauseInUs: int)
  15. proc GC_step*(us: int, strongAdvice = false, stackSize = -1)
  16. The unit of the parameters `maxPauseInUs` and `us` is microseconds.
  17. These two procs are the two modus operandi of the real-time garbage collector:
  18. (1) GC_SetMaxPause Mode
  19. You can call `GC_SetMaxPause` at program startup and then each triggered
  20. garbage collector run tries to not take longer than `maxPause` time. However, it is
  21. possible (and common) that the work is nevertheless not evenly distributed
  22. as each call to `new` can trigger the garbage collector and thus take `maxPause`
  23. time.
  24. (2) GC_step Mode
  25. This allows the garbage collector to perform some work for up to `us` time.
  26. This is useful to call in the main loop to ensure the garbage collector can do its work.
  27. To bind all garbage collector activity to a `GC_step` call,
  28. deactivate the garbage collector with `GC_disable` at program startup.
  29. If `strongAdvice` is set to `true`,
  30. then the garbage collector will be forced to perform the collection cycle.
  31. Otherwise, the garbage collector may decide not to do anything,
  32. if there is not much garbage to collect.
  33. You may also specify the current stack size via `stackSize` parameter.
  34. It can improve performance when you know that there are no unique Nim references
  35. below a certain point on the stack. Make sure the size you specify is greater
  36. than the potential worst-case size.
  37. It can improve performance when you know that there are no unique Nim
  38. references below a certain point on the stack. Make sure the size you specify
  39. is greater than the potential worst-case size.
  40. These procs provide a "best effort" real-time guarantee; in particular the
  41. cycle collector is not aware of deadlines. Deactivate it to get more
  42. predictable real-time behaviour. Tests show that a 1ms max pause
  43. time will be met in almost all cases on modern CPUs (with the cycle collector
  44. disabled).
  45. Time measurement with garbage collectors
  46. ----------------------------------------
  47. The garbage collectors' way of measuring time uses
  48. (see ``lib/system/timers.nim`` for the implementation):
  49. 1) `QueryPerformanceCounter` and `QueryPerformanceFrequency` on Windows.
  50. 2) `mach_absolute_time` on Mac OS X.
  51. 3) `gettimeofday` on Posix systems.
  52. As such it supports a resolution of nanoseconds internally; however, the API
  53. uses microseconds for convenience.
  54. Define the symbol `reportMissedDeadlines` to make the
  55. garbage collector output whenever it missed a deadline.
  56. The reporting will be enhanced and supported by the API in later versions of the collector.
  57. Tweaking the garbage collector
  58. ------------------------------
  59. The collector checks whether there is still time left for its work after
  60. every `workPackage`'th iteration. This is currently set to 100 which means
  61. that up to 100 objects are traversed and freed before it checks again. Thus
  62. `workPackage` affects the timing granularity and may need to be tweaked in
  63. highly specialized environments or for older hardware.
  64. Thread coordination
  65. -------------------
  66. When the `NimMain()` function is called Nim initializes the garbage
  67. collector to the current thread, which is usually the main thread of your
  68. application. If your C code later spawns a different thread and calls Nim
  69. code, the garbage collector will fail to work properly and you will crash.
  70. As long as you don't use the threadvar emulation Nim uses native thread
  71. variables, of which you get a fresh version whenever you create a thread. You
  72. can then attach a GC to this thread via
  73. .. code-block:: nim
  74. system.setupForeignThreadGc()
  75. It is **not** safe to disable the garbage collector and enable it after the
  76. call from your background thread even if the code you are calling is short
  77. lived.
  78. Before the thread exits, you should tear down the thread's GC to prevent memory
  79. leaks by calling
  80. .. code-block:: nim
  81. system.tearDownForeignThreadGc()
  82. Keeping track of memory
  83. =======================
  84. If you need to pass around memory allocated by Nim to C, you can use the
  85. procs `GC_ref` and `GC_unref` to mark objects as referenced to avoid them
  86. being freed by the garbage collector.
  87. Other useful procs from `system <system.html>`_ you can use to keep track of memory are:
  88. * `getTotalMem()` Returns the amount of total memory managed by the garbage collector.
  89. * `getOccupiedMem()` Bytes reserved by the garbage collector and used by objects.
  90. * `getFreeMem()` Bytes reserved by the garbage collector and not in use.
  91. * `GC_getStatistics()` Garbage collector statistics as a human-readable string.
  92. These numbers are usually only for the running thread, not for the whole heap,
  93. with the exception of `--mm:boehm`:option: and `--mm:go`:option:.
  94. In addition to `GC_ref` and `GC_unref` you can avoid the garbage collector by manually
  95. allocating memory with procs like `alloc`, `alloc0`, `allocShared`, `allocShared0` or `allocCStringArray`.
  96. The garbage collector won't try to free them, you need to call their respective *dealloc* pairs
  97. (`dealloc`, `deallocShared`, `deallocCStringArray`, etc)
  98. when you are done with them or they will leak.
  99. Heap dump
  100. =========
  101. The heap dump feature is still in its infancy, but it already proved
  102. useful for us, so it might be useful for you. To get a heap dump, compile
  103. with `-d:nimTypeNames`:option: and call `dumpNumberOfInstances`
  104. at a strategic place in your program.
  105. This produces a list of the used types in your program and for every type
  106. the total amount of object instances for this type as well as the total
  107. amount of bytes these instances take up.
  108. The numbers count the number of objects in all garbage collector heaps, they refer to
  109. all running threads, not only to the current thread. (The current thread
  110. would be the thread that calls `dumpNumberOfInstances`.) This might
  111. change in later versions.