Trips with an event when the runtime memory usage of the process is over the limit and exits after a while.
go get -u github/theodesp/memwatch
package main
import (
"fmt"
"time"
"github.com/theodesp/memwatch"
)
func main() {
watch := memwatch.New(&memwatch.WatchConfig{
// Will cycle 10 times if memory is over the WarningLimit
Cycle: 10,
WarningLimit: 300 * memwatch.KiloByte,
// Once we reach the CriticalLimit though we bail immediately
CriticalLimit: 400 * memwatch.KiloByte,
// Will exit after 2 seconds
ExitTime: 2 * time.Second,
})
events := watch.Start()
// Watch for memory overload
boom := <-events
// boom you have 2 seconds to evacuate
fmt.Print(boom)
time.Sleep(10 * time.Second) // too late!
fmt.Println("clean up") // sorry won't happen!
}
- Start(): Starts the monitor
- Stop(): Stops the monitor
The code supports building under Go >= 1.8.
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
Copyright © 2017 Theo Despoudis BSD license