go-generics-cache icon indicating copy to clipboard operation
go-generics-cache copied to clipboard

Addressing strange memory leak

Open gonejack opened this issue 1 year ago • 2 comments

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 avatar Jun 19 '24 18:06 gonejack

@gonejack Thanks! Could you give me a minimal reproduction code to cause it?

So I can fix this problem🙏

Code-Hex avatar Jul 21 '24 02:07 Code-Hex

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.

gonejack avatar Jul 21 '24 03:07 gonejack