Skip to content

fix: memory leak in codeCell #205616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

SimonSiefke
Copy link
Contributor

@SimonSiefke SimonSiefke commented Feb 19, 2024

fixes #186361

When scrolling a notebook up and down, I found the largest amount of detached dom nodes was this:

{
  "detachedDomNodesWithStackTraces": {
    "after": [
      {
        "className": "HTMLDivElement",
        "description": "div.monaco-editor.no-user-select.showUnused.showDeprecated.vs-dark",
        "stackTrace": [
          "View (vscode/out/vs/editor/browser/view.js:86:73)",
          "CodeEditorWidget._createView (vscode/out/vs/editor/browser/widget/codeEditorWidget.js:1483:26)",
          "CodeEditorWidget._attachModel (vscode/out/vs/editor/browser/widget/codeEditorWidget.js:1380:46)",
          "CodeEditorWidget.setModel (vscode/out/vs/editor/browser/widget/codeEditorWidget.js:415:18)",
          "vscode/out/vs/workbench/contrib/notebook/browser/view/cellParts/codeCell.js:184:46"
        ],
        "count": 3997
      }
    ]
  }
}

More details

// codeCell.ts
private initializeEditor(initEditorHeight: number) {
	const cts = new CancellationTokenSource();
	this._register({ dispose() { cts.dispose(true); } });
	raceCancellation(this.viewCell.resolveTextModel(), cts.token).then(model => {
		if (this._isDisposed) {
			return;
		}

In the initializeEditor after resolveTextModel.then the model disposable is not registered, which seemingly causes a memory leak when scrolling in the notebook.

Fix

Now the model is registered, similar to other disposables:

this._register(model);

For some reason, registering the model didn't fix the issue entirely, which is why there is also (copied from commentsController) this code:

this.templateData.editor = null!; 

With both changes in place, the number of detached div.monaco-editor dom nodes when scrolling a notebook up and down 1997 times was reduced from 3997 to 0.

return;
}

if (model) {
this._register(model);
Copy link
Member

Choose a reason for hiding this comment

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

This seems legit, but I'll leave it to someone else to make the call

Copy link
Member

Choose a reason for hiding this comment

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

@roblourens I'm not sure if it's a good idea to dispose the model since BaseCellViewModel has a model ref _textModelRef. Disposing the model here would leave us an obsolete _textModelRef and textModel.

@roblourens roblourens assigned rebornix and Yoyokrazy and unassigned roblourens Jul 26, 2024
@rebornix rebornix self-requested a review July 29, 2024 06:58
@rebornix rebornix added this to the August 2024 milestone Jul 29, 2024
@rebornix rebornix modified the milestones: August 2024, September 2024 Aug 26, 2024
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.

Scrolling in a notebook leaks memory
5 participants