1
2
3
4
5 package main
6
7 import (
8 "internal/synctest"
9 "runtime"
10 "runtime/metrics"
11 )
12
13
14
15
16
17
18
19
20 func numGCCycles() uint64 {
21 samples := []metrics.Sample{{Name: "/gc/cycles/total:gc-cycles"}}
22 metrics.Read(samples)
23 if samples[0].Value.Kind() == metrics.KindBad {
24 panic("metric not supported")
25 }
26 return samples[0].Value.Uint64()
27 }
28
29 func main() {
30 synctest.Run(func() {
31
32 p := new(int)
33 runtime.SetFinalizer(p, func(*int) {})
34
35 startingCycles := numGCCycles()
36 ch1 := make(chan *int)
37 ch2 := make(chan *int)
38 defer close(ch1)
39 go func() {
40 for i := range ch1 {
41 v := *i + 1
42 ch2 <- &v
43 }
44 }()
45 for {
46
47 for i := 0; i < 1000; i++ {
48 v := new(int)
49 *v = i
50
51 runtime.SetFinalizer(v, func(*int) {})
52 ch1 <- v
53 <-ch2
54 }
55
56
57
58 synctest.Wait()
59
60
61 if numGCCycles()-startingCycles > 1 {
62 break
63 }
64 }
65 })
66 println("success")
67 }
68
View as plain text