Skip to content

Instantly share code, notes, and snippets.

@jasonkeene
Created October 1, 2017 01:33
Show Gist options
  • Save jasonkeene/b4f9e95c5e47b8e94652b095460fc35a to your computer and use it in GitHub Desktop.
Save jasonkeene/b4f9e95c5e47b8e94652b095460fc35a to your computer and use it in GitHub Desktop.
Test program for playing with pprof's heap profiler.
package main
import (
"log"
"math/rand"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"runtime/debug"
"time"
)
var store [][]byte
func storeReference(b []byte) {
store = append(store, b)
}
func removeReferences() {
store = nil
debug.FreeOSMemory()
}
func escapedAllocation() []byte {
b := make([]byte, 65536)
rand.Read(b)
return b
}
func leak() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
for {
if done(sigint) {
log.Print("clearing all references")
removeReferences()
break
}
storeReference(escapedAllocation())
time.Sleep(time.Millisecond)
}
<-sigint
os.Exit(0)
}
func done(sigint chan os.Signal) bool {
select {
case <-sigint:
return true
default:
return false
}
}
func main() {
go leak()
log.Print("pprof listening on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment