Description
Extracted from #4889.
@aykevl wrote
In TinyGo, I think it is unlikely we'll ever have a moving garbage collector that would require pinning. Not just because of DMA, but also because it means interrupts can't run while the GC moves the heap.
I've seen this comment before, but I'd like to say I find the current garbage collector too unreliable for dynamic allocation because of memory fragmentation. I frequently run into out-of-memory panics before even half of available memory (512kB) has been filled because of fragmentation. Coupled with the escape analyzer missing key allocations I find myself spending significant effort (and API contortions) to avoid allocations altogether.
I don't see any other way of fixing fragmentation and making the GC reliable than making it moving (e.g. mark-compact GC).
In other words, please reconsider adding a garbage collector that is doesn't fail because of memory fragmentation.
I realize that non-GC runtimes such as the C runtime are also prone to memory fragmentation, which is why embedded programmers shy away from dynamic memory allocation. However, GC languages such as Go are uniquely positioned to eliminate memory fragmentation, thus guaranteeing robustness provided the dynamic heap size fits in available memory.
A practical implementation will need to support pinning of memory used for DMA and interrupts. The recently added runtime.Pinner
was added to Big Go for such purposes. Adding Pinner
to DMA and interrupt code is a challenge, but I think the trade-off is worth it: there's much more user code than DMA or interrupt code.