Memory leak caused by Safari bug with input element not being garbage collected #4197
loredanaspataru
started this conversation in
General
Replies: 1 comment 2 replies
-
A couple of questions:
|
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The memory leak issue is caused by a Safari bug with input elements that aren't garbage collected properly. This happens because the browser runs an extension used by Autofill code that retains references to the input elements. SWC components that are impacted by this bug are
sp-textfield
,sp-search
,sp-number-field
.For large apps that use web components extensively like ours, this results in a considerable number of detached DOM subtrees not being garbage collected because of the input elements in the tree.
A couple of my colleagues investigated this issue last year and they put together a patch for
TextfieldBase
. The solution they came up with, after multiple rounds of testing, is to wrap the input in a form element forWebKit
and manually detach the form from the tree ondisconnectedCallback
. Although this doesn't entirely solve the memory leak issue, it mitigates the impact to just the input and form wrapper elements being retained in memory, while the other nodes are garbage collected properly.I've added here the patch that we currently use for
@spectrum-web-components/textfield: 0.33.2
.Based on Kin Blas's work, I've put together a project to test and showcase how using the patched version impacts the items being retained in the DOM tree, using
sp-textfield
instead of HTML input element.The script builds a snapshot of the DOM tree and uses
WeakRefs
to track which nodes are retained after they are detached from the tree. You can see here how the results look for the patched VS unpatched version:The project used to test the impact of Textfield and patched changes can be found at https://github.com/loredanaspataru/swc-input-patch-test.
One big pain for our project is that each time we do an upgrade we have to maintain the patch and test that everything still works as expected. That's why we wanted to start a discussion here to check if introducing such changes in the actual SWC component would be a viable option.
Or if you have any other suggestions that might help in minimizing bug impact or maintenance work for this issue, that would also help us a lot. What do you think?
Beta Was this translation helpful? Give feedback.
All reactions