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


Golang runtime.GC函數代碼示例

本文整理匯總了Golang中runtime.GC函數的典型用法代碼示例。如果您正苦於以下問題:Golang GC函數的具體用法?Golang GC怎麽用?Golang GC使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了GC函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: MallocDisablePool

func MallocDisablePool(ebs map[int]*ElasticBuf, pool *MemPool) {
	runtime.GC()
	runtime.GC()
	pprof()
	for i := 0; i <= 100000; i++ {
		ebs[i] = NewElasticBuf(1, pool)
	}
	ebs = make(map[int]*ElasticBuf)
	runtime.GC()
	runtime.GC()
}
開發者ID:shitfSign,項目名稱:gorpc,代碼行數:11,代碼來源:memPool_test.go

示例2: TestRuntimeMemStats

func TestRuntimeMemStats(t *testing.T) {
	r := NewRegistry()
	RegisterRuntimeMemStats(r)
	CaptureRuntimeMemStatsOnce(r)
	zero := runtimeMetrics.MemStats.PauseNs.Count() // Get a "zero" since GC may have run before these tests.
	runtime.GC()
	CaptureRuntimeMemStatsOnce(r)
	if count := runtimeMetrics.MemStats.PauseNs.Count(); 1 != count-zero {
		t.Fatal(count - zero)
	}
	runtime.GC()
	runtime.GC()
	CaptureRuntimeMemStatsOnce(r)
	if count := runtimeMetrics.MemStats.PauseNs.Count(); 3 != count-zero {
		t.Fatal(count - zero)
	}
	for i := 0; i < 256; i++ {
		runtime.GC()
	}
	CaptureRuntimeMemStatsOnce(r)
	if count := runtimeMetrics.MemStats.PauseNs.Count(); 259 != count-zero {
		t.Fatal(count - zero)
	}
	for i := 0; i < 257; i++ {
		runtime.GC()
	}
	CaptureRuntimeMemStatsOnce(r)
	if count := runtimeMetrics.MemStats.PauseNs.Count(); 515 != count-zero { // We lost one because there were too many GCs between captures.
		t.Fatal(count - zero)
	}
}
開發者ID:scozss,項目名稱:goperfcounter,代碼行數:31,代碼來源:runtime_test.go

示例3: benchCheck

func benchCheck() {
	fn := func(name string, encfn benchFn, decfn benchFn) {
		benchBs = benchBs[0:0]
		buf := bytes.NewBuffer(benchBs)
		var err error
		runtime.GC()
		tnow := time.Now()
		if err = encfn(buf, &benchTs); err != nil {
			logT(nil, "\t%10s: **** Error encoding benchTs: %v", name, err)
		}
		encDur := time.Now().Sub(tnow)
		encLen := buf.Len()
		//log("\t%10s: encLen: %v, len: %v, cap: %v\n", name, encLen, len(benchBs), cap(benchBs))
		buf = bytes.NewBuffer(benchBs[0:encLen])
		runtime.GC()
		tnow = time.Now()
		if err = decfn(buf, new(TestStruc)); err != nil {
			logT(nil, "\t%10s: **** Error decoding into new TestStruc: %v", name, err)
		}
		decDur := time.Now().Sub(tnow)
		logT(nil, "\t%10s: Encode Size: %5d, Encode Time: %8v, Decode Time: %8v", name, encLen, encDur, decDur)
	}
	logT(nil, "Benchmark One-Pass Unscientific Marshal Sizes:")
	fn("msgpack", fnMsgpackEncodeFn, fnMsgpackDecodeFn)
	fn("gob", fnGobEncodeFn, fnGobDecodeFn)
	fn("bson", fnBsonEncodeFn, fnBsonDecodeFn)
	fn("json", fnJsonEncodeFn, fnJsonDecodeFn)
}
開發者ID:amilarcap,項目名稱:go-msgpack,代碼行數:28,代碼來源:msgpack_bench_test.go

示例4: f

func f() (r, s *T) {
	r = &T{0x30, 0x31, 0x32, 0x33}
	runtime.GC()
	s = &T{0x40, 0x41, 0x42, 0x43}
	runtime.GC()
	return
}
開發者ID:Xiahl1990,項目名稱:go,代碼行數:7,代碼來源:issue14591.go

示例5: BenchmarkWatch

func BenchmarkWatch(b *testing.B) {
	b.StopTimer()
	s := newStore()
	kvs, _ := generateNRandomKV(b.N, 128)
	b.StartTimer()

	memStats := new(runtime.MemStats)
	runtime.GC()
	runtime.ReadMemStats(memStats)

	for i := 0; i < b.N; i++ {
		w, _ := s.Watch(kvs[i][0], false, false, 0)

		e := newEvent("set", kvs[i][0], uint64(i+1), uint64(i+1))
		s.WatcherHub.notify(e)
		<-w.EventChan
		s.CurrentIndex++
	}

	s.WatcherHub.EventHistory = nil
	afterMemStats := new(runtime.MemStats)
	runtime.GC()
	runtime.ReadMemStats(afterMemStats)
	fmt.Printf("\nBefore Alloc: %v; After Alloc: %v\n",
		memStats.Alloc/1000, afterMemStats.Alloc/1000)
}
開發者ID:Rozandas,項目名稱:etcd,代碼行數:26,代碼來源:store_bench_test.go

示例6: main

func main() {
	runtime.MemProfileRate = 0

	c := make(chan int)
	dummy := make(chan int)

	// warm up
	go sender(c, 100000)
	receiver(c, dummy, 100000)
	runtime.GC()
	memstats := new(runtime.MemStats)
	runtime.ReadMemStats(memstats)
	alloc := memstats.Alloc

	// second time shouldn't increase footprint by much
	go sender(c, 100000)
	receiver(c, dummy, 100000)
	runtime.GC()
	runtime.ReadMemStats(memstats)

	// Be careful to avoid wraparound.
	if memstats.Alloc > alloc && memstats.Alloc-alloc > 1.1e5 {
		println("BUG: too much memory for 100,000 selects:", memstats.Alloc-alloc)
	}
}
開發者ID:2thetop,項目名稱:go,代碼行數:25,代碼來源:select2.go

示例7: sortUrls

func sortUrls(urlHits *map[Key]HitCount) (Elems, uint64, string) {
	uniqueUrlsCount := len(*urlHits)
	sortedUrls := make(Elems, 0, uniqueUrlsCount)

	var largestHit uint64 = 0
	var largestHitURL string = ""

	if showHumanStatistics && aggregateBy == "url" {
		for key, value := range *urlHits {

			if uint64(value) > uint64(largestHit) {
				largestHit = uint64(value)

				largestHitURL = string(key)
			}

			sortedUrls = append(sortedUrls, &Elem{key, value})
		}
	} else {
		for key, value := range *urlHits {
			sortedUrls = append(sortedUrls, &Elem{key, value})
		}
	}

	*urlHits = make(map[Key]HitCount)

	runtime.GC()

	sort.Sort(ByReverseCount{sortedUrls})

	runtime.GC()

	return sortedUrls, largestHit, largestHitURL
}
開發者ID:sdgdsffdsfff,項目名稱:logalyzer,代碼行數:34,代碼來源:logalyzer.go

示例8: main

func main() {
	const N = 10000
	st := new(runtime.MemStats)
	memstats := new(runtime.MemStats)
	runtime.ReadMemStats(st)
	for i := 0; i < N; i++ {
		c := make(chan int, 10)
		_ = c
		if i%100 == 0 {
			for j := 0; j < 4; j++ {
				runtime.GC()
				runtime.Gosched()
				runtime.GC()
				runtime.Gosched()
			}
		}
	}

	runtime.ReadMemStats(memstats)
	obj := memstats.HeapObjects - st.HeapObjects
	if obj > N/5 {
		fmt.Println("too many objects left:", obj)
		os.Exit(1)
	}
}
開發者ID:RajibTheKing,項目名稱:gcc,代碼行數:25,代碼來源:gc2.go

示例9: doIndex

