fix: add maxSize and periodic eviction to TTLCache to prevent memory leak - #8361
fix: add maxSize and periodic eviction to TTLCache to prevent memory leak#8361ionfwsrijan wants to merge 2 commits into
Conversation
…leak (JhaSourav07#8269) - Add maxSize constructor parameter (default 500) to cap cache entries - Add periodic sweep of expired entries via setInterval with .unref() - Add FIFO eviction on set() when cache is at capacity - Evict expired entries before inserting new ones - Update cache instances in github.ts to use maxSize=500 Fixes JhaSourav07#8269
|
This PR addresses a critical issue with the TTLCache class by implementing memory management features such as a maximum size limit and periodic cleanup of expired entries. This aligns with previous decisions made to enhance memory management in the TTLCache, ensuring that we avoid unbounded memory growth in long-running processes. Thank you for your contribution! |
|
@ionfwsrijan is attempting to deploy a commit to the jhasourav07's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
@Aamod007 Please review this |
📦 Next.js Bundle Size Report (Gzipped Sizes)✨ No significant bundle size changes detected. 📊 Summary of Totals
|
Aamod007
left a comment
There was a problem hiding this comment.
Adding periodic eviction and cleanup on clear() to TTLCache is the right fix for the memory leak. The private evictExpired method is clean and simple. CI is failing though — please investigate and push fixes.
Labels: level:beginner (1 file, targeted fix), quality:clean, type:bug, mentor:Aamod007
|
@Aamod007 No CI check is failing wrong observation |
Aamod007
left a comment
There was a problem hiding this comment.
Re-reviewing after noting the only CI failure is Vercel authorization (not code-related). All other checks (Format · Lint · Typecheck · Test, Production Build, CodeQL) pass. Approving — the Vercel deploy auth is a repo-level config issue, not a code problem.
Bug: Unbounded TTLCache Causes Memory Leak in Long-Running Processes
Problem
The
TTLCacheclass inlib/cache.tsstores entries in a plainMapwith no maximum size limit and no proactive cleanup of expired entries. Expired entries are only removed onget()(lazy eviction). In long-running Node.js processes, expired entries accumulate indefinitely, causing unbounded memory growth and potential OOM crashes.Changes
lib/cache.tsmaxSizeconstructor parameter (default 500) to cap total entries per cache instanceevictExpired()sweep viasetIntervalwith.unref()so it doesn't keep Node.js aliveset()when cache is at capacity (after expired cleanup)lib/github.tscontributionsCache,profileCache, andreposCacheinstances to usemaxSize=500Behavior
.unref()so it doesn't prevent graceful process shutdownCloses
#8269