Skip to content

fix: prevent memory leak by pruning lifecycleEntries and tierOverrides maps#726

Open
zhangshu9206 wants to merge 1 commit intoCortexReach:masterfrom
zhangshu9206:fix/memory-leak-prune-lifecycle-tier
Open

fix: prevent memory leak by pruning lifecycleEntries and tierOverrides maps#726
zhangshu9206 wants to merge 1 commit intoCortexReach:masterfrom
zhangshu9206:fix/memory-leak-prune-lifecycle-tier

Conversation

@zhangshu9206
Copy link
Copy Markdown

Summary

This PR fixes a memory leak in the Gateway caused by unbounded growth of lifecycleEntries and tierOverrides Map objects in index.ts.

Problem

The lifecycleEntries and tierOverrides Maps were being populated on every lifecycle event and tier transition but never cleaned up, leading to continuous memory growth (approx. 150 MB/hour).

Fix

  • Added LIFECYCLE_ENTRIES_MAX_SIZE (500) and TIER_OVERRIDES_MAX_SIZE (200) constants.
  • Called pruneMapIfOver() after populating both Maps to prevent unbounded growth.
  • Added DEFAULT_REFLECTION_AGENT_CACHE_MAX_SIZE (50) constant for consistency.

Impact

  • Memory stability: GC troughs are now stable (~1335-1440 MB) instead of continuously rising.
  • Performance: Negligible impact; pruning only runs when Maps exceed the safe limits.

Verification

  • Observed GC trough stability over 4+ hours post-fix.
  • RSS growth rate reduced from ~150 MB/h to ~0 MB/h (stable).

…s maps

- Add LIFECYCLE_ENTRIES_MAX_SIZE (500) and TIER_OVERRIDES_MAX_SIZE (200) constants.
- Call pruneMapIfOver() after populating lifecycleEntries and tierOverrides to prevent unbounded memory growth.
- Add DEFAULT_REFLECTION_AGENT_CACHE_MAX_SIZE (50) constant.
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

Successfully merging this pull request may close these issues.

2 participants