Ballooning memory use and then crash in new Popup().setHTML()
- Dominant language
- TypeScript
- Stars
- 12.4k
- Forks
- 2.4k
- PR merge metrics
- No merged PRs in 30d
Description
**mapbox-gl-js version**: v2.13.0
**browser**: Webkit on Linux and macOS (Safari)
### Steps to Trigger Behavior
1. Create a new popup and call `setHTML()` with more than a few HTML tags (but you don't need a million -- maybe 100 will do). Repeat this for a few markers/popups.
2. The map will freeze, run out of memory, and crash.
By watching memory use while stepping through with the debugger, I can see that the `setHTML()` function is inefficient. A single trivial-sized HTML string uses about 3 GB of memory in this loop:
https://github.com/mapbox/mapbox-gl-js/blob/03257b5dbe35022c3af8cd369878c09484b22ada/src/ui/popup.js#L388-L392
It could also be related to setDOMContent, I dunno -- but setText() calls this too, and setText() doesn't crash with the same inputs. I suspect it's something to do with HTML specifically.
Is there any reason we can't just set `innerHTML` instead?
### Link to Demonstration
https://jsfiddle.net/vnswtykh/1/
### Expected Behavior
No crash
### Actual Behavior
Within about a second, the entire process freezes, and it runs up memory usage until it crashes with out of memory:
```
Unable to shrink memory footprint of process (22498 MB) below the kill thresold (16384 MB). Killed
```
The WebKit process is what runs up the memory bill. It may be a bug in WebKit rather than Mapbox-GL-JS.
Contributor guide
Research direction
Start with src/ui/popup.js at the linked lines and reproduce the memory growth using the provided JSFiddle in WebKit on Safari. Compare the behavior of setHTML() with setDOMContent() and setText(), while checking whether the issue is specific to HTML handling or WebKit. Done means repeated popup creation no longer freezes or exhausts process memory.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- html, javascript
- Domain
- frontend, web-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100