mirror of
https://github.com/golang/go
synced 2024-09-05 16:04:50 +00:00
![Keith Randall](/assets/img/avatar_default.png)
We're dropping this behavior in favor of runtime.KeepAlive. Implement runtime.KeepAlive as an intrinsic. Update #15843 Change-Id: Ib60225bd30d6770ece1c3c7d1339a06aa25b1cbc Reviewed-on: https://go-review.googlesource.com/28310 Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com>
41 lines
849 B
Go
41 lines
849 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.
|
|
// +build amd64
|
|
|
|
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)
|
|
}
|
|
runtime.KeepAlive(x)
|
|
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)
|
|
}
|
|
runtime.KeepAlive(x)
|
|
}
|
|
|
|
func main() {
|
|
x := inuse()
|
|
f(new(big), x)
|
|
}
|
|
|
|
func inuse() int64 {
|
|
runtime.GC()
|
|
var st runtime.MemStats
|
|
runtime.ReadMemStats(&st)
|
|
return int64(st.Alloc)
|
|
}
|