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

gcache Memory leakage #3467

Open
zhiqyang opened this issue Apr 9, 2024 · 1 comment
Open

gcache Memory leakage #3467

zhiqyang opened this issue Apr 9, 2024 · 1 comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@zhiqyang
Copy link

zhiqyang commented Apr 9, 2024

What version of Go and system type/arch are you using?

1.20 windows/intel x86
What version of GoFrame are you using?

v2.7.0

Can this bug be re-produced with the latest release?
YES

What did you do?

func main() {
glog.SetFlags(44)

ctx := context.Background()
cache := gcache.New()

for {
	key := guid.S()
	val := grand.B(10 * 1024 * 1024)
	err := cache.Set(ctx, key, val, 30*time.Minute)
	if err != nil {
		glog.Fatal(ctx, err)
	}

	_, err = cache.Remove(ctx, key)
	if err != nil {
		glog.Error(ctx, "delete failed", err)
	}

	time.Sleep(time.Millisecond * 10)
}

}

What did you expect to see?

What did you see instead?
问题: 持续运行上述代码,内存会不断增加。

检查源码 发现当调用 func (c *AdapterMemory) Remove(ctx context.Context, keys ...interface{})接口时,会将过期时间设置为 gtime.TimestampMilli()-1000000,,并放入事件列表eventList中
在syncEventAndClearExpired 接口,只会对当前时间的前5秒的的数据进行清空
eks = []int64{ek - 1000, ek - 2000, ek - 3000, ek - 4000, ek - 5000}

因此,对于Remove掉的缓存,在expireSets里面会一直保留一份key值,导致内存会缓慢增加

@zhiqyang zhiqyang added the bug It is confirmed a bug, but don't worry, we'll handle it. label Apr 9, 2024
@zhiqyang
Copy link
Author

zhiqyang commented Apr 9, 2024

Showing top 10 nodes out of 11
flat flat% sum% cum cum%
1078.34kB 41.13% 41.13% 1078.34kB 41.13% github.com/gogf/gf/v2/os/gcache.(*adapterMemoryExpireTimes).Set
1027.94kB 39.21% 80.35% 1027.94kB 39.21% github.com/gogf/gf/v2/container/gset.(*Set).Add
515.19kB 19.65% 100% 515.19kB 19.65% unicode/utf16.Encode
0 0% 100% 515.19kB 19.65% fmt.Fprintln
0 0% 100% 515.19kB 19.65% fmt.Println (inline)
0 0% 100% 2621.47kB 100% github.com/gogf/gf/v2/os/gcache.(*AdapterMemory).syncEventAndClearExpired
0 0% 100% 2621.47kB 100% github.com/gogf/gf/v2/os/gtimer.(*Entry).Run.func1
0 0% 100% 515.19kB 19.65% internal/poll.(*FD).Write
0 0% 100% 515.19kB 19.65% internal/poll.(*FD).writeConsole
0 0% 100% 515.19kB 19.65% os.(*File).Write

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.
Projects
None yet
Development

No branches or pull requests

1 participant