Short version: @CodingIntrigue's suggestion fixed the issue I was having in Internet Explorer 11
If you encounter a memory leak problem, you can incorporate this code snippet into your HTML logic:
<script type="text/javascript">
while(true) {} //stop messing around and crash this window to trigger IE11's default behavior of opening a new one
</script>
Note: My browser window was already struggling with memory issues at this point, making it easy to replicate the error consistently. However, on less complex pages, IE11 may not crash as quickly or reliably.
I was facing a memory leak in IE11 with my JavaScript React web app. To resolve it, I attempted to use window.open
for a fresh memory start and planned to close the current window using window.close
due to IE11-related memory leaks, following the guidelines mentioned here.
The challenge arose when I couldn't execute window.open
within the memory full error callback. The memory constraints prevented it. However, by forcing IE11 to follow its default error flow of reopening the tab, I achieved a similar result. The upcoming code illustrates how I implemented the while
loop strategy in this scenario, incorporating unhandled promises handling as well.
window.addEventListener('error', function(e) { //respond to errors
var errorMessage = e && e.message ? e.message.toString() : '',
errorType = e && e.type ? e.type.toString() : '',
targetNodeName = e && e.target && e.target.nodeName ?
e.target.nodeName.toString().toUpperCase() : '';
//Additional logic added here based on `errorMessage` and `errorType`
while(true) {} //stop messing around and crash this window to open a new one via IE11 default behavior
})
window.addEventListener('unhandledrejection', function(e) {
//Note - This callback has not been invoked yet
var errorMessage = e && e.message ? e.message : '';
console.log('in unhandledrejection callback listener: ' + errorMessage, e);
}, true); //`, true` sets `useCapture` Boolean
The following snippet demonstrates how you could attempt to fill up the memory (although personally, I didn't manage to make it work due to existing memory limitations). Reference taken from @Jaroslav Tulach's answer. You may need to adjust it further according to your requirements.
function gc(max) {
var arr = [];
for (var i = 0; i < max; i++) {
arr.push(i);
}
return arr.length;
}
//Attempt to prompt IE11 garbage collector to run
for (var i = 0; i < 10; i++) { // repeat until sufficient:
gc(Math.pow(2, i));
}