Finding heap allocations
The TinyGo compiler toolchain tries to optimize code in such a way that no heap allocations are left in the result, but some allocations cannot be optimized. Is there a way to know which those allocations are? Yes! We can deactivate the garbage collector (GC) by passing a flag to the build
and flash
commands.
When the GC is deactivated, the compilation process is going to fail and throws an error, which points to the line of code that caused a heap allocation. Let's check out the following code example, which causes a heap allocation:
package main var myString *string func main() { Â Â Â Â Â Â Â Â Â Â Â Â value := "my value" Â Â Â Â Â Â Â Â Â Â Â Â myString = &value }
When building this program, we will have the GC deactivated with the following command:
tinygo build -o Appendix/allocations/hex --gc=none --target=arduino Appendix...