优化内存缓存碎片GC程序

This commit is contained in:
刘祥超
2023-10-09 18:08:30 +08:00
parent 534d64673f
commit e8570bfd09

View File

@@ -30,9 +30,14 @@ func init() {
} }
goman.New(func() { goman.New(func() {
var ticker = time.NewTicker(100 * time.Millisecond) var ticker = time.NewTicker(200 * time.Millisecond)
for range ticker.C { 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 // GCNextBucket gc one bucket
func (this *MemoryFragmentPool) GCNextBucket() { func (this *MemoryFragmentPool) GCNextBucket() (isEmpty bool) {
if !this.isOk { if !this.isOk {
return return
} }
@@ -250,6 +255,20 @@ func (this *MemoryFragmentPool) GCNextBucket() {
var bucketIndex = this.gcBucketIndex var bucketIndex = this.gcBucketIndex
var bucketMap = this.bucketMaps[bucketIndex] 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 { for itemId, item := range bucketMap {
if item.IsExpired() { if item.IsExpired() {
itemIds = append(itemIds, itemId) itemIds = append(itemIds, itemId)
@@ -278,10 +297,12 @@ func (this *MemoryFragmentPool) GCNextBucket() {
// move to next bucket index // move to next bucket index
bucketIndex++ bucketIndex++
if bucketIndex >= len(this.bucketMaps) { if bucketIndex >= this.countBuckets {
bucketIndex = 0 bucketIndex = 0
} }
this.gcBucketIndex = bucketIndex this.gcBucketIndex = bucketIndex
return
} }
func (this *MemoryFragmentPool) SetCapacity(capacity int64) { func (this *MemoryFragmentPool) SetCapacity(capacity int64) {