當前位置: 首頁>>代碼示例>>Golang>>正文


Golang runtime.UpdateMemStats函數代碼示例

本文整理匯總了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")
	}
}
開發者ID:anuvazhayil,項目名稱:HelloWorld_32bitOS,代碼行數:35,代碼來源:closure.go

示例2: numAllocations

func numAllocations(f func()) int {
	runtime.UpdateMemStats()
	n0 := runtime.MemStats.Mallocs
	f()
	runtime.UpdateMemStats()
	return int(runtime.MemStats.Mallocs - n0)
}
開發者ID:vkargov,項目名稱:gcc,代碼行數:7,代碼來源:itoa_test.go

示例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)
}
開發者ID:aubonbeurre,項目名稱:gcc,代碼行數:25,代碼來源:timing_test.go

示例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")
	}
}
開發者ID:WXB506,項目名稱:golang,代碼行數:58,代碼來源:mallocrep1.go

示例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)
		}
	}
}
開發者ID:vkargov,項目名稱:gcc,代碼行數:15,代碼來源:fmt_test.go

示例6: main

func main() {
	runtime.Free(runtime.Alloc(1))
	runtime.UpdateMemStats()
	if *chatty {
		fmt.Printf("%+v %v\n", runtime.MemStats, uint64(0))
	}
}
開發者ID:WXB506,項目名稱:golang,代碼行數:7,代碼來源:malloc1.go

示例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)
	}
}
開發者ID:krasin,項目名稱:go-deflate,代碼行數:21,代碼來源:gc_test.go

示例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")
	}
}
開發者ID:anuvazhayil,項目名稱:HelloWorld_32bitOS,代碼行數:22,代碼來源:init1.go

示例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")
		}
	}
}
開發者ID:WXB506,項目名稱:golang,代碼行數:13,代碼來源:mallocrand.go

示例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")
		}
	}
}
開發者ID:vkargov,項目名稱:gcc,代碼行數:13,代碼來源:mallocrep.go

示例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)
	}
}
開發者ID:Quantumboost,項目名稱:gcc,代碼行數:13,代碼來源:gc_test.go

示例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
}
開發者ID:anuvazhayil,項目名稱:HelloWorld_32bitOS,代碼行數:13,代碼來源:tree2.go

示例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
		}
	}
}
開發者ID:vkargov,項目名稱:gcc,代碼行數:37,代碼來源:mallocrep.go

示例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
}
開發者ID:Quantumboost,項目名稱:gcc,代碼行數:24,代碼來源:server_test.go

示例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)
}
開發者ID:xinghuwang,項目名稱:toolbox,代碼行數:36,代碼來源:goroutine_limit.go


注:本文中的runtime.UpdateMemStats函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。