summary refs log tree commit diff stats
path: root/lib/system.nim
Commit message (Expand)AuthorAgeFilesLines
* Clarify JS cstring len (#14184)hlaaftana2020-05-051-6/+7
* fix #14217 (#14218)cooldome2020-05-051-1/+1
* move since from inclrtl to std/private/since (#14188)hlaaftana2020-05-021-2/+4
* Error -> Defect for defects (#13908)Jacek Sieka2020-04-281-6/+6
* `$` now works for unsigned intergers with `nim js` (#14122)Timothee Cour2020-04-271-4/+5
* since now takes an optional patch, eg: `since: (1, 3, 1)` (#14124)Timothee Cour2020-04-261-1/+5
* forward type alignment information to seqs (#12430)Arne Döring2020-04-191-2/+10
* bump devel version to 1.3.1narimiran2020-04-031-1/+1
* stacktraces can now show custom runtime msgs per frame (#13351)Timothee Cour2020-03-301-16/+20
* arc optimizations (#13325)Andreas Rumpf2020-03-181-8/+1
* catchable defects (#13626)Andreas Rumpf2020-03-121-1/+4
* rename `lenTuple` and `lenVarargs` (#13639)Miran2020-03-121-3/+3
* cleanup Ordinal (#13501)Timothee Cour2020-02-271-0/+12
* Documentation staticRead maximum file size limits (#13485)Juan Carlos2020-02-261-0/+3
* stdlib/system: add sink and move (#13283)cooldome2020-02-181-44/+92
* Repr v2 progress (#13268)cooldome2020-01-281-5/+2
* [backport] fix #11440, add docs to isNil for seq types needing nilseq (#13234...D-Nice2020-01-231-0/+12
* style fix: change 'JS' to 'js' to make it consistent (#13168)Miran2020-01-161-28/+28
* System cleanup, part 2 (#13155)Miran2020-01-151-1649/+29
* Added 'ansic' os support for minimal (embedded) targets (#13088)Ico Doornekamp2020-01-151-2/+7
* more arc features (#13098)Andreas Rumpf2020-01-101-12/+19
* System cleanup, part 1 (#13069)Miran2020-01-101-496/+512
* --exception:goto switch for deterministic exception handling (#12977)Andreas Rumpf2020-01-011-5/+7
* ARC: default to a shared heap with --threads:onAraq2019-12-241-5/+5
* ported channels to ARCAraq2019-12-241-1/+1
* lenVarargs: number of varargs elements (#12907)Timothee Cour2019-12-231-0/+5
* update documentation for `closureScope` and `capture` (#12886)Judd2019-12-211-1/+6
* system.reset is no longer magic (#12937)Clyybber2019-12-191-11/+9
* ARC: cycle detector (#12823)Andreas Rumpf2019-12-171-3/+5
* ARC related bugfixes and refactorings (#12781)Andreas Rumpf2019-12-051-4/+9
* fix db_mysql getRow() when column is null error raised (#12806) [backport]itsumura-h2019-12-051-3/+4
* #12103 - CI for FreeBSD (#12179)Euan2019-11-291-0/+2
* make addQuoted work on nimscript (#12717) [backport]Jasper Jenkins2019-11-281-5/+6
* ARC: ported the GC tests over to --gc:arcAraq2019-11-261-12/+13
* gc:arc: support GC_ref/unref for ref TAraq2019-11-131-1/+1
* fix compilation warning (#12618)cooldome2019-11-081-5/+5
* remove deprecated procs (#12535)Andreas Rumpf2019-11-051-90/+0
* NimPatch of devel version should be an odd number because of the earlier hacksnarimiran2019-11-031-1/+1
* [backport] Add docs to better distinguish among getProjectPath, getCurrentDir...Kaushal Modi2019-10-311-1/+13
* introduce csize_t instead of fixing csize (#12497)Arne Döring2019-10-311-3/+5
* --gc:destructors now means Nim uses pure refcounting (#12557)Andreas Rumpf2019-10-301-8/+10
* fix several typos in documentation and comments (#12553)Nindaleth2019-10-301-4/+4
* Extent json.to testing to VM, add workrounds for VM bugs. (#12493)Arne Döring2019-10-281-2/+8
* fixes #12502Andreas Rumpf2019-10-271-2/+2
* development version should be 1.1.0 so that version checking can work properlyAndreas Rumpf2019-10-271-2/+2
* minor improvementsAndreas Rumpf2019-10-271-7/+7
* Fix word wrappingJjp1372019-10-221-13/+16
* Fix many broken linksJjp1372019-10-221-10/+10
* first implementation of the new --seqsv2 switchAraq2019-10-201-10/+10
* Revert "Fixes #12187 (#12321)" (#12447)Andreas Rumpf2019-10-181-2/+2
a737aa0c2'>^
4aba7421f ^
8aecacc1d ^
7e7c514df ^
ae8eb1ec3 ^
4aba7421f ^
7e7c514df ^


a0ccfd16a ^
0b44d812f ^
a0ccfd16a ^
7e7c514df ^



a0ccfd16a ^
ae8eb1ec3 ^
a0ccfd16a ^





0dc35b784 ^
ae8eb1ec3 ^


7e7c514df ^
ae8eb1ec3 ^
c74cee4e6 ^
ae8eb1ec3 ^
8aecacc1d ^

4aba7421f ^

a0ccfd16a ^

4aba7421f ^
ae8eb1ec3 ^
a0ccfd16a ^
4aba7421f ^




ae8eb1ec3 ^
0b44d812f ^
4aba7421f ^
a0ccfd16a ^


4aba7421f ^
4aba7421f ^
a0ccfd16a ^

7e7c514df ^
0b44d812f ^
7e7c514df ^



4aba7421f ^
7956737ef ^
c74cee4e6 ^

7956737ef ^
3ea644690 ^
7956737ef ^
a0ccfd16a ^

7956737ef ^
a0ccfd16a ^



f5b4d9a2e ^


7956737ef ^
a0ccfd16a ^




915cc86f5 ^









877d52061 ^
915cc86f5 ^
a0ccfd16a ^
915cc86f5 ^


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184


                       
 


                      
  
 
 
                                                   
 
 


            







                                                                                       



                                                                  

                                                                                               





                                                                                                 
 
                                                                                          

                                                                                      
 
                                                                                           
                                                          

 










                                                                                  
 










                                                                                  
 
                                                                             
                                                                                    

 
                      
                     
 
                                                                        
                                                                         

                                                                         
 
                   
                                         
                                                              
 
                                                                       
 
                                                                              
 


                                                                              
                                                                                         
                                                                              
                                                                                         



                
                                                                                 
                                                                                            





                                                                            
                                                                            


                                                                                 
 
                                                                          
                                                                    
                                                                

                                                                             

 

                                        
 
                                                  
                                                       




                                                                            
                                                                            
                                  
 


                                                                                           
 
 

                              
 
                                                                        



                                                                            
 
 

                       
 
                                                                        
                                                                             

                                                                                        
 



                                                                                        


                                                                              
 




                                                                                                                    









                                                                                                 
                                        
 
                                                                                     


                                                                        
=======================
Nim's Memory Management
=======================

:Author: Andreas Rumpf
:Version: |nimversion|

..


  "The road to hell is paved with good intentions."


Introduction
============

This document describes how the multi-paradigm memory management strategies work.
How to tune the garbage collectors for your needs, like (soft) `realtime systems`:idx:,
and how the memory management strategies that are not garbage collectors work.


Multi-paradigm Memory Management Strategies
===========================================

To choose the memory management strategy use the ``--gc:`` switch.

- ``--gc:refc``. This is the default GC. It's a
  deferred reference counting based garbage collector
  with a simple Mark&Sweep backup GC in order to collect cycles. Heaps are thread-local.
- ``--gc:markAndSweep``. Simple Mark-And-Sweep based garbage collector. Heaps are thread-local.
- ``--gc:boehm``. Boehm based garbage collector, it offers a shared heap.
- ``--gc:go``. Go's garbage collector, useful for interoperability with Go. Offers a shared heap.
- ``--gc:arc``. Plain reference counting with
  `move semantic optimizations <destructors.html#move-semantics>`_, offers a shared heap.
  It offers deterministic performance for `hard realtime`:idx: systems. Reference cycles
  cause memory leaks, beware.

- ``--gc:orc``. Same as ``--gc:arc`` but adds a cycle collector based on "trial deletion".
  Unfortunately, that makes its performance profile hard to reason about so it is less
  useful for hard real-time systems.

- ``--gc:none``. No memory management strategy nor a garbage collector. Allocated memory is
  simply never freed. You should use ``--gc:arc`` instead.


================== ======== ================= ============== ===================
Memory Management  Heap     Reference Cycles  Stop-The-World Command line switch
================== ======== ================= ============== ===================
RefC               Local    Cycle Collector   No             ``--gc:refc``
Mark & Sweep       Local    Cycle Collector   No             ``--gc:markAndSweep``
ARC                Shared   Leak              No             ``--gc:arc``
ORC                Shared   Cycle Collector   No             ``--gc:orc``
Boehm              Shared   Cycle Collector   Yes            ``--gc:boehm``
Go                 Shared   Cycle Collector   Yes            ``--gc:go``
None               Manual   Manual            Manual         ``--gc:none``
================== ======== ================= ============== ===================

JavaScript's garbage collector is used for the `JavaScript and NodeJS
<backends.html#backends-the-javascript-target>`_ compilation targets.
The `NimScript <nims.html>`_ target uses the memory management strategy built into
the Nim compiler.


Tweaking the refc GC
====================

Cycle collector
---------------

The cycle collector can be en-/disabled independently from the other parts of
the garbage collector with ``GC_enableMarkAndSweep`` and ``GC_disableMarkAndSweep``.


Soft real-time support
---------------------

To enable real-time support, the symbol `useRealtimeGC`:idx: needs to be
defined via ``--define:useRealtimeGC`` (you can put this into your config
file as well).
With this switch the garbage collector supports the following operations:

.. code-block:: nim
  proc GC_setMaxPause*(maxPauseInUs: int)
  proc GC_step*(us: int, strongAdvice = false, stackSize = -1)

The unit of the parameters ``maxPauseInUs`` and ``us`` is microseconds.

These two procs are the two modus operandi of the real-time garbage collector:

(1) GC_SetMaxPause Mode

    You can call ``GC_SetMaxPause`` at program startup and then each triggered
    garbage collector run tries to not take longer than ``maxPause`` time. However, it is
    possible (and common) that the work is nevertheless not evenly distributed
    as each call to ``new`` can trigger the garbage collector and thus take  ``maxPause``
    time.

(2) GC_step Mode

    This allows the garbage collector to perform some work for up to ``us`` time.
    This is useful to call in the main loop to ensure the garbage collector can do its work.
    To bind all garbage collector activity to a ``GC_step`` call,
    deactivate the garbage collector with ``GC_disable`` at program startup.
    If ``strongAdvice`` is set to ``true``,
    then the garbage collector will be forced to perform collection cycle.
    Otherwise, the garbage collector may decide not to do anything,
    if there is not much garbage to collect.
    You may also specify the current stack size via ``stackSize`` parameter.
    It can improve performance when you know that there are no unique Nim
    references below a certain point on the stack. Make sure the size you specify
    is greater than the potential worst-case size.

These procs provide a "best effort" real-time guarantee; in particular the
cycle collector is not aware of deadlines. Deactivate it to get more
predictable real-time behaviour. Tests show that a 1ms max pause
time will be met in almost all cases on modern CPUs (with the cycle collector
disabled).


Time measurement with garbage collectors
----------------------------------------

The garbage collectors' way of measuring time uses
(see ``lib/system/timers.nim`` for the implementation):

1) ``QueryPerformanceCounter`` and ``QueryPerformanceFrequency`` on Windows.
2) ``mach_absolute_time`` on Mac OS X.
3) ``gettimeofday`` on Posix systems.

As such it supports a resolution of nanoseconds internally; however, the API
uses microseconds for convenience.

Define the symbol ``reportMissedDeadlines`` to make the
garbage collector output whenever it missed a deadline.
The reporting will be enhanced and supported by the API in later versions of the collector.


Tweaking the garbage collector
------------------------------

The collector checks whether there is still time left for its work after
every ``workPackage``'th iteration. This is currently set to 100 which means
that up to 100 objects are traversed and freed before it checks again. Thus
``workPackage`` affects the timing granularity and may need to be tweaked in
highly specialized environments or for older hardware.


Keeping track of memory
=======================

If you need to pass around memory allocated by Nim to C, you can use the
procs ``GC_ref`` and ``GC_unref`` to mark objects as referenced to avoid them
being freed by the garbage collector.
Other useful procs from `system <system.html>`_ you can use to keep track of memory are:

* ``getTotalMem()`` Returns the amount of total memory managed by the garbage collector.
* ``getOccupiedMem()`` Bytes reserved by the garbage collector and used by objects.
* ``getFreeMem()`` Bytes reserved by the garbage collector and not in use.
* ``GC_getStatistics()`` Garbage collector statistics as a human-readable string.

These numbers are usually only for the running thread, not for the whole heap,
with the exception of ``--gc:boehm`` and ``--gc:go``.

In addition to ``GC_ref`` and ``GC_unref`` you can avoid the garbage collector by manually
allocating memory with procs like ``alloc``, ``alloc0``, ``allocShared``, ``allocShared0`` or ``allocCStringArray``.
The garbage collector won't try to free them, you need to call their respective *dealloc* pairs
(``dealloc``, ``deallocShared``, ``deallocCStringArray``, etc)
when you are done with them or they will leak.


Heap dump
=========

The heap dump feature is still in its infancy, but it already proved
useful for us, so it might be useful for you. To get a heap dump, compile
with ``-d:nimTypeNames`` and call ``dumpNumberOfInstances`` at a strategic place in your program.
This produces a list of used types in your program and for every type
the total amount of object instances for this type as well as the total
amount of bytes these instances take up.

The numbers count the number of objects in all garbage collector heaps, they refer to
all running threads, not only to the current thread. (The current thread
would be the thread that calls ``dumpNumberOfInstances``.) This might
change in later versions.