If you really want to go without garbage collection, you need to use the --gc
with the none
parameter, as explained in the Nim Compiler User Guide. The none
parameter disables the garbage collector, leaving you on your own. This usually means any string operations will produce a warning, because despite memory being allocated, nobody is freeing it later:
proc main() =
let x = 5
echo "Hello " & $x & " there"
main()
If you compile this little test with nim c --gc:none -r test.nim
you will get the gc warnings:
test.nim(3, 19) Warning: '$ x' uses GC'ed memory [GcMem]
test.nim(3, 22) Warning: '&("Hello ", $ x, " there")' uses GC'ed memory [GcMem]
This can help you catch which parts of Nim are safe to use in a GC-less environment, either directly or indirectly. Note, however, that certain operations can be moved to the compilation phase. As such, the following example producing the same output is safe to use without GC because all the const
expressions are allocated statically by the generated C code:
proc main() =
const x = 5
echo "Hello " & $x & " there"
main()
Looking inside the nimcache
directory you will find the source contains a line similar to this:
STRING_LITERAL(TM_ipcYmBC9bj9a1BW35ABoB1Kw_2, "Hello 5 there", 13);
Still, note that in the documentation mentioned above there is a link to Nim's Garbage Collector documentation, which contains a very specific Realtime support section which might be of help, and possibly avoid you the pain of handling manually memory if the compromise it offers meets your requirements.