helloworld.nim
echo "hello world"
Attempt 1
nim c -r --mm:none --warningAsError:GcMem helloworld.nim
Hint: used config file '/etc/nim/nim.cfg' [Conf]
Hint: used config file '/etc/nim/config.nims' [Conf]
..................................................
/usr/lib/nim/system/excpt.nim(317, 12) Error: 'add(s, "No stack traceback available\n")' uses GC'ed memory [GcMem]
Attempt 2
nim c -r --mm:none --warningAsError:GcMem --stackTrace:off helloworld.nim
Hint: used config file '/etc/nim/nim.cfg' [Conf]
Hint: used config file '/etc/nim/config.nims' [Conf]
..................................................
/usr/lib/nim/system/excpt.nim(49, 26) Error: '$data' uses GC'ed memory [GcMem]
Is "echo" unsafe to use without garbage collection?
Is there any other options to try to make hello world work without garbage collection?
I also tried adding the --exceptions:goto option without any result.
The Nim compiler is version 1.6.6.
--mm:arc
Is --mm:none deprecated or is the problem with using echo instead of calling system through FFI?
If I write a program which uses only stack memory, and don't declare "ref" type in my codebase, --mm:none should still work right?
Strings in nim are heap-allocated and garbage collected. If you want to declare a string and print it (hello world), you'd have to use a cstring and {.importc.} C's printf, or something like that.
That said, ARC is a fine recommendation. It produces allocs and frees in the C code at compile-time, so it's exactly what you'd be doing manually. No GC overhead.
There was no change with using cstring type with printf, --panics:on, or stdout.write. Using --os:any caused a new error: "port memory manager to your platform". From the previous pasted errors it seems the problem is in "system/excpt.nim". The top comment in that file states "Carefully coded so that tiny programs which do not use the heap (and nor exceptions) do not include the GC or memory allocator."
Perhaps "--warningAsError:GcMem" compiler option is broken and causing the GC to be included in the compiled program when compiler warnings are turned on?
When I compile helloworld without "--warningAsError:GcMem" there are actually no GcMem warnings, the errors just appear when option to convert warnings to errors is turned on.
nim c -r --mm:none --warning:GcMem helloworld.nim
Hint: used config file '/etc/nim/nim.cfg' [Conf]
Hint: used config file '/etc/nim/config.nims' [Conf]
........................................................
Hint: [Link]
Hint: gc: none; opt: none (DEBUG BUILD, `-d:release` generates faster code)
25298 lines; 0.185s; 25.41MiB peakmem; proj: /home/randbox/helloworld.nim; out: /home/randbox/helloworld [SuccessX]
Hint: /home/randbox/helloworld [Exec]
hello world