document.getElementById('button').addEventListener('click', function() {
const largeData = new Array(1000000).fill('data');
// Use largeData...
});
This code may cause a memory leak because the event listener is never removed: 1) Each time the button is clicked, a new, large array is created, 2) The event listener function forms a closure that maintains access to this array, 3) Even after the array is no longer needed, it remains in memory due to the closure, 4) If the button remains in the DOM, this listener will continue to exist, 5) Without explicitly removing the event listener, these large arrays accumulate with each click, 6) This pattern is particularly problematic because the array is very large (1,000,000 elements), 7) The proper solution would be to remove the event listener when it's no longer needed or restructure the code to avoid capturing large data, 8) This is a common pattern of memory leaks in web applications, especially single-page applications.