解析方案

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// 替换 ioutil.ReadAll
func ReadAll(data io.ReadCloser) (body []byte) {
	buffer := bytes.NewBuffer(make([]byte, 0, 65536))
	io.Copy(buffer, data)
	temp := buffer.Bytes()
	length := len(temp)
	if cap(temp) > (length + length/10) {
		body = make([]byte, length)
		copy(body, temp)
	} else {
		body = temp
	}
	return
}

参考

[golang]内存不断增长bytes.makeSlice

Golang Slices And The Case Of The Missing Memory