go-generics-cache
go-generics-cache copied to clipboard
Addressing strange memory leak
https://github.com/Code-Hex/go-generics-cache/blob/2601bb0d3c67fc00cdbd5763cc2361d2a363f0ee/cache.go#L277-L279
After some pprof job, I believe this line make memory leaks as this would not checking lru cache capacity before doing heap push.
@gonejack Thanks! Could you give me a minimal reproduction code to cause it?
So I can fix this problem🙏
func TestMemory(t *testing.T) {
c := New[string, int](AsLRU[string, int](lru.WithCapacity(100)))
for i := 0; i < 1e7; i++ {
c.Set(fmt.Sprint(i), i, WithExpiration(time.Second))
}
time.Sleep(time.Second * 2)
fmt.Printf("len(c.expManager.queue)=%d\n", len(c.expManager.queue))
fmt.Printf("cap(c.expManager.queue)=%d\n", cap(c.expManager.queue))
fmt.Printf("len(c.expManager.mapping)=%d\n", len(c.expManager.mapping))
}
Image we are doing something like c.Set(visitorId, 1, WithExpiration(time.Minute)) in a web service, after hours c.expManager.queue and c.expManager.mapping will end up memory leak.