mirror of
https://github.com/golang/go
synced 2024-09-24 19:30:12 -06:00
3572c6418b
Introduce a KeepAlive op which makes sure that its argument is kept live until the KeepAlive. Use KeepAlive to mark pointer input arguments as live after each function call and at each return. We do this change only for pointer arguments. Those are the critical ones to handle because they might have finalizers. Doing compound arguments (slices, structs, ...) is more complicated because we would need to track field liveness individually (we do that for auto variables now, but inputs requires extra trickery). Turn off the automatic marking of args as live. That way, when args are explicitly nulled, plive will know that the original argument is dead. The KeepAlive op will be the eventual implementation of runtime.KeepAlive. Fixes #15277 Change-Id: I5f223e65d99c9f8342c03fbb1512c4d363e903e5 Reviewed-on: https://go-review.googlesource.com/22365 Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Russ Cox <rsc@golang.org>
38 lines
789 B
Go
38 lines
789 B
Go
// run
|
|
|
|
// Copyright 2016 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import "runtime"
|
|
|
|
type big [10 << 20]byte
|
|
|
|
func f(x *big, start int64) {
|
|
if delta := inuse() - start; delta < 9<<20 {
|
|
println("after alloc: expected delta at least 9MB, got: ", delta)
|
|
}
|
|
x = nil
|
|
if delta := inuse() - start; delta > 1<<20 {
|
|
println("after drop: expected delta below 1MB, got: ", delta)
|
|
}
|
|
x = new(big)
|
|
if delta := inuse() - start; delta < 9<<20 {
|
|
println("second alloc: expected delta at least 9MB, got: ", delta)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
x := inuse()
|
|
f(new(big), x)
|
|
}
|
|
|
|
func inuse() int64 {
|
|
runtime.GC()
|
|
var st runtime.MemStats
|
|
runtime.ReadMemStats(&st)
|
|
return int64(st.Alloc)
|
|
}
|