Files
Nim/mm.html
github-actions[bot] ef3d9e7b38 Deploy to GitHub pages
2025-12-28 15:55:52 +00:00

74 lines
11 KiB
HTML

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- This file is generated by Nim. -->
<html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nim's Memory Management</title>
<!-- Google fonts -->
<link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
<link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
<!-- Favicon -->
<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=="/>
<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=">
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
<!-- JS -->
<script type="text/javascript" src="dochack.js?v=2.3.1"></script>
</head>
<body>
<div class="document" id="documentId">
<div class="container">
<h1 class="title">Nim's Memory Management</h1>
<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>
<tr><th class="docinfo-name">Version:</th><td>2.3.1</td></tr>
</tbody></table><blockquote class="markdown-quote"><p>&quot;The road to hell is paved with good intentions.&quot;</p></blockquote>
<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>
<div class="admonition admonition-info"><span class="admonition-info-text"><b>Hint:</b></span>
<strong>The recommended switch for newly written Nim code is `--mm:orc`.</strong></div>
<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>
<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>
<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>
<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>
<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>
<h2 id="other-mm-modes">Other MM modes</h2><div class="admonition admonition-info"><span class="admonition-info-text"><b>Note:</b></span>
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>
<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>
<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>
<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>
<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>
<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>
</div><p>Here is a comparison of the different memory management modes:</p>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
</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>
<div class="twelve-columns footer">
<span class="nim-sprite"></span>
<br>
<small style="color: var(--hint);">Made with Nim. Generated: 2025-12-28 15:54:22 UTC</small>
</div>
</div>
</div>
<script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
</body>
</html>