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

Addressing strange memory leak #59

Open
gonejack opened this issue Jun 19, 2024 · 2 comments
Open

Addressing strange memory leak #59

gonejack opened this issue Jun 19, 2024 · 2 comments

Comments

@gonejack
Copy link

gonejack commented Jun 19, 2024

go-generics-cache/cache.go

Lines 277 to 279 in 2601bb0

if item.hasExpiration() {
c.expManager.update(key, item.Expiration)
}

After some pprof job, I believe this line make memory leaks as this would not checking lru cache capacity before doing heap push.

AxelPrel added a commit to orus-io/go-generics-cache that referenced this issue Jun 27, 2024
@Code-Hex
Copy link
Owner

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

So I can fix this problem🙏

@gonejack
Copy link
Author

gonejack commented Jul 21, 2024

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.

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

No branches or pull requests

2 participants