Skip to content

fix: memory leak in menu hidden defaults - #348413

Merged
Dmitriy Vasyura (dmitrivMS) merged 3 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-menu-hidden-defaults
Nov 17, 2026
Merged

Dmitriy Vasyura (dmitrivMS) merged 3 commits into
microsoft:mainfrom
SimonSiefke:fix/memory-leak-menu-hidden-defaults

Conversation

@SimonSiefke

Copy link
Copy Markdown
Contributor

Details

The menu service caches false defaults under combined menu and command IDs. The cache outlives dynamic command registrations and keeps their key strings after the commands are disposed.

Change

Store only true hidden defaults and delete false entries, since a missing entry already means false.

Before

When registering commands, opening the Command Palette, and disposing the commands 17 times, the cache holds 1,052 redundant false entries. These include 17 disposed-command keys occupying 1,020 bytes. Total string growth is 200,784 bytes and 5,056 strings.

Before

After

The cache has zero visible-default entries, including zero disposed-command keys. Total string growth is 155,664 bytes and 4,380 strings; other UI allocations remain.

After

Test Video

menu-hidden-defaults-native.webm

AI disclosure: Model: GPT 6.1 Sol. Worktime: 15 min

Copilot AI balanced review requested due to automatic review settings November 15, 2026 17:47

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.

🟢 Approval recommended

No active review issues remain for the menu-service change to reduce retained cache entries.

No open issues 🎉

What changed in this PR

This PR reduces memory retention in the menu service by removing redundant visible-default cache entries, so disposed dynamic commands no longer leave those keys behind.

Changes:

  • Cache only hidden (true) defaults and delete entries for visible (false) defaults.
  • Add regression tests for default changes, user overrides, disposed registrations, and replacement registrations.

No actionable findings remain. Tests were not run during this review.

File Description
src/​vs/​platform/​actions/​test/​common/​menuService.test.ts Adds coverage for cache retention and hidden-state behavior.
src/​vs/​platform/​actions/​common/​menuService.ts Deletes false-default entries instead of retaining them.

Review effort: Balanced

@sbatten SteVen Batten (sbatten) left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤖Generated by Copilot

Thanks Simon Siefke (@SimonSiefke) — this looks good to me.

Correctness. setDefaultState has a single caller (createActionGroups), and every reader of _hiddenByDefaultCache goes through _isHiddenByDefault, which already returns get(key) ?? false. So deleting the key instead of storing false doesn't change isHidden, updateHidden or isModifiedFromDefault. It also behaves the same when one command is registered twice in the same menu with different defaults, because the last write wins either way. The change just stops the map from growing with a false entry for every visible menu item, which includes dynamic commands that are later disposed.

Validation (local, at 7ab102f):

  • node test/unit/node/index.js --run src/vs/platform/actions/test/common/menuService.test.ts (Node unit runner) → 27 passing.
  • With only menuService.ts reverted to the merge base (and the compiled output checked): "does not retain visible defaults from disposed registrations" and "removes a cached true default when a registration is replaced" fail. Their diffs show the retained false entries. All other tests, including "updates hidden defaults while preserving user overrides", still pass. Restoring the change brings it back to 27 passing.
  • pnpm run oxlint on both changed files → clean. pnpm run typecheck-client → clean.

Non-blocking notes:

  1. A true entry for a hidden-by-default registration that is later disposed is still kept. The number of such entries is limited by how many distinct menu/command pairs are declared isHiddenByDefault, and this was already the case before. Not something this PR needs to solve. If it ever matters, the follow-up should evict only keys whose registration was actually removed, for example by tracking disposal of MenuRegistry.appendMenuItem registrations. Clearing on onDidChangeMenu wouldn't work: that event reports only menu IDs, and clearing the cache would also drop live defaults for items whose when is currently false.
  2. Two of the tests read menuService['_hiddenStates']['_hiddenByDefaultCache'] behind an eslint-disable. That's fine for a targeted retention test, but it will need updating if those private fields are renamed.

Full review report (plan, evidence, review rounds): https://aka.ms/publish?a=75a9e684-8496-4bfb-81a6-f9741f2d38be&v=bf6f0c77-6542-48dd-82dd-21864c119c7a

@dmitrivMS Dmitriy Vasyura (dmitrivMS) added freeze-slow-crash-leak VS Code crashing, performance, freeze and memory leak issues and removed triage-needed labels Nov 17, 2026
@dmitrivMS

Copy link
Copy Markdown
Collaborator

Simon Siefke (@SimonSiefke) Thank you!

@dmitrivMS
Dmitriy Vasyura (dmitrivMS) added this pull request to the merge queue Nov 17, 2026
Merged via the queue into microsoft:main with commit 1259705 Nov 17, 2026
37 of 38 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.148.0 milestone Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

freeze-slow-crash-leak VS Code crashing, performance, freeze and memory leak issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants