Avoid memory leak (#107)

This commit is contained in:
thinkgo 2020-08-29 18:51:56 +08:00 committed by GitHub
parent 21f632368a
commit ef60172172
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 0 deletions

View File

@ -81,6 +81,7 @@ func (p *Pool) purgePeriodically() {
// are located on non-local CPUs. // are located on non-local CPUs.
for i := range expiredWorkers { for i := range expiredWorkers {
expiredWorkers[i].task <- nil expiredWorkers[i].task <- nil
expiredWorkers[i] = nil
} }
// There might be a situation that all workers have been cleaned up(no any worker is running) // There might be a situation that all workers have been cleaned up(no any worker is running)

View File

@ -68,6 +68,7 @@ func (wq *loopQueue) detach() *goWorker {
} }
w := wq.items[wq.head] w := wq.items[wq.head]
wq.items[wq.head] = nil
wq.head++ wq.head++
if wq.head == wq.size { if wq.head == wq.size {
wq.head = 0 wq.head = 0
@ -90,6 +91,7 @@ func (wq *loopQueue) retrieveExpiry(duration time.Duration) []*goWorker {
break break
} }
wq.expiry = append(wq.expiry, wq.items[wq.head]) wq.expiry = append(wq.expiry, wq.items[wq.head])
wq.items[wq.head] = nil
wq.head++ wq.head++
if wq.head == wq.size { if wq.head == wq.size {
wq.head = 0 wq.head = 0

View File

@ -35,6 +35,7 @@ func (wq *workerStack) detach() *goWorker {
} }
w := wq.items[l-1] w := wq.items[l-1]
wq.items[l-1] = nil // avoid memory leaks
wq.items = wq.items[:l-1] wq.items = wq.items[:l-1]
return w return w
@ -53,6 +54,9 @@ func (wq *workerStack) retrieveExpiry(duration time.Duration) []*goWorker {
if index != -1 { if index != -1 {
wq.expiry = append(wq.expiry, wq.items[:index+1]...) wq.expiry = append(wq.expiry, wq.items[:index+1]...)
m := copy(wq.items, wq.items[index+1:]) m := copy(wq.items, wq.items[index+1:])
for i := m; i < n; i++ {
wq.items[i] = nil
}
wq.items = wq.items[:m] wq.items = wq.items[:m]
} }
return wq.expiry return wq.expiry
@ -74,6 +78,7 @@ func (wq *workerStack) binarySearch(l, r int, expiryTime time.Time) int {
func (wq *workerStack) reset() { func (wq *workerStack) reset() {
for i := 0; i < wq.len(); i++ { for i := 0; i < wq.len(); i++ {
wq.items[i].task <- nil wq.items[i].task <- nil
wq.items[i] = nil
} }
wq.items = wq.items[:0] wq.items = wq.items[:0]
} }