本文整理汇总了Golang中runtime.UpdateMemStats函数的典型用法代码示例。如果您正苦于以下问题:Golang UpdateMemStats函数的具体用法?Golang UpdateMemStats怎么用?Golang UpdateMemStats使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了UpdateMemStats函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
var fail bool
go f()
check([]int{1, 4, 5, 4})
a := accum(0)
b := accum(1)
go g(a, b)
check([]int{2, 4, 6, 9})
go h()
check([]int{100, 200, 101, 201, 500, 101, 201, 500})
runtime.UpdateMemStats()
n0 := runtime.MemStats.Mallocs
x, y := newfunc(), newfunc()
if x(1) != 1 || y(2) != 2 {
println("newfunc returned broken funcs")
fail = true
}
runtime.UpdateMemStats()
if n0 != runtime.MemStats.Mallocs {
println("newfunc allocated unexpectedly")
fail = true
}
ff(1)
if fail {
panic("fail")
}
}
示例2: numAllocations
func numAllocations(f func()) int {
runtime.UpdateMemStats()
n0 := runtime.MemStats.Mallocs
f()
runtime.UpdateMemStats()
return int(runtime.MemStats.Mallocs - n0)
}
示例3: TestCountDecodeMallocs
func TestCountDecodeMallocs(t *testing.T) {
var buf bytes.Buffer
enc := NewEncoder(&buf)
bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")}
const count = 1000
for i := 0; i < count; i++ {
err := enc.Encode(bench)
if err != nil {
t.Fatal("encode:", err)
}
}
dec := NewDecoder(&buf)
runtime.UpdateMemStats()
mallocs := 0 - runtime.MemStats.Mallocs
for i := 0; i < count; i++ {
*bench = Bench{}
err := dec.Decode(&bench)
if err != nil {
t.Fatal("decode:", err)
}
}
runtime.UpdateMemStats()
mallocs += runtime.MemStats.Mallocs
fmt.Printf("mallocs per decode of type Bench: %d\n", mallocs/count)
}
示例4: AllocAndFree
func AllocAndFree(size, count int) {
if *chatty {
fmt.Printf("size=%d count=%d ...\n", size, count)
}
runtime.UpdateMemStats()
n1 := stats.Alloc
for i := 0; i < count; i++ {
b[i] = runtime.Alloc(uintptr(size))
base, n := runtime.Lookup(b[i])
if base != b[i] || !OkAmount(uintptr(size), n) {
println("lookup failed: got", base, n, "for", b[i])
panic("fail")
}
runtime.UpdateMemStats()
if stats.Sys > 1e9 {
println("too much memory allocated")
panic("fail")
}
}
runtime.UpdateMemStats()
n2 := stats.Alloc
if *chatty {
fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats)
}
n3 := stats.Alloc
for j := 0; j < count; j++ {
i := j
if *reverse {
i = count - 1 - j
}
alloc := uintptr(stats.Alloc)
base, n := runtime.Lookup(b[i])
if base != b[i] || !OkAmount(uintptr(size), n) {
println("lookup failed: got", base, n, "for", b[i])
panic("fail")
}
runtime.Free(b[i])
runtime.UpdateMemStats()
if stats.Alloc != uint64(alloc-n) {
println("free alloc got", stats.Alloc, "expected", alloc-n, "after free of", n)
panic("fail")
}
if runtime.MemStats.Sys > 1e9 {
println("too much memory allocated")
panic("fail")
}
}
runtime.UpdateMemStats()
n4 := stats.Alloc
if *chatty {
fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats)
}
if n2-n1 != n3-n4 {
println("wrong alloc count: ", n2-n1, n3-n4)
panic("fail")
}
}
示例5: TestCountMallocs
func TestCountMallocs(t *testing.T) {
for _, mt := range mallocTest {
const N = 100
runtime.UpdateMemStats()
mallocs := 0 - runtime.MemStats.Mallocs
for i := 0; i < N; i++ {
mt.fn()
}
runtime.UpdateMemStats()
mallocs += runtime.MemStats.Mallocs
if mallocs/N > uint64(mt.count) {
t.Errorf("%s: expected %d mallocs, got %d", mt.desc, mt.count, mallocs/N)
}
}
}
示例6: main
func main() {
runtime.Free(runtime.Alloc(1))
runtime.UpdateMemStats()
if *chatty {
fmt.Printf("%+v %v\n", runtime.MemStats, uint64(0))
}
}
示例7: TestGcSys
func TestGcSys(t *testing.T) {
runtime.GC()
runtime.UpdateMemStats()
sys := runtime.MemStats.Sys
for i := 0; i < 1000000; i++ {
workthegc()
}
// Should only be using a few MB.
runtime.UpdateMemStats()
if sys > runtime.MemStats.Sys {
sys = 0
} else {
sys = runtime.MemStats.Sys - sys
}
t.Logf("used %d extra bytes", sys)
if sys > 4<<20 {
t.Fatalf("using too much memory: %d bytes", sys)
}
}
示例8: init
func init() {
c := make(chan int)
go send(c)
<-c
const chunk = 1 << 20
runtime.UpdateMemStats()
sys := runtime.MemStats.Sys
b := make([]byte, chunk)
for i := range b {
b[i] = byte(i%10 + '0')
}
s := string(b)
for i := 0; i < 1000; i++ {
x = []byte(s)
}
runtime.UpdateMemStats()
sys1 := runtime.MemStats.Sys
if sys1-sys > chunk*50 {
println("allocated 1000 chunks of", chunk, "and used ", sys1-sys, "memory")
}
}
示例9: bigger
func bigger() {
runtime.UpdateMemStats()
if f := runtime.MemStats.Sys; footprint < f {
footprint = f
if *chatty {
println("Footprint", footprint, " for ", allocated)
}
if footprint > 1e9 {
println("too big")
panic("fail")
}
}
}
示例10: bigger
func bigger() {
runtime.UpdateMemStats()
if st := runtime.MemStats; oldsys < st.Sys {
oldsys = st.Sys
if *chatty {
println(st.Sys, " system bytes for ", st.Alloc, " Go bytes")
}
if st.Sys > 1e9 {
println("too big")
panic("fail")
}
}
}
示例11: TestGcSys
func TestGcSys(t *testing.T) {
for i := 0; i < 1000000; i++ {
workthegc()
}
// Should only be using a few MB.
runtime.UpdateMemStats()
sys := runtime.MemStats.Sys
t.Logf("using %d MB", sys>>20)
if sys > 10e6 {
t.Fatalf("using too much memory: %d MB", sys>>20)
}
}
示例12: gc
func gc() {
runtime.GC()
runtime.UpdateMemStats()
pause := runtime.MemStats.PauseTotalNs
inuse := runtime.MemStats.Alloc
free := runtime.MemStats.TotalAlloc - inuse
fmt.Printf("gc pause: %8.3f ms; collect: %8.0f MB; heapsize: %8.0f MB\n",
float64(pause-lastPauseNs)/1e6,
float64(free-lastFree)/1048576,
float64(inuse)/1048576)
lastPauseNs = pause
lastFree = free
}
示例13: main
func main() {
runtime.GC() // clean up garbage from init
runtime.UpdateMemStats() // first call can do some allocations
runtime.MemProfileRate = 0 // disable profiler
runtime.MemStats.Alloc = 0 // ignore stacks
flag.Parse()
for i := 0; i < 1<<7; i++ {
for j := 1; j <= 1<<22; j <<= 1 {
if i == 0 && *chatty {
println("First alloc:", j)
}
if a := runtime.MemStats.Alloc; a != 0 {
println("no allocations but stats report", a, "bytes allocated")
panic("fail")
}
b := runtime.Alloc(uintptr(j))
runtime.UpdateMemStats()
during := runtime.MemStats.Alloc
runtime.Free(b)
runtime.UpdateMemStats()
if a := runtime.MemStats.Alloc; a != 0 {
println("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)")
panic("fail")
}
bigger()
}
if i%(1<<10) == 0 && *chatty {
println(i)
}
if i == 0 {
if *chatty {
println("Primed", i)
}
// runtime.frozen = true
}
}
}
示例14: countMallocs
func countMallocs(dial func() (*Client, os.Error), t *testing.T) uint64 {
once.Do(startServer)
client, err := dial()
if err != nil {
t.Fatal("error dialing", err)
}
args := &Args{7, 8}
reply := new(Reply)
runtime.UpdateMemStats()
mallocs := 0 - runtime.MemStats.Mallocs
const count = 100
for i := 0; i < count; i++ {
err := client.Call("Arith.Add", args, reply)
if err != nil {
t.Errorf("Add: expected no error but got string %q", err.String())
}
if reply.C != args.A+args.B {
t.Errorf("Add: expected %d got %d", reply.C, args.A+args.B)
}
}
runtime.UpdateMemStats()
mallocs += runtime.MemStats.Mallocs
return mallocs / count
}
示例15: main
func main() {
flag.Parse()
if *n <= 0 {
fmt.Fprintf(os.Stderr, "invalid number of goroutines")
os.Exit(1)
}
// Limit the number of spare OS threads to just 1
runtime.GOMAXPROCS(1)
// Make a copy of MemStats
runtime.UpdateMemStats()
m0 := runtime.MemStats
t0 := time.Nanoseconds()
for i := 0; i < *n; i++ {
go f()
}
runtime.Gosched()
t1 := time.Nanoseconds()
runtime.GC()
// Make a copy of MemStats
runtime.UpdateMemStats()
m1 := runtime.MemStats
if counter != *n {
fmt.Fprintf(os.Stderr, "failed to begin execution of all goroutines")
os.Exit(1)
}
fmt.Printf("Number of goroutines: %d\n", *n)
fmt.Printf("Per goroutine:\n")
fmt.Printf(" Memory: %.2f bytes\n", float64(m1.Sys-m0.Sys)/float64(*n))
fmt.Printf(" Time: %f µs\n", float64(t1-t0)/float64(*n)/1e3)
}