TinyGo -gc=conservative vs -gc=leaking vs -gc=none for Firmware
Compare the TinyGo -gc=conservative, -gc=leaking and -gc=none garbage collector settings, what each costs, and which one fits a given firmware.

Go programs allocate without being asked to. A slice that grows, a string built by concatenation and a closure that captures a variable can all end up on the heap, and Go's answer is a garbage collector. A microcontroller with 264 KB of RAM cannot carry the collector of a desktop runtime, so TinyGo ships three choices, picked at build time with the -gc flag: conservative, leaking and none. They trade code size, RAM and predictability against each other. This post compares them for TinyGo 0.37, gives a measurement program that shows each one's behavior, and ends with a decision table. It assumes you can build and flash a program, as in the Pico blinky post, and it does not cover tuning of the conservative collector's internals.
The flag is documented in the TinyGo command reference, and recent releases add further collectors, so run tinygo help build to see what yours accepts.
What each setting does
**-gc=conservative** is the default on most targets. It is a mark-and-sweep collector. At collection time it scans the stack and global variables for words that look like pointers into the heap, and treats every such word as a live reference, without knowing the types. "Conservative" means it can keep an object alive because an integer happens to look like an address, but it never frees an object that is in use. Allocation takes memory from a heap that fills the RAM between the end of static data and the stack, and a collection runs when an allocation cannot be satisfied.
**-gc=leaking** is a bump allocator. Each allocation advances a pointer through the heap. Nothing is ever freed. It is the smallest allocator that still lets allocating Go code run, and it is fast and exactly predictable, until the heap is full and the program stops.
**-gc=none** removes the allocator. A program built this way is meant to make no heap allocations, so memory use is the static data plus the stack, known at link time. Code that needs the heap either fails to build or fails on the first allocation, depending on the release.
Measure the difference
A single program shows all three, because it allocates in a loop and reports the runtime's own counters. The runtime.MemStats structure is filled by runtime.ReadMemStats:
package main
import (
"runtime"
"time"
)
var sink []byte
func main() {
time.Sleep(2 * time.Second)
var m runtime.MemStats
for i := 0; i < 8; i++ {
sink = make([]byte, 4096)
sink[0] = byte(i)
runtime.ReadMemStats(&m)
println("iter", i, "heap", m.HeapAlloc, "mallocs", m.Mallocs)
time.Sleep(100 * time.Millisecond)
}
}Build it with each setting:
tinygo flash -target=pico -gc=conservative gcdemo.go
tinygo flash -target=pico -gc=leaking gcdemo.goThe first makes eight 4 KB allocations and drops each one by overwriting sink. On a Pico with a heap of roughly 250 KB, the collector has no reason to run in eight iterations, so the figures grow the same way as the leaking build. The difference appears only after the heap fills, which this short program does not reach. The output of the conservative build, with illustrative values:
iter 0 heap 4112 mallocs 41
iter 1 heap 8208 mallocs 42
iter 2 heap 12304 mallocs 43
iter 3 heap 16400 mallocs 44
iter 4 heap 20496 mallocs 45
iter 5 heap 24592 mallocs 46
iter 6 heap 28688 mallocs 47
iter 7 heap 32784 mallocs 48The absolute numbers depend on what the runtime allocated before main, which is why the first line already shows a non-zero count. The step of 4,096 plus a small header per allocation is the point. Now raise the loop count to 100,000, so that the program allocates many times the size of RAM. The conservative build keeps running: when the heap runs out it collects, frees the dead 4 KB blocks, and the heap figure falls back to a small value and climbs again, a sawtooth. The leaking build prints rising values until the heap is full and then stops with an out-of-memory panic. Nothing the program does can prevent that.
The none build of the same file does not run. Because the code calls make with a size known only at run time, it needs the allocator, and the build or the first allocation fails. That outcome is the useful one: it is how you prove, by compiling, that a piece of firmware never allocates.
What the costs are
| Property | conservative | leaking | none |
|---|---|---|---|
| Frees memory | Yes | No | Not applicable |
| Code size | Largest of the three, since it includes the collector | Small | Smallest |
| Allocation time | Variable; a collection can pause the program | Constant, a pointer bump | Not applicable |
| Worst-case pause | Proportional to live data and stack size | None | None |
| Fits long-running programs that allocate | Yes | No | Only if they never allocate |
| Out-of-memory behavior | Panics when live data exceeds the heap | Panics when total allocations exceed the heap | Not applicable |
The pause row matters for real-time work. A collection scans every pointer-sized word of the stack and globals and walks the live heap, and its duration depends on how much is live. If the main loop controls a motor, an occasional pause of unknown length is a bug.
The conservative collector has one more quirk. Because it treats any word that looks like a pointer as a pointer, a large integer can keep a large buffer from being freed. This is rare on 32-bit targets with 256 KB of RAM, where few random integers fall inside the heap's address range, but it means the heap use you see is an upper bound on what is live.
Choose by what the program does
The decision depends on whether the program allocates after start-up, and for how long it must run.
| Firmware | Best setting | Reason |
|---|---|---|
A sensor logger that runs for months and uses append or strings | conservative | It must reclaim memory, and pauses are harmless |
| A one-shot tool that runs, reports and powers off | leaking | No collector code, and the run is short enough that the heap never fills |
| A motor controller or a protocol stack with deadlines | none | Allocation-free by construction, and proven by the build |
Anything using goroutines, channels and fmt heavily | conservative | These allocate; removing the collector breaks them |
| A bootloader or a tiny part with little flash | none or leaking | Saves the collector's code |
If you choose none, design for it from the start. Declare arrays at package level, size every buffer by constants, and avoid features that allocate: append beyond capacity, fmt formatting, closures that escape, interface conversions of non-pointer values, string concatenation and map creation. A fixed ring buffer is the usual shape:
package main
var ring [64]byte
var head, count int
func put(b byte) {
ring[(head+count)%len(ring)] = b
if count < len(ring) {
count++
} else {
head = (head + 1) % len(ring)
}
}
func main() {
for i := 0; i < 70; i++ {
put(byte(i))
}
println("count", count, "oldest", ring[head])
}count 64 oldest 6Seventy bytes written into a buffer of 64 leave the last 64, which start at 6, so the oldest byte is 6. The program has no heap allocation, and it builds with -gc=none. If you add a line that appends to a slice, the build fails and tells you where the heap crept in.
The compiler already avoids many allocations
TinyGo performs escape analysis. A value that does not outlive its function is placed on the stack, so var buf [32]byte inside a function is stack memory and make([]byte, 32) with a constant size that does not escape may be too. The consequence is that you cannot predict allocation by reading the source alone. Build with -gc=none to find out, since it turns every surviving allocation into a visible failure. The -size full output also shows how much of the binary the collector occupies.
Mixing the settings during development
A practical workflow uses conservative while writing the program, because it forgives careless allocation, and then switches to -gc=none or leaking once the design is stable to harden it. Each switch finds allocations you did not know about. Do the reverse for a library you plan to publish: test it with -gc=none, so users of any setting can use it.