func doIndex() bool {
	idxSegm, err := gcse.IndexSegments.GenMaxSegment()
	if err != nil {
		log.Printf("GenMaxSegment failed: %v", err)
		return false
	}

	runtime.GC()
	gcse.DumpMemStats()

	log.Printf("Indexing to %v ...", idxSegm)

	fpDocDB := sophie.LocalFsPath(configs.DocsDBPath().S())
	ts, err := gcse.Index(kv.DirInput(fpDocDB), idxSegm.Join("").S())
	if err != nil {
		log.Printf("Indexing failed: %v", err)
		return false
	}

	if !func() bool {
		f, err := idxSegm.Join(gcse.IndexFn).Create()
		if err != nil {
			log.Printf("Create index file failed: %v", err)
			return false
		}
		defer f.Close()

		log.Printf("Saving index to %v ...", idxSegm)
		if err := ts.Save(f); err != nil {
			log.Printf("ts.Save failed: %v", err)
			return false
		}
		return true
	}() {
		return false
	}
	runtime.GC()
	gcse.DumpMemStats()

	storePath := idxSegm.Join(configs.FnStore)
	log.Printf("Saving store snapshot to %v", storePath)
	if err := store.SaveSnapshot(storePath.S()); err != nil {
		log.Printf("SaveSnapshot %v failed: %v", storePath, err)
	}

	if err := idxSegm.Done(); err != nil {
		log.Printf("segm.Done failed: %v", err)
		return false
	}

	log.Printf("Indexing success: %s (%d)", idxSegm, ts.DocCount())
	gcse.AddBiValueAndProcess(bi.Average, "index.doc-count", ts.DocCount())

	ts = nil
	gcse.DumpMemStats()
	runtime.GC()
	gcse.DumpMemStats()

	return true
}
開發者ID:xavieryang007,項目名稱:gcse,代碼行數:60,代碼來源:index.go

示例10: threadList

func threadList(bl []Board, cpu int) []interface{} {
	tlist := make([]interface{}, 0, 400000)
	ch := make(chan MiniThread, cpu*16)
	sync := make(chan bool, cpu)
	go func() {
		for {
			if data := <-ch; data.Sure != "" {
				tlist = append(tlist, data)
			} else {
				break
			}
		}
	}()
	for _, it := range bl {
		sync <- true
		runtime.GC()
		go func() {
			threadThread(it, ch)
			<-sync
		}()
	}
	for cpu > 0 {
		sync <- true
		runtime.GC()
		cpu--
	}
	close(ch)
	close(sync)
	return tlist
}
開發者ID:tanaton,項目名稱:mamire-go,代碼行數:30,代碼來源:mamire.go

示例11: Test_Bind_Function

func Test_Bind_Function(t *testing.T) {
	template := engine.NewObjectTemplate()

	goFunc1 := func(text string, obj *Object, callback *Function) {
		t.Log("fetch")
		for i := 0; i < 10; i++ {
			t.Log(i)
			callback.Call(engine.NewString(text), obj.Value)
			runtime.GC()
		}
	}

	goFunc2 := func(text1, text2 string) {
		t.Logf("print(%s, %s)", text1, text2)
	}

	template.Bind("fetch", goFunc1)

	template.Bind("print", goFunc2)

	engine.NewContext(template).Scope(func(cs ContextScope) {
		cs.Eval(`
		var testObj = {Name: function() {
			return "test object"
		}};
		fetch("test", testObj, function(text, obj) {
			print(text, obj.Name())
		});`)
	})

	runtime.GC()
}
開發者ID:navy1125,項目名稱:v8.go,代碼行數:32,代碼來源:v8_binding_test.go

示例12: BenchmarkExtract

func BenchmarkExtract(b *testing.B) {
	config, err := ioutil.ReadFile("test/recursor_stats.json")
	if err != nil {
		b.Fatalf("could not read config file: %v", err.Error())
	}

	h := newPowerDNS(config)
	defer h.Close()

	hostURL, _ := url.Parse(h.URL)

	e := NewExporter("12345", "recursor", hostURL)

	var before, after runtime.MemStats
	runtime.GC()
	runtime.ReadMemStats(&before)
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		ch := make(chan prometheus.Metric)
		go func(ch chan prometheus.Metric) {
			for _ = range ch {
			}
		}(ch)

		e.Collect(ch)
		close(ch)
	}

	runtime.GC()
	runtime.ReadMemStats(&after)

	b.Logf("%d bytes used after %d runs", after.Alloc-before.Alloc, b.N)
}
開發者ID:janeczku,項目名稱:powerdns_exporter,代碼行數:34,代碼來源:powerdns_exporter_test.go

示例13: TestPauses

func TestPauses(t *testing.T) {
	p := NewPauses(1024)

	// Reset GC count
	p.Update()
	p.Snapshot()

	// Run GC once
	runtime.GC()
	p.Update()
	if count := len(p.Snapshot()); count != 1 {
		t.Fatalf("captured %d gc runs, expect 1", count)
	}

	// Run GC twice
	runtime.GC()
	runtime.GC()
	p.Update()
	if count := len(p.Snapshot()); count != 2 {
		t.Fatalf("captured %d gc runs, expected 2", count)
	}

	// Wraps GC counts
	for i := 0; i < 257; i++ {
		runtime.GC()
	}
	p.Update()
	if count := len(p.Snapshot()); count != 256 {
		t.Fatalf("captured %d gc runs, expected 256", count)
	}
}
開發者ID:heroku,項目名稱:instruments,代碼行數:31,代碼來源:runtime_test.go

示例14: testCacheGetMemoryLeak

// Cache memory leak for get
func testCacheGetMemoryLeak() {
	pc.OverrideLeaseSeconds(1)
	defer pc.OverrideLeaseSeconds(0)

	var memstats runtime.MemStats
	var initAlloc, midAlloc, finalAlloc uint64
	longValue := strings.Repeat("this sentence is 30 char long\n", 30)

	// Run garbage collection and get memory stats
	runtime.GC()
	runtime.ReadMemStats(&memstats)
	initAlloc = memstats.Alloc

	// Cache a lot of data
	for i := 0; i < 10000; i++ {
		key := fmt.Sprintf("keymemleakget:%d", i)
		pc.Reset()
		forceCacheGet(key, longValue)
		if pc.GetLeaseRequestCount() == 0 {
			LOGE.Println("FAIL: not requesting leases")
			failCount++
			return
		}
		pc.Reset()
		v, err := ls.Get(key)
		if checkError(err, false) {
			return
		}
		if v != longValue {
			LOGE.Println("FAIL: got wrong value")
			failCount++
			return
		}
		if pc.GetRpcCount() > 0 {
			LOGE.Println("FAIL: not caching data")
			failCount++
			return
		}
	}

	runtime.GC()
	runtime.ReadMemStats(&memstats)
	midAlloc = memstats.Alloc

	// Wait for data to expire and someone to cleanup
	time.Sleep(20 * time.Second)

	// Run garbage collection and get memory stats
	runtime.GC()
	runtime.ReadMemStats(&memstats)
	finalAlloc = memstats.Alloc

	if finalAlloc < initAlloc || (finalAlloc-initAlloc) < 5000000 {
		fmt.Fprintln(output, "PASS")
		passCount++
	} else {
		LOGE.Printf("FAIL: not cleaning cache - memory leak - init %d mid %d final %d\n", initAlloc, midAlloc, finalAlloc)
		failCount++
	}
}
開發者ID:CarterAppleton,項目名稱:p2,代碼行數:61,代碼來源:libtest.go

示例15: TestLiveness

func TestLiveness(t *testing.T) {
	dir := testutil.TempDir()
	defer os.RemoveAll(dir)
	root := nodefs.NewDefaultNode()
	s, _, err := nodefs.MountRoot(dir, root, nil)
	if err != nil {
		t.Fatalf("MountRoot: %v", err)
	}
	go s.Serve()
	if err := s.WaitMount(); err != nil {
		t.Fatal("WaitMount", err)
	}
	defer s.Unmount()

	if _, err := ioutil.ReadDir(dir); err != nil {
		t.Fatalf("ReadDir: %v", err)
	}

	// We previously encountered a sitation where a finalizer would close our fd out from under us. Try to force both finalizers to run and object destruction to complete.
	runtime.GC()
	runtime.GC()

	if _, err := ioutil.ReadDir(dir); err != nil {
		t.Fatalf("ReadDir: %v", err)
	}
}
開發者ID:jszwedko,項目名稱:ec2-metadatafs,代碼行數:26,代碼來源:mount_test.go


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