Skip to content
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

Browser bug: repeatedly changing image src causes a memory leak #2

Open
fozcode opened this issue May 9, 2017 · 1 comment
Open

Comments

@fozcode
Copy link
Member

fozcode commented May 9, 2017

Amazingly, all the major browsers seem to suffer from a bug where repeatedly changing the src of an image, even just toggling it between 2 values, causes memory use to grow without bounds:

This bug renders image-defer's maxLoaded feature useless, and in fact image-defer just makes it worse when maxLoaded is in force, because it causes the src attributes to change more often than it otherwise would.

The issue is reproducible with the image-defer live demo page in latest Firefox 53 and Chrome 58. Just set the limit to 10 then repeatedly scroll up and down.

Given that the browser vendors haven't fixed this in 5 years, some kind of workaround will be needed. It might be possible, instead of just changing the src, to replace the image's DOM element with a clone of itself already set to the new src. The browser should then garbage collect the old DOM element along with the image data it contained. The DOM element in image-defer's _state.images will also need to be replaced.

@fozcode
Copy link
Member Author

fozcode commented May 11, 2017

Changing the src by replacing the img with a clone looks to require something like this:

var clone = img.cloneNode(true);
clone.src = newsrc;
clone.addEventListener('load', clone._defer_load_event);  // if _defer_load_event exists
img.parentNode.replaceChild(clone, img);
// then replace img with clone in _state.images
// img should be garbage collected if there are no other references to it

Refs:
MDN clone
MSDN clone

Looks like it won't affect the existing browser compatibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant