Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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
@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.

2 participants