(fix): Memory leak in hyper-link extension #687
Merged
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There's a memory leak in the
hyper-linkextension which will reliably crash a Chrome tab. To reproduce, go to to the documentation page in Chrome and paste inhttps://www.npmjs.com/package/@uiw/react-codemirror3000-4000 times. You can paste them all at once or a couple hundred at a time.Here's a recording of the crash:
codemirror-hyperlink-crash.mp4
The issue here is that when
doc.toString()is in the while loop, a new string is created of the entire document, and Chrome creates a sliced string when runningdefaultRegexp.exec. That sliced string has aparentreference back to the string created bydoc.toString(). That sliced string, with the giantparentreference, gets set as theurlstate on theHyperLinkIconwidget. The result is that Chrome has in-memory a copy of the entire document for each of the links in the document, and runs out of memory as the document gets large.Here's what it looks like in a Chrome memory snapshot:
Doing the
toString()once outside of the loop creates a single sliced string, and each widget will have a reference to that single string.I was able to verify locally that this change resolves the memory leak. I was able to paste 100,000 links in my app that uses the hyperlink extension without issue.