Skip to content

Bump version to 0.10.32 and fix memory leaks#161

Open
Axis-qc wants to merge 1 commit into
cwtools:mainfrom
Axis-qc:fix/memory-leak
Open

Bump version to 0.10.32 and fix memory leaks#161
Axis-qc wants to merge 1 commit into
cwtools:mainfrom
Axis-qc:fix/memory-leak

Conversation

@Axis-qc

@Axis-qc Axis-qc commented Jun 11, 2026

Copy link
Copy Markdown

Problem

The CWTools language server's memory grows continuously up to 12GB+ during long-running sessions. Root cause: addDataEventTargetLinks() creates ScopedEffect entries with EffectType.ValueTrigger that leak into lookup.triggers. Each RefreshCaches cycle reads these back from lookup.triggers and appends them again, adding ~430k duplicate trigger entries per cycle — growing from ~200k to over 3M within hours.

Changes

1. Trigger accumulation fix (core)

  • STLGame: Filter out ScopedEffect instances from lookup.triggers in refreshConfigAfterFirstTypesHook — these are data event target links, not real triggers. This breaks the cross-cycle accumulation feedback loop.
  • STLGame / HOI4 / EU4 / CK2: refreshConfigAfterVarDefHook now uses lookup.eventTargetLinks instead of updateEventTargetLinks + dataEventTargetLinksCache, eliminating intra-cycle ValueTrigger duplication.
  • Lookup: Added dataEventTargetLinksCache field to avoid redundant computation of addDataEventTargetLinks.

2. Cache cleanup API

  • Added ClearResources() / ClearValidationCache() to IGame interface, implemented across all 10 game types.
  • ResourceManager.Clear() — clears fileMap and entitiesMap.
  • ErrorCache.Clear() — clears all cached validation errors.
  • Periodic deep cleanup every 10 analysis cycles.

3. Memory monitoring

  • Status bar showing server process memory, refreshed every 60s.
  • Click for detailed breakdown (managed/unmanaged heap, GC stats, file cache, type system, trigger/effect counts, string interning, etc.).
  • Localized labels (EN/ZH) via .resx resource files, following the system culture.

4. Other

  • Periodic GC.Collect in delayedAnalyze.
  • Version bump 0.10.31 → 0.10.32.

⚠️ Caveats & Risks

This PR has NOT been thoroughly tested. Please be aware of the following before merging:

  1. Only Stellaris has been verified. HOI4/EU4/CK2 have the same fix pattern applied (VarDefHook uses eventTargetLinks), but have NOT been actually tested to confirm trigger count stops growing.

  2. ScopedEffect filtering is brittle. The filter in STLGame assumes legitimate triggers are DocEffect or ScriptedEffect instances, and only data link entries are ScopedEffect. If any game defines real triggers as ScopedEffect, the filter will incorrectly remove them.

  3. Jomini-based games are NOT fixed. CK3 / VIC3 / EU5 / IR / VIC2 share Hooks.refreshConfigAfterHook, which calls addDataEventTargetLinks in both first-types and var-def hooks. This likely has the same accumulation issue — not yet investigated.

  4. Cache clearing may cause CPU spikes. ClearResources() drops all parsed ASTs; the next analysis cycle will re-parse everything. Deep cleanup runs every 10 cycles and may cause brief CPU spikes on large projects.

  5. GC.Collect overhead. Periodic GC.Collect(2, GCCollectionMode.Optimized) may increase GC pause times on large projects.

Merge Order

This PR depends on a companion PR in the cwtools submodule:

  1. First: Axis-qc/cwtools fix/memory-leakcwtools/cwtools master (15 files, +70/−13 lines)
  2. Then: This PR → cwtools/cwtools-vscode main

The main repo's submodules/cwtools pointer references the new submodule commit. Merging in the wrong order will break the submodule reference.

- Add periodic cache cleanup and GC management to prevent sustained memory growth
- Fix scripted trigger accumulation: filter ScopedEffect data links from lookup.triggers in STLGame, use eventTargetLinks in VarDefHook across STL/HOI4/EU4/CK2
- Add dataEventTargetLinksCache to avoid redundant computation
- Add IGame.ClearResources()/ClearValidationCache() interface with implementations across all 10 game types
- Add getMemoryDetails command and memory monitor status bar
- Add i18n support via LangResources for memory monitor labels
- Update cwtools submodule to include all fixes
- Version bump 0.10.31 → 0.10.32

Caveats:
- Only tested on Stellaris; HOI4/EU4/CK2 have same fix unconfirmed
- Jomini-based games (CK3/VIC3/EU5/IR/VIC2) use shared hooks that also call addDataEventTargetLinks twice per cycle — not yet investigated
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.

1 participant