Last active
August 29, 2015 14:03
-
-
Save icholy/5a9e895362e59eecf4e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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