I have the following HTML that can reproduce the issue I'm seeing. As I add/remove IFrames over time the browser is never releasing the IFrame memory. As a result the memory grows and grows overtime which results in degraded performance. I have seen this impact Safari/IE11/Edge quite a bit while Firefox/Chrome handle it better. Does anyone have any tips on how to circumvent the IFrame memory leak?
<html>
<body>
<button onclick="add()">ADD</button>
<button onclick="remove()">REMOVE</button>
<script>
function add() {
var ifrm = document.createElement('iframe');
ifrm.setAttribute('id', 'ifrm');
ifrm.setAttribute('src', 'http://somewebsite.com');
document.body.appendChild(ifrm);
}
function remove() {
document.body.removeChild(document.getElementById('ifrm'));
}
</script>
</body>
</html>
Detached DOM elements are the elements which have been removed from the DOM but their memory is still retained because of JavaScript. This means that as long the element have a reference to any variable or an object anywhere, it does not garbage collected even after destroyed from the DOM.
Tony Rowan on Jul 26, 2022. A memory leak is an unintentional, uncontrolled, and unending increase in memory usage. No matter how small, eventually, a leak will cause your process to run out of memory and crash.
What are memory leaks? In simple words, a memory leak is an allocated piece of memory that the JavaScript engine is unable to reclaim. The JavaScript engine allocates memory when you create objects and variables in your application, and it is smart enough to clear out the memory when you no longer need the objects.
Try these operations:
Operation 2 and 3 are available only if the iframe's page is developed by yourself.
If you did 1 and 2, then the chrome will release the js heap immediately which is allocated by the iframe page.
If you did 1, 2 and 3 then the firefox will release the js heap immediately too.
But, even if you did all the operations above, the Safari won't release the js heap in a period.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With