I’ve been investigating memory leaks on my application and have realized that Codemirror leaks. Tested on v5, then moved to v6.0.1, and still the leaks occur.
I’ve created the most basic test to check the leak:
To replicate:
Open the stackblitz, wait for the application to show.
Click Open Preview in New Tab, then connect.
Open Chrome DevTools on Memory tab.
Select Heap Snapshot and select the profile that ends with .webcontainer.io: Main.
Click on Destroy in the web page.
Click Take Snapshot from Memory.
Instead of All objects, look at “Objects retained by detached DOM nodes”.
As seen all of those objects have leaked, even though the corresponding editor.destroy() function was used. It does not seem that the EditorView can be destroyed completely in v5 or v6.
That script looks like it is still holding on to the editor variable when you’re doing this measurement. That’s obviously going to hold on to the structures linked from that object.
I can reproduce this. Seems Chrome’s new EditContext feature leaks cyclic event handlers. I’ve reported Chrome issue 353650038, and added a CodeMirror patch to work around the issue.
I can confirm that by overriding @codemirror/views to 6.28.5 version which you have just published with the fix above, there a 0 detached views left Thank you!
Any chance you could port this into Codemirror v5 latest as well?
I’m not sure what you mean by you don’t see evidence.
In the stackblitz provided above, before CodeMirror instance is created, there are 0 detached nodes. Once it is created, there are 100 detached nodes. Even after it is destroyed, there are 100 detaches nodes.
The Detached HTMLPreElement is actually a pre.CodeMirror-line-like element, and it is retained by CodeMirror.
Right, there’s a single <pre> element with some <br> children, being preserved by a cache. Creating more editors will not cause more such elements to be allocated. I don’t think that’s going to take down your website though. This is a wontfix.