Source file
src/runtime/debug/heapdump_test.go
1
2
3
4
5 package debug_test
6
7 import (
8 "os"
9 "runtime"
10 . "runtime/debug"
11 "testing"
12 )
13
14 func TestWriteHeapDumpNonempty(t *testing.T) {
15 if runtime.GOOS == "js" {
16 t.Skipf("WriteHeapDump is not available on %s.", runtime.GOOS)
17 }
18 f, err := os.CreateTemp("", "heapdumptest")
19 if err != nil {
20 t.Fatalf("TempFile failed: %v", err)
21 }
22 defer os.Remove(f.Name())
23 defer f.Close()
24 WriteHeapDump(f.Fd())
25 fi, err := f.Stat()
26 if err != nil {
27 t.Fatalf("Stat failed: %v", err)
28 }
29 const minSize = 1
30 if size := fi.Size(); size < minSize {
31 t.Fatalf("Heap dump size %d bytes, expected at least %d bytes", size, minSize)
32 }
33 }
34
35 type Obj struct {
36 x, y int
37 }
38
39 func objfin(x *Obj) {
40
41 }
42
43 func TestWriteHeapDumpFinalizers(t *testing.T) {
44 if runtime.GOOS == "js" {
45 t.Skipf("WriteHeapDump is not available on %s.", runtime.GOOS)
46 }
47 f, err := os.CreateTemp("", "heapdumptest")
48 if err != nil {
49 t.Fatalf("TempFile failed: %v", err)
50 }
51 defer os.Remove(f.Name())
52 defer f.Close()
53
54
55 println("allocating objects")
56 x := &Obj{}
57 runtime.SetFinalizer(x, objfin)
58 y := &Obj{}
59 runtime.SetFinalizer(y, objfin)
60
61
62 println("starting gc")
63 runtime.GC()
64
65
66 println("starting dump")
67 WriteHeapDump(f.Fd())
68 println("done dump")
69 }
70
View as plain text