/autoreleasepool

autoreleasepool memory allocator for golang using "arena"

Primary LanguageGoApache License 2.0Apache-2.0

Autoreleasepool memory management for golang

License GoDoc Testing Status Go Report Card Coverage Status

Minimal version of golang 1.20

Most of the golang applications are web services, which are processing a lot of requests. In this case, the memory management is very important. The main problem is that the golang does not have a garbage collector, which can free the memory after the request is processed. The main idea of this package is to create a pool of autoreleasepools, which can be used to free the memory after the request is processed.

This package has been inspired by @autoreleasepool of Objective-C and Swift and can be used in the same way. All memory allocated in the autoreleasepool will be freed after the request is processed, wich can significantly speed up the application and reduce the garbage collector load.

The "arena" package is experimental and must be activated by GOEXPERIMENT=arenas.

Example

package main

import (
  "fmt"
  "context"

  "github.com/demdxx/autoreleasepool/httpautoreleasepool"
  "github.com/demdxx/autoreleasepool"
)

type RenderContext struct {
  name string
  age string
}

func indexHttpHandler(w http.ResponseWriter, r *http.Request) {
  ct := autoreleasepool.New[RenderContext](r.Context())
  ct.name = "Dem"
  ct.age = "30"
  fmt.Fprintf(w, "Hello, %s! You are %s years old.", ct.name, ct.age)
}

func main() {
  http.HandleFunc("/", httpautoreleasepool.Wrap(indexHttpHandler))
  http.ListenAndServe(":8080", nil)
}