codekingpro/portable-devtools
114k
1// run2 3// Copyright 2018 The Go Authors. All rights reserved.4// Use of this source code is governed by a BSD-style5// license that can be found in the LICENSE file.6 7package main8 9import (10 "fmt"11 "runtime"12)13 14type HeapObj [8]int6415 16type StkObj struct {17 h *HeapObj18}19 20var n int21var c int = -122 23func gc() {24 // encourage heap object to be collected, and have its finalizer run.25 runtime.GC()26 runtime.GC()27 runtime.GC()28 n++29}30 31func main() {32 f()33 gc() // prior to stack objects, heap object is not collected until here34 if c < 0 {35 panic("heap object never collected")36 }37 if c != 1 {38 panic(fmt.Sprintf("expected collection at phase 1, got phase %d", c))39 }40}41 42func f() {43 var s StkObj44 s.h = new(HeapObj)45 runtime.SetFinalizer(s.h, func(h *HeapObj) {46 // Remember at what phase the heap object was collected.47 c = n48 })49 g(&s)50 gc()51}52 53func g(s *StkObj) {54 gc() // heap object is still live here55 runtime.KeepAlive(s)56 gc() // heap object should be collected here57}58 