Allocate offheap memory in Go programs
Package offheap
provides a simple library for allocating offheap memory in
Go programs. Doing so is useful for allocating large []byte
that should not
be managed by the garbage collector.
The offheap
package can be installed by running
go get gophers.dev/pkgs/offheap
package main
import (
"fmt"
"github/shoenig/offheap"
)
func main() {
var m offheap.Memory
var e error
// allocate 5 MB
if m, e = offheap.New(5 * 1024 * 1024); e != nil {
panic(e)
}
fmt.Println("m[0]", m[0])
m[0] = 42
fmt.Println("m[0]", m[0])
if e = m.Unmap(); e != nil {
panic(e)
}
fmt.Println("finished")
}
Package offheap
is pretty minimalist, and so it's basically feature complete.
Bug fixes and good ideas though are always welcome, please just file an issue.
The gophers.dev/pkgs/offheap
module is open source under the BSD-3-Clause license.