Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: memory leak when gcache.NewAdapterMemory with lru #3241

Merged
merged 1 commit into from
Jan 3, 2024

Conversation

qinyuguang
Copy link
Contributor

@qinyuguang qinyuguang commented Jan 3, 2024

Example

package main

import (
	"context"
	"runtime"
	"time"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gcache"
)

func main() {
	ctx := context.Background()

	cache := gcache.NewAdapterMemory(1)
	cache.Set(ctx, "key", "value", 10*time.Minute)

	var m runtime.MemStats
	timer1 := time.NewTicker(time.Microsecond)
	timer2 := time.NewTicker(time.Second)
	var i int
	for {
		select {
		case <-timer1.C:
			i++
			cache.Get(ctx, "key")
		case <-timer2.C:
			runtime.ReadMemStats(&m)
			size, _ := cache.Size(ctx)
			g.Log().Infof(ctx, "memory usage: %dKB, gc num: %d, gcache size: %d, call times: %d", m.HeapInuse/1024, m.NumGC, size, i)
		}
	}
}                                                                                                                                               

Output

image

@gqcn gqcn merged commit 5a01798 into gogf:master Jan 3, 2024
20 checks passed
@qinyuguang qinyuguang deleted the fix-gcache-leak branch January 3, 2024 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants