From e8570bfd09ad7d1d2b2e76a0f1a53fcaa7494b7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Mon, 9 Oct 2023 18:08:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=86=85=E5=AD=98=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E7=A2=8E=E7=89=87GC=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/caches/memory_fragment_pool.go | 29 +++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/internal/caches/memory_fragment_pool.go b/internal/caches/memory_fragment_pool.go index b556e8e..a9928a2 100644 --- a/internal/caches/memory_fragment_pool.go +++ b/internal/caches/memory_fragment_pool.go @@ -30,9 +30,14 @@ func init() { } goman.New(func() { - var ticker = time.NewTicker(100 * time.Millisecond) + var ticker = time.NewTicker(200 * time.Millisecond) for range ticker.C { - SharedFragmentMemoryPool.GCNextBucket() + for i := 0; i < 10; i++ { // skip N empty buckets + var isEmpty = SharedFragmentMemoryPool.GCNextBucket() + if !isEmpty { + break + } + } } }) } @@ -238,7 +243,7 @@ func (this *MemoryFragmentPool) GC() { } // GCNextBucket gc one bucket -func (this *MemoryFragmentPool) GCNextBucket() { +func (this *MemoryFragmentPool) GCNextBucket() (isEmpty bool) { if !this.isOk { return } @@ -250,6 +255,20 @@ func (this *MemoryFragmentPool) GCNextBucket() { var bucketIndex = this.gcBucketIndex var bucketMap = this.bucketMaps[bucketIndex] + isEmpty = len(bucketMap) == 0 + if isEmpty { + this.mu.RUnlock() + + // move to next bucket index + bucketIndex++ + if bucketIndex >= this.countBuckets { + bucketIndex = 0 + } + this.gcBucketIndex = bucketIndex + + return + } + for itemId, item := range bucketMap { if item.IsExpired() { itemIds = append(itemIds, itemId) @@ -278,10 +297,12 @@ func (this *MemoryFragmentPool) GCNextBucket() { // move to next bucket index bucketIndex++ - if bucketIndex >= len(this.bucketMaps) { + if bucketIndex >= this.countBuckets { bucketIndex = 0 } this.gcBucketIndex = bucketIndex + + return } func (this *MemoryFragmentPool) SetCapacity(capacity int64) {