本文整理汇总了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()
}
示例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)
}
}
示例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)
}
示例4: f
func f() (r, s *T) {
r = &T{0x30, 0x31, 0x32, 0x33}
runtime.GC()
s = &T{0x40, 0x41, 0x42, 0x43}
runtime.GC()
return
}
示例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)
}
示例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)
}
}
示例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
}
示例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)
}
}
示例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
}
示例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
}
示例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()
}
示例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)
}
示例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)
}
}
示例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++
}
}
示例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)
}
}