-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
58 lines (51 loc) · 1.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"context"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"runtime/pprof"
"sync"
"time"
)
func oneWorkItem(_ context.Context, leakySliceEntry []string) {
fmt.Printf("Start: %v\n", time.Now())
// Memory
for i := 0; i < 500000; i++ {
leakySliceEntry = append(leakySliceEntry, "aaaa")
}
// Blocking
time.Sleep(2 * time.Second)
fmt.Printf("End: %v\n", time.Now())
}
// Some function that does work
func hardWork(ctx context.Context, wg *sync.WaitGroup, leakySlice [][]string) {
defer wg.Done()
var counter int
for {
pprof.Do(ctx,
pprof.Labels("instance", fmt.Sprintf("instance%d", counter)),
func(ctx context.Context) {
leakySliceEntry := make([]string, 0)
leakySlice = append(leakySlice, leakySliceEntry)
oneWorkItem(ctx, leakySliceEntry)
})
counter++
}
}
func main() {
var wg sync.WaitGroup
wg.Add(1)
go func() {
fmt.Println("Listening on localhost:6060 for pprof, use \"go tool pprof -seconds 5 http://localhost:6060/debug/pprof/heap\" to check heap profile.")
log.Printf("Listener started: %s\n", http.ListenAndServe("localhost:6060", nil))
fmt.Println("Done listening")
wg.Done()
}()
wg.Add(1)
ctx := pprof.WithLabels(context.Background(), pprof.Labels("name", "hardWork"))
var leakySlice [][]string
go hardWork(ctx, &wg, leakySlice)
wg.Wait()
}