// Prime the data structures by allocating one of // each block in order. After this, there should be // little reason to ask for more memory from the OS. func prime() { for i := 0; i < 16; i++ { b := runtime.Alloc(1 << uint(i)) runtime.Free(b) } for i := uintptr(0); i < 256; i++ { b := runtime.Alloc(i << 12) runtime.Free(b) } }
func main() { flag.Parse() // prime(); var blocks [1]struct { base *byte siz uintptr } for i := 0; i < 1<<10; i++ { if i%(1<<10) == 0 && *chatty { println(i) } b := rand.Int() % len(blocks) if blocks[b].base != nil { // println("Free", blocks[b].siz, blocks[b].base); runtime.Free(blocks[b].base) blocks[b].base = nil allocated -= uint64(blocks[b].siz) continue } siz := uintptr(rand.Int() >> (11 + rand.Uint32()%20)) base := runtime.Alloc(siz) // ptr := uintptr(syscall.BytePtr(base))+uintptr(siz/2); // obj, size, ref, ok := allocator.find(ptr); // if obj != base || *ref != 0 || !ok { // panicln("find", siz, obj, ref, ok); // } blocks[b].base = base blocks[b].siz = siz allocated += uint64(siz) // println("Alloc", siz, base); memset(base, 0xbb, siz) bigger() } }
func main() { runtime.Free(runtime.Alloc(1)) runtime.UpdateMemStats() if *chatty { fmt.Printf("%+v %v\n", runtime.MemStats, uint64(0)) } }
func main() { flag.Parse() runtime.MemStats.Alloc = 0 // ignore stacks 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 { panicln("no allocations but stats report", a, "bytes allocated") } b := runtime.Alloc(uintptr(j)) during := runtime.MemStats.Alloc runtime.Free(b) if a := runtime.MemStats.Alloc; a != 0 { panic("allocated ", j, ": wrong stats: during=", during, " after=", a, " (want 0)") } bigger() } if i%(1<<10) == 0 && *chatty { println(i) } if i == 0 { if *chatty { println("Primed", i) } // runtime.frozen = true; } } }
func main() { runtime.GC() // clean up garbage from init 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)) during := runtime.MemStats.Alloc runtime.Free(b) 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 } } }
func AllocAndFree(size, count int) { defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1)) if *chatty { fmt.Printf("size=%d count=%d ...\n", size, count) } runtime.ReadMemStats(stats) 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.ReadMemStats(stats) if stats.Sys > 1e9 { println("too much memory allocated") panic("fail") } } runtime.ReadMemStats(stats) 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.ReadMemStats(stats) if stats.Alloc != uint64(alloc-n) { println("free alloc got", stats.Alloc, "expected", alloc-n, "after free of", n) panic("fail") } if stats.Sys > 1e9 { println("too much memory allocated") panic("fail") } } runtime.ReadMemStats(stats) 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") } }
func main() { memstats := new(runtime.MemStats) runtime.Free(runtime.Alloc(1)) runtime.ReadMemStats(memstats) if *chatty { fmt.Printf("%+v %v\n", memstats, uint64(0)) } }
func newBagKV(size uint8) *bagKV { asize := sizeofSub*uintptr(size) + sizeofBagKV b := (*bagKV)(unsafe.Pointer(runtime.Alloc(asize))) b.occupied_ = size return b }
func newSpanKV(size uint16) *spanKV { asize := sizeofSub*uintptr(size) + sizeofSpanKV s := (*spanKV)(unsafe.Pointer(runtime.Alloc(asize))) s.size = size return s }