mm.html 11 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <!-- This file is generated by Nim. -->
  4. <html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>Nim's Memory Management</title>
  9. <!-- Google fonts -->
  10. <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
  11. <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
  12. <!-- Favicon -->
  13. <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
  14. <link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
  15. <!-- CSS -->
  16. <link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
  17. <!-- JS -->
  18. <script type="text/javascript" src="dochack.js?v=2.3.1"></script>
  19. </head>
  20. <body>
  21. <div class="document" id="documentId">
  22. <div class="container">
  23. <h1 class="title">Nim's Memory Management</h1>
  24. <table class="docinfo" frame="void" rules="none"><col class="docinfo-name" /><col class="docinfo-content" /><tbody valign="top"><tr><th class="docinfo-name">Author:</th><td>Andreas Rumpf</td></tr>
  25. <tr><th class="docinfo-name">Version:</th><td>2.3.1</td></tr>
  26. </tbody></table><blockquote class="markdown-quote"><p>&quot;The road to hell is paved with good intentions.&quot;</p></blockquote>
  27. <h1 id="multiminusparadigm-memory-management-strategies">Multi-paradigm Memory Management Strategies</h1><p>Nim offers multiple different memory management strategies. To choose the memory management strategy use the <tt class="docutils literal"><span class="pre option">--mm:</span></tt> switch.</p>
  28. <div class="admonition admonition-info"><span class="admonition-info-text"><b>Hint:</b></span>
  29. <strong>The recommended switch for newly written Nim code is `--mm:orc`.</strong></div>
  30. <h2 id="arcslashorc">ARC/ORC</h2><p>ORC is the default memory management strategy. It is a memory management mode primarily based on reference counting. Reference cycles are handled by a cycle collection mechanism based on &quot;trial deletion&quot;. Since algorithms based on &quot;tracing&quot; are not used, the runtime behavior is oblivious to the involved heap and stack sizes.</p>
  31. <p>The reference counting operations (= &quot;RC ops&quot;) do not use atomic instructions and do not have to -- instead entire subgraphs are <em>moved</em> between threads. The Nim compiler also aggressively optimizes away RC ops and exploits <a class="reference external" href="destructors.html#move-semantics">move semantics</a>.</p>
  32. <p>Nim performs a fair share of optimizations for ARC/ORC; you can inspect what it did to your time critical function via <tt class="docutils literal"><span class="pre option">--expandArc:functionName</span></tt>. Likewise, you can inspect the whole module via <tt class="docutils literal"><span class="pre option">--expandArc:fileName</span></tt>.</p>
  33. <p><tt class="docutils literal"><span class="pre option">--mm:arc</span></tt> uses the same mechanism as <tt class="docutils literal"><span class="pre option">--mm:orc</span></tt>, but it leaves out the cycle collector. Both ARC and ORC offer deterministic performance for <span id="hard-realtime_1">hard realtime</span> systems, but ARC can be easier to reason about for people coming from Ada/C++/C -- roughly speaking the memory for a variable is freed when it goes &quot;out of scope&quot;.</p>
  34. <p>We generally advise you to use the <tt class="docutils literal"><span class="pre option">acyclic</span></tt> annotation in order to optimize away the cycle collector's overhead but <tt class="docutils literal"><span class="pre option">--mm:orc</span></tt> also produces more machine code than <tt class="docutils literal"><span class="pre option">--mm:arc</span></tt>, so if you're on a target where code size matters and you know that your code does not produce cycles, you can use <tt class="docutils literal"><span class="pre option">--mm:arc</span></tt>. Notice that the default <span id="async_1">async</span> implementation produces cycles and leaks memory with <tt class="docutils literal"><span class="pre option">--mm:arc</span></tt>, in other words, for <tt class="docutils literal"><span class="pre option">async</span></tt> you need to use <tt class="docutils literal"><span class="pre option">--mm:orc</span></tt>.</p>
  35. <h2 id="other-mm-modes">Other MM modes</h2><div class="admonition admonition-info"><span class="admonition-info-text"><b>Note:</b></span>
  36. The <tt class="docutils literal"><span class="pre option">refc</span></tt> GC is incremental, thread-local and not &quot;stop-the-world&quot;.</div>
  37. <div class="option-list"><div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--mm:refc</span></tt></div><div class="option-list-description">It's a deferred reference counting based garbage collector with a simple Mark&amp;Sweep backup GC in order to collect cycles. Heaps are thread-local. <a class="reference external" href="refc.html">This document</a> contains further information.</div></div>
  38. <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--mm:markAndSweep</span></tt></div><div class="option-list-description">Simple Mark-And-Sweep based garbage collector. Heaps are thread-local.</div></div>
  39. <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--mm:boehm</span></tt></div><div class="option-list-description">Boehm based garbage collector, it offers a shared heap.</div></div>
  40. <div class="option-list-item"><div class="option-list-label"><tt><span class="option">--mm:go</span></tt></div><div class="option-list-description">Go's garbage collector, useful for interoperability with Go. Offers a shared heap.</div></div>
  41. <div class="option-list-item odd"><div class="option-list-label"><tt><span class="option">--mm:none</span></tt></div><div class="option-list-description">No memory management strategy nor a garbage collector. Allocated memory is simply never freed. You should use <tt class="docutils literal"><span class="pre option">--mm:arc</span></tt> instead.</div></div>
  42. </div><p>Here is a comparison of the different memory management modes:</p>
  43. <table border="1" class="docutils"><tr><th>Memory Management</th><th>Heap</th><th>Reference Cycles</th><th>Stop-The-World</th><th>Atomic</th><th>Valgrind compatible</th><th>Command line switch</th></tr>
  44. <tr><td>ORC</td><td>Shared</td><td>Cycle Collector</td><td>No</td><td>No</td><td>Yes</td><td><tt class="docutils literal"><span class="pre option">--mm:orc</span></tt></td></tr>
  45. <tr><td>ARC</td><td>Shared</td><td>Leak</td><td>No</td><td>No</td><td>Yes</td><td><tt class="docutils literal"><span class="pre option">--mm:arc</span></tt></td></tr>
  46. <tr><td>Atomic ARC</td><td>Shared</td><td>Leak</td><td>No</td><td>Yes</td><td>Yes</td><td><tt class="docutils literal"><span class="pre option">--mm:atomicArc</span></tt></td></tr>
  47. <tr><td>RefC</td><td>Local</td><td>Cycle Collector</td><td>No</td><td>No</td><td>No</td><td><tt class="docutils literal"><span class="pre option">--mm:refc</span></tt></td></tr>
  48. <tr><td>Mark &amp; Sweep</td><td>Local</td><td>Cycle Collector</td><td>No</td><td>No</td><td>No</td><td><tt class="docutils literal"><span class="pre option">--mm:markAndSweep</span></tt></td></tr>
  49. <tr><td>Boehm</td><td>Shared</td><td>Cycle Collector</td><td>Yes</td><td>No</td><td>No</td><td><tt class="docutils literal"><span class="pre option">--mm:boehm</span></tt></td></tr>
  50. <tr><td>Go</td><td>Shared</td><td>Cycle Collector</td><td>Yes</td><td>No</td><td>No</td><td><tt class="docutils literal"><span class="pre option">--mm:go</span></tt></td></tr>
  51. <tr><td>None</td><td>Manual</td><td>Manual</td><td>Manual</td><td>Manual</td><td>Manual</td><td><tt class="docutils literal"><span class="pre option">--mm:none</span></tt></td></tr>
  52. </table><p>JavaScript's garbage collector is used for the <a class="reference external" href=" backends.html#backends-the-javascript-target">JavaScript and NodeJS</a> compilation targets. The <a class="reference external" href="nims.html">NimScript</a> target uses the memory management strategy built into the Nim compiler. </p>
  53. <div class="twelve-columns footer">
  54. <span class="nim-sprite"></span>
  55. <br>
  56. <small style="color: var(--hint);">Made with Nim. Generated: 2025-03-15 14:12:49 UTC</small>
  57. </div>
  58. </div>
  59. </div>
  60. <script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
  61. </body>
  62. </html>