Skip to content

fix: add maxSize and periodic eviction to TTLCache to prevent memory leak - #8361

Open
ionfwsrijan wants to merge 2 commits into
JhaSourav07:mainfrom
ionfwsrijan:fix/8269-ttlcache-memory-leak
Open

fix: add maxSize and periodic eviction to TTLCache to prevent memory leak#8361
ionfwsrijan wants to merge 2 commits into
JhaSourav07:mainfrom
ionfwsrijan:fix/8269-ttlcache-memory-leak

Conversation

@ionfwsrijan

Copy link
Copy Markdown
Contributor

Bug: Unbounded TTLCache Causes Memory Leak in Long-Running Processes

Problem

The TTLCache class in lib/cache.ts stores entries in a plain Map with no maximum size limit and no proactive cleanup of expired entries. Expired entries are only removed on get() (lazy eviction). In long-running Node.js processes, expired entries accumulate indefinitely, causing unbounded memory growth and potential OOM crashes.

Changes

lib/cache.ts

  • Added maxSize constructor parameter (default 500) to cap total entries per cache instance
  • Added periodic evictExpired() sweep via setInterval with .unref() so it doesn't keep Node.js alive
  • Added FIFO eviction on set() when cache is at capacity (after expired cleanup)
  • Expired entries are now proactively reclaimed even for keys that are never accessed again

lib/github.ts

  • Updated contributionsCache, profileCache, and reposCache instances to use maxSize=500

Behavior

  • Each cache is now capped at 500 entries (configurable via constructor)
  • Expired entries are swept every 60 seconds automatically
  • When the cache is full and a new key is inserted, the oldest entry is evicted (FIFO)
  • The cleanup timer uses .unref() so it doesn't prevent graceful process shutdown

Closes

#8269

…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
Copilot AI review requested due to automatic review settings July 26, 2026 17:01
@retenta-bot

retenta-bot Bot commented Jul 26, 2026

Copy link
Copy Markdown

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!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@vercel

vercel Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

@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.

@ionfwsrijan

Copy link
Copy Markdown
Contributor Author

@Aamod007 Please review this

@github-actions

Copy link
Copy Markdown
Contributor

📦 Next.js Bundle Size Report (Gzipped Sizes)

✨ No significant bundle size changes detected.

📊 Summary of Totals

Category PR Size Base Size Difference
Total JS 4052.50 KB 4052.50 KB 0 B
Total CSS 338.94 KB 338.94 KB 0 B

@Aamod007 Aamod007 added level:beginner Small changes Usually isolated fixes or simple UI/text updates. quality:clean PR follows clean coding practices, proper formatting, documentation, and maintainability standards. mentor:Aamod007 type:bug Something isn't working as expected labels Jul 27, 2026

@Aamod007 Aamod007 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@ionfwsrijan

Copy link
Copy Markdown
Contributor Author

@Aamod007 No CI check is failing wrong observation

@Aamod007 Aamod007 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

level:beginner Small changes Usually isolated fixes or simple UI/text updates. mentor:Aamod007 quality:clean PR follows clean coding practices, proper formatting, documentation, and maintainability standards. type:bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants