Skip to content

Instantly share code, notes, and snippets.

@icholy
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icholy/5a9e895362e59eecf4e6 to your computer and use it in GitHub Desktop.
Save icholy/5a9e895362e59eecf4e6 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"runtime/pprof"
"sync"
)
var (
total int
mutex sync.Mutex
)
// allocate a megabyte in a goroutine that blocks forever
func allocateMegabyte() int {
go func() {
var mb [131072]int64
_ = mb
select {}
}()
mutex.Lock()
defer mutex.Unlock()
total++
return total
}
func main() {
http.HandleFunc("/allocate", func(w http.ResponseWriter, r *http.Request) {
tot := allocateMegabyte()
if _, err := fmt.Fprintf(w, "total: %d mb", tot); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
http.HandleFunc("/dump", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/binary")
if err := pprof.WriteHeapProfile(w); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment