small and unhandy (yet working) GC for tiny-go + wasm
It's like the leaking GC, but with the stack management elements inside:
package main
import (
"github.com/metrico/micro-gc/stack"
)
func main() {
for i := 0; i < 5; i++ {
stack.PushStack() // push current heap pointer to the stack
arr := make([]byte, 100 * 1024 * 1024) //allocate as much as you want
arr[0] = byte(i)
stack.PopStack() // release all the memory allocated since the last PushStack
}
}
- tiny-go v0.30+
- Import the library
"github.com/metrico/micro-gc/stack"
into the go code - Add
stack.PushStack
andstack.PopStack
where you want - Build the wasm application with the
-gc=custom
flag
- Be very careful with
sync.Pool
and all the global variables.PopStack
may corrupt the data. - Be very careful with all the libraries using
sync.Pool
as well. Apparently,fmt.Println
uses it. - Be careful working with goroutines.
- Definitely not for multicore environments.
Feel free to get inspiration from the examples in the examples
directory.
Build an example like this:
tinygo build -gc=custom -target=wasm -o=test.wasm github.com/metrico/micro-gc/examples/array/
Then run it using nodeJS and wasm_exec file: node wasm_exec.js test.wasm