/memwatch

Trips with an event when the runtime memory usage of the process is over the limit and exits after a while.

Primary LanguageGoBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

memwatch

All Contributors

GoDoc License

Trips with an event when the runtime memory usage of the process is over the limit and exits after a while.

Installation

go get -u github/theodesp/memwatch

How To Use

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!
}

The code supports building under Go >= 1.8.

Contributors

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

LICENCE

Copyright © 2017 Theo Despoudis BSD license