func main() { flag.Parse() malloc.GetStats().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 := malloc.GetStats().Alloc; a != 0 { panicln("no allocations but stats report", a, "bytes allocated") } b := malloc.Alloc(uintptr(j)) during := malloc.GetStats().Alloc malloc.Free(b) if a := malloc.GetStats().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) } // malloc.frozen = true; } } }
func TestCountMallocs(t *testing.T) { mallocs := 0 - malloc.GetStats().Mallocs for i := 0; i < 100; i++ { Sprintf("") } mallocs += malloc.GetStats().Mallocs Printf("mallocs per Sprintf(\"\"): %d\n", mallocs/100) mallocs = 0 - malloc.GetStats().Mallocs for i := 0; i < 100; i++ { Sprintf("xxx") } mallocs += malloc.GetStats().Mallocs Printf("mallocs per Sprintf(\"xxx\"): %d\n", mallocs/100) mallocs = 0 - malloc.GetStats().Mallocs for i := 0; i < 100; i++ { Sprintf("%x", i) } mallocs += malloc.GetStats().Mallocs Printf("mallocs per Sprintf(\"%%x\"): %d\n", mallocs/100) mallocs = 0 - malloc.GetStats().Mallocs for i := 0; i < 100; i++ { Sprintf("%x %x", i, i) } mallocs += malloc.GetStats().Mallocs Printf("mallocs per Sprintf(\"%%x %%x\"): %d\n", mallocs/100) }
func TestStringVectorNums(t *testing.T) { var v StringVector c := "" malloc.GC() m0 := *malloc.GetStats() v.Resize(memTestN, memTestN) for i := 0; i < memTestN; i++ { v.Set(i, c) } malloc.GC() m := *malloc.GetStats() v.Resize(0, 0) malloc.GC() n := m.Alloc - m0.Alloc t.Logf("%T.Push(%#v), n = %s: Alloc/n = %.2f\n", v, c, s(memTestN), float(n)/memTestN) }
func AllocAndFree(size, count int) { if *chatty { fmt.Printf("size=%d count=%d ...\n", size, count) } n1 := stats.Alloc for i := 0; i < count; i++ { b[i] = malloc.Alloc(uintptr(size)) base, n := malloc.Lookup(b[i]) if base != b[i] || !OkAmount(uintptr(size), n) { panicln("lookup failed: got", base, n, "for", b[i]) } if malloc.GetStats().Sys > 1e9 { panicln("too much memory allocated") } } 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 := malloc.Lookup(b[i]) if base != b[i] || !OkAmount(uintptr(size), n) { panicln("lookup failed: got", base, n, "for", b[i]) } malloc.Free(b[i]) if stats.Alloc != uint64(alloc-n) { panicln("free alloc got", stats.Alloc, "expected", alloc-n, "after free of", n) } if malloc.GetStats().Sys > 1e9 { panicln("too much memory allocated") } } n4 := stats.Alloc if *chatty { fmt.Printf("size=%d count=%d stats=%+v\n", size, count, *stats) } if n2-n1 != n3-n4 { panicln("wrong alloc count: ", n2-n1, n3-n4) } }
func bigger() { if st := malloc.GetStats(); oldsys < st.Sys { oldsys = st.Sys if *chatty { println(st.Sys, " system bytes for ", st.Alloc, " Go bytes") } if st.Sys > 1e9 { panicln("too big") } } }
func bigger() { if f := malloc.GetStats().Sys; footprint < f { footprint = f if *chatty { println("Footprint", footprint, " for ", allocated) } if footprint > 1e9 { panicln("too big") } } }
func (c *Clock) memUsageMsg(msg string) uint64 { usedMemoryKb := (malloc.GetStats().Alloc - c.startMem) / 1024 fmt.Printf("%s used %d [kb]\n", msg, usedMemoryKb) return usedMemoryKb }
func (c *Clock) start() { c.startTime = time.Nanoseconds(); c.startMem = malloc.GetStats().Alloc }
package main import ( "flag" "fmt" "malloc" "strconv" ) var chatty = flag.Bool("v", false, "chatty") var reverse = flag.Bool("r", false, "reverse") var longtest = flag.Bool("l", false, "long test") var b []*byte var stats = malloc.GetStats() func OkAmount(size, n uintptr) bool { if n < size { return false } if size < 16*8 { if n > size+16 { return false } } else { if n > size*9/8 { return false } } return true
func main() { malloc.Free(malloc.Alloc(1)) if *chatty { fmt.Printf("%+v %v\n", *malloc.GetStats(), uint64(0)) } }