chunkCrc32Hash := crc32.NewIEEE()chunkBuf := bufferpool.GetInstance().Get()//回收bufferdefer bufferpool.GetInstance().Put(chunkBuf)writer := io.MultiWriter(chunkBuf, chunkCrc32Hash)_, copyErr := io.Copy(writer, chunkResp.RawResponse.Body)if copyErr != nil && copyErr != io.EOF { logmgr.ErrorLogger(ctx, "fail to write chunk data to crc32 or buffer, req: %s, err: %s", originReq.PrettyPrint(), copyErr) return false, exception.OEFException(nil, http.StatusInternalServerError, CopyChunkError, "internal server exception")}
Any item stored in the Pool may be removed automatically at any time without 18 // notification. If the Pool holds the only reference when this happens, the 19 // item might be deallocated.
<20200326-111844.png>
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/6dbe93e6-9bb7-4112-87cb-085e68d9d393%40googlegroups.com.
<20200326-111844.png>
type SyncPool struct{ Pool sync.Pool }
func (s *SyncPool) Get(n int) []byte {
//比n大,不需要slice增长时,使用buffer pool
if b, ok := s.Pool.Get().([]byte); ok && cap(b) >= n {
return b[:n]
} else {
//miss size
s.Put(b)
return make([]byte, n)
}
}
func (s *SyncPool) Put(b []byte) {
s.Pool.Put(b)
}
const FourMB = 4 * 1024 * 1024
var pool = &SyncPool{
sync.Pool{
New: func() interface{} {
return make([]byte, FourMB)
},
},
}
If the pool is a sync.Pool:Any item stored in the Pool may be removed automatically at any time without 18 // notification. If the Pool holds the only reference when this happens, the 19 // item might be deallocated.So placing an object in the pool does not guarantee it won’t be collected - causing a future allocation.
On Mar 25, 2020, at 10:22 PM, steve tang <tsg...@gmail.com> wrote:
chunkCrc32Hash := crc32.NewIEEE()chunkBuf := bufferpool.GetInstance().Get()//回收bufferdefer bufferpool.GetInstance().Put(chunkBuf)writer := io.MultiWriter(chunkBuf, chunkCrc32Hash)_, copyErr := io.Copy(writer, chunkResp.RawResponse.Body)if copyErr != nil && copyErr != io.EOF {logmgr.ErrorLogger(ctx, "fail to write chunk data to crc32 or buffer, req: %s, err: %s", originReq.PrettyPrint(), copyErr)return false, exception.OEFException(nil, http.StatusInternalServerError, CopyChunkError, "internal server exception")}In above codes, it writes chunk data to buffer pool, and the chunk data size is 4MB. When I used golang tool pprof to export memory pprof, there about 8MB space has been allocated. Could somebody help me why golang allocated memory space increases?<20200326-111844.png>
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.
sync.Pool MUST use pointers (*[]byte)
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/DB9E8E73-10B0-451B-BE66-13AF2930BB74%40kastelo.net.