go-pkgz / lcw

Loading Cache Wrapper
https://go-pkgz.umputun.dev/lcw/
MIT License
20 stars 5 forks source link

Improve v1 expirable cache memory management by recreating map on purge #46

Closed paskal closed 1 month ago

paskal commented 1 month ago

Modified the v1 LoadingCache.Purge and LoadingCache.purge methods to recreate the data map, allowing the old map to be garbage collected, and added a new test to check for it.

Props to @foobarbazmeow for highlighting the issue and writing a test for it. Resolves #45.

ChatGPT description of the issue and of the fix:

The issue you’re encountering is due to the fact that Go’s garbage collector (GC) doesn’t immediately release the memory back to the system after objects are no longer in use. Even though you’ve deleted entries from LoadingCache.data, the underlying memory allocated for the map may not be released right away, leading to the observed memory usage.

When items are deleted from a map, the map’s capacity doesn’t shrink. Instead, the map may keep its allocated memory to avoid frequent allocations and deallocations. This behavior can lead to what looks like a memory leak, but it is actually just the map holding on to memory for potential future use.

To mitigate this, you can create a new map and replace the old one when you purge the cache. This will allow the old map to be garbage collected, freeing up memory.