Source file
test/finprofiled.go
1
2
3
4
5
6
7
8
9
10
11 package main
12
13 import (
14 "runtime"
15 "time"
16 "unsafe"
17 )
18
19 func main() {
20 runtime.MemProfileRate = 1
21
22
23
24
25 const (
26 N = 1 << 20
27 tinyBlockSize = 16
28 )
29 hold := make([]*int32, 0, N)
30 for i := 0; i < N; i++ {
31 x := new(int32)
32 if i%3 == 0 {
33 runtime.SetFinalizer(x, func(p *int32) {
34 hold = append(hold, p)
35 })
36 }
37 }
38
39
40
41 for i := 0; i < 5; i++ {
42 runtime.GC()
43 time.Sleep(10 * time.Millisecond)
44 }
45
46 var prof []runtime.MemProfileRecord
47 for {
48 if n, ok := runtime.MemProfile(prof, false); ok {
49 prof = prof[:n]
50 break
51 } else {
52 prof = make([]runtime.MemProfileRecord, n+10)
53 }
54 }
55
56 var totalBytes int64
57 for _, p := range prof {
58 bytes := p.AllocBytes - p.FreeBytes
59 nobj := p.AllocObjects - p.FreeObjects
60 if nobj == 0 {
61
62
63 continue
64 }
65 size := bytes / nobj
66 if size == tinyBlockSize {
67 totalBytes += bytes
68 }
69 }
70
71 if want := N*int64(unsafe.Sizeof(int32(0))) - 2*tinyBlockSize; totalBytes < want {
72 println("got", totalBytes, "want >=", want)
73 panic("some of the tiny objects are not profiled")
74 }
75
76 if len(hold) != 0 && hold[0] == nil {
77 panic("bad")
78 }
79 }
80
View as plain text