github.com/songzhibin97/gkit@v1.2.13/watching/example/gcheap/gcheap.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "math/rand" 6 "net/http" 7 "time" 8 9 "github.com/songzhibin97/gkit/watching" 10 ) 11 12 func init() { 13 http.HandleFunc("/rand", randAlloc) 14 http.HandleFunc("/spike", spikeAlloc) 15 go http.ListenAndServe(":10024", nil) 16 } 17 18 func main() { 19 w := watching.NewWatching( 20 watching.WithCoolDown("10s"), 21 watching.WithDumpPath("./tmp"), 22 watching.WithBinaryDump(), 23 watching.WithMemoryLimit(100*1024*1024), // 100MB 24 watching.WithGCHeapDump(10, 20, 40), 25 ) 26 w.EnableGCHeapDump().Start() 27 time.Sleep(time.Hour) 28 } 29 30 var base = make([]byte, 1024*1024*10) // 10 MB long live memory. 31 32 func randAlloc(wr http.ResponseWriter, req *http.Request) { 33 s := make([][]byte, 0) // short live 34 for i := 0; i < 1024; i++ { 35 len := rand.Intn(1024) 36 bytes := make([]byte, len) 37 38 s = append(s, bytes) 39 40 if len == 0 { 41 s = make([][]byte, 0) 42 } 43 } 44 time.Sleep(time.Millisecond * 10) 45 fmt.Fprintf(wr, "slice current length: %v\n", len(s)) 46 } 47 48 func spikeAlloc(wr http.ResponseWriter, req *http.Request) { 49 s := make([][]byte, 0, 1024) // spike, 10MB 50 for i := 0; i < 10; i++ { 51 bytes := make([]byte, 1024*1024) 52 s = append(s, bytes) 53 } 54 // live for a while 55 time.Sleep(time.Millisecond * 500) 56 fmt.Fprintf(wr, "spike slice length: %v\n", len(s)) 57 }