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
Iframes Memory Leak with jQuery and Sizzle #5281
Comments
Thanks for the report. The unload handler is needed by IE & Edge Legacy; in jQuery 4 that drops support for the latter, we'll explicitly only set it in IE, avoiding the memory leak outside of IE. To avoid the leak, IE or not, we'd probably have to add an additional |
We could also consider limiting the issues to IE & Edge Legacy even in the 3.x line by doing an indirect support test - by checking of presence of Neither IE nor Edge Legacy are developed anymore and I cannot imagine modern browsers now suddenly adding this non-standard API so such a check should be safe. A real fix for all browsers would probably require defining something like: function setupUnloadHandler( subWindow ) {
function unloadHandler() {
window.removeEventListener( "unload", unloadHandler );
subWindow.removeEventListener( "unload", unloadHandler );
setDocument();
}
window.addEventListener( "unload", unloadHandler );
subWindow.addEventListener( "unload", unloadHandler );
} and then, in the place where we currently attach setupUnloadHandler( subWindow ); That would be +24 bytes, though. If we went for this, I'd also want to first make sure this still fixes the IE issues and I have troubles actually reproducing it in IE 11 with the Help with finding a good test case is welcome! |
Thanks for responding and acknowledging the problem. Let me know when you have next "Plan of Actions" to fix this issue after your internal meeting and review. |
Just for reference - @vamsikrishnach90 would you be satisfied if we fix the issue outside of IE & Edge Legacy or do you care about those legacy browsers as well? I’m asking because every workaround incurs cost: additional size, testing & maintenance burden. This issue existed in the code base for ages and AFAIK this is the first report we have about it. That suggests it may be an edge case and caring about it in IE might be an even bigger edge case. |
I am fine with the fix being available to all other browsers except IE & Edge Legacy. @mgol Which version of JQuery do you think is going to receive a fix for this? |
We still need to discuss this but I'd expect changes to arrive in jQuery 3.7.1. |
Both IE & Edge Legacy need the workaround of calling `setDocument()` in an `unload` handler to avoid "permission denied" errors. However, due to not being possible to feature-detect this issue, the handler has been applied in all browsers for windows different than the one in which jQuery was loaded. jQuery 4.0, which drops Edge Legacy support, guards this workaround with a `document.documentMode` check. This won't work in the 3.x line due to still supporting Edge Legacy but we can check for `document.documentElement.msMatchesSelector` instead as that API is supported in IE 9+ and all Edge Legacy versions. Fixes jquerygh-5281 Ref jquerygh-4792
PR: #5282 |
My workaround from #5281 (comment) would require more changes as |
Both IE & Edge Legacy need the workaround of calling `setDocument()` in an `unload` handler to avoid "permission denied" errors. However, due to not being possible to feature-detect this issue, the handler has been applied in all browsers for windows different than the one in which jQuery was loaded. jQuery 4.0, which drops Edge Legacy support, guards this workaround with a `document.documentMode` check. This won't work in the 3.x line due to still supporting Edge Legacy but we can check for `document.documentElement.msMatchesSelector` instead as that API is supported in IE 9+ and all Edge Legacy versions. Fixes gh-5281 Closes gh-5282 Ref gh-4792
Fixes for non-IE non-Edge Legacy browsers in #5282 - and that's all we plan, closing. |
Description
We have a situation where there are 3 levels of nested iframes. The 3rd iframe initializes few heavy size objects, but this iframe is discarded and rebuilt in the DOM whenever user navigates across the application.
A memory leak is noticed where heap size of the application keeps on increasing whenever a new iframe at 3rd level is discarded and constructed.
Link to test case
Goto https://vamsikrishnach90.github.io/ in Chrome. Open developer tools (Hit F12) and navigate to Memory Tab. Pay attention to the JS heap size which is arppox 5 MB.
Repeat steps 1 - 3 for about 2 more times. After step 2, note down the heap size. You'll notice that the heap keeps on increasing every time we add a child iframe and bombard it. Even though at step 3, we discard the frame, the heap is not claimed by the garbage collector.
Usecase demo help (Picture)
Observations
After repeating above 1-3 steps for 3x times, take a heap snapshot. Notice that the 3 instances of custom class 'Vamsi' that were created every time we added the child page and bombarded it still exist in the heap, even though the corresponding iframes are deleted from the DOM.
Notice in the object graph, that an unloadHandler() has been bound to the Window object belonging to the discarded iframes. Heap Memory Snapshot
Root Cause
The child page has a jquery lookup statement in the parent page:
$('div.msg-banner', window.parent.document).show();
This one line statement is causing the issue for memory leak.
The above part of the statement, calls JQuery find api. This find API is overridden by Sizzle, a pure-JavaScript CSS selector engine adopted by JQuery. This find api from JQuery calls Sizzle's setDocument function See Picture which registers an unloadHandler() with the requested document's(window.parent.document) window object. This is leading in the parent page's unload event bound to unloadHandler() callback function sitting in the child page. Hence, even though the child page iframe is removed from the DOM, the window object is never eligible to be garbage collected and is the reason for memory leak.
Link to Stackoverflow thread: https://stackoverflow.com/questions/76579826/iframes-memory-leak-with-jquery-and-sizzle
The text was updated successfully, but these errors were encountered: