package mem import ( "encoding/json" "log" "math/rand" "os" "runtime" "strings" "testing" "time" ) var MemBuffer []byte func init() { log.SetOutput(os.Stdout) log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds) } func MemTest() { x := rand.Intn(1024 * 1024 * 1024 * 4) log.Println("alloc str", x, "(", x/1024/1024, "MB)") str := strings.Repeat("-", x) log.Println("str buf") nbs := []byte(str) // log.Println("append") // MemBuffer = append(MemBuffer, nbs...) x = len(MemBuffer) + len(nbs) log.Println("make buf", x, "(", x/1024/1024, "MB)") tbs := make([]byte, x) x = len(MemBuffer) log.Println("copy old", x, "(", x/1024/1024, "MB)") copy(tbs, MemBuffer) x = len(nbs) log.Println("slice buf") xbs := tbs[len(MemBuffer):] MemBuffer = tbs log.Println("copy new", x, "(", x/1024/1024, "MB)") copy(xbs, nbs) x = len(MemBuffer) log.Println("buf size", x, "(", x/1024/1024/1024, "GB)") //MemBuffer = []byte{} MemStats() } func MemStats() map[string]interface{} { var m runtime.MemStats runtime.ReadMemStats(&m) //bs, _ := json.Marshal(m) mm := map[string]interface{}{ "Alloc": m.Alloc, "HeapIdle": m.HeapIdle, "HeapInuse": m.HeapInuse, "HeapSys": m.HeapSys, "MCacheInuse": m.MCacheInuse, "MCacheSys": m.MCacheSys, "MSpanInuse": m.MSpanInuse, "MSpanSys": m.MSpanSys, "OtherSys": m.OtherSys, "StackInuse": m.StackInuse, "StackSys": m.StackSys, "Sys": m.Sys, } //json.Unmarshal(bs, &mm) // delete(mm, "PauseNs") // delete(mm, "PauseEnd") // delete(mm, "BySize") obs, _ := json.Marshal(mm) log.Println(string(obs)) return mm } func HugeMemTest() { x := 1024 * 1024 * 1024 * 30 log.Println("make buf", x, "(", x/1024/1024/1024, "GB)") tbs := make([]byte, x) log.Println("make buf ok", &tbs) MemStats() } func MapTest() map[string]interface{} { m := map[int][]byte{} n := 0 t := 0 for ; t < 1024*1024*1024*20; n += 1 { x := 1024 * 1024 * n bs := make([]byte, x) for i := 0; i < len(bs); i++ { bs[i] = byte(rand.Int()) } m[n] = bs t += x log.Println("map size", len(m), "content size", t, "(", t/1024/1024/1024, "GB)") MemStats() time.Sleep(1 * time.Millisecond) } mmm := MemStats() for ; n > 0; n -= 1 { t -= len(m[n]) delete(m, n) log.Println("map size", len(m), "content size", t, "(", t/1024/1024/1024, "GB)") MemStats() time.Sleep(1 * time.Millisecond) } return mmm } func Gogogo(n int, bsc []byte) []byte { x := 1024 * 1024 * n bs := make([]byte, x) log.Println(len(bs)) if n > 0 { bs = Gogogo(n+1, bs) bs = append(bs, bsc...) } return bs } func Test_MemoryAlloc(t *testing.T) { // gogogo(1, nil) mmm := MapTest() wait := true n := 0 for ; wait; n += 1 { //test() time.Sleep(1000 * time.Millisecond) if n%10 == 0 { bs, _ := json.Marshal(mmm) log.Println(string(bs)) } MemStats() } }