Memory Leaks and GPU Crashes in recent model-viewer versions
Description
I noticed that there seems to be new issues in terms of memory handling in model-viewer (or in Chrome, I don't know). I'm not 100% sure but I think this is a regression.
When working on models and doing frequent refreshs (e.g. through Glitch), memory leaks and ultimately leads to browser crash / system freeze. The same seems to happen for graphics memory, I've seen black screens, GPU crashes recently. This is not only on Chrome Windows, but also on Chrome Android (I've had cases of black screens and fully glitched out system-wide GPU issues which I've never seen on Android, needing a full phone restart to resolve).
Might be related to #1074
Live Demo
I've seen this when working on regular 1-model pages, but it reproduces faster with this stress test here: https://glitch.com/edit/#!/model-viewer-stresstest (these are duplicate model files, not the same one loading multiple times).
- open glitch page
- open "In A New Window"
- open the glitch editor and the new window next to each other (so that both are visible)
- open Task Manager to watch RAM
- insert space anywhere in glitch, let page refresh
- wait for loading done
- repeat repeat repeat
- watch RAM all the time

The same seems to be true when just refreshing the page, but I had the feeling it's faster when glitch causes the update (not sure).
Also, I had the feeling that the speed of leaking is increased when the page is in "mobile preview" mode (not sure).

Browser Affected
- [X] Chrome
- [ ] Edge
- [ ] Firefox
- [ ] Helios
- [ ] IE
- [ ] Safari
OS
- [X] Android
- [X] iOS
- [ ] Linux
- [ ] MacOS
- [X] Windows
Versions
- model-viewer: >= v0.9.0 (not sure if a regression or I just now noticed it)
Edit: Checked "iOS" as well as I'm getting the same black-model issues on iOS with the same "strategy" of many refreshes after each other. Sometimes after page refresh, sometimes after coming back from QuickLook.
I also noticed that GPU usage, on a high-end machine, now goes to nearly 100%. Have you maybe removed the frames-per-second clamping? Usually you want to clamp to device framerate, and if not possible, to 60.
@soraryu thanks for reporting. I have to do some testing on my own, but some initial thoughts:
- To some extent, memory leaks may be outside of our control, but we can certainly report them upstream to Chrome. It would be interesting to see if these leaks also happen in other browsers.
- We have had some reports of long-lived
<model-viewer>elements acting badly e.g., #1074
For some initial anecdata, I tried out the stress case in Chrome on my 15" MacBook Pro.

Although overall CPU usage was high, the stress test tab was only at about ~75". This is honestly around what I would expect for what amounts to a very relatively hard scenario for <model-viewer>:


Have you maybe removed the frames-per-second clamping? Usually you want to clamp to device framerate, and if not possible, to 60.
@soraryu on this topic, IIRC we do not rate limit FPS at this time (we just use whatever requestAnimationFrame gives us). This means we'll likely be rendering at 90, 120, 144 FPS etc. depending on your display refresh rate. We haven't done much investigation to determine how problematic this is. Can you confirm your display frequency for us?
I wasn't even aware of it but turns out my laptop has a 240 Hz display. That being said, there's a 2070 Max-Q in there, and I'm developing graphics-heavy apps day in and out with millions of polygons and dozens of fx passes, and the GPU doesn't get as heavy a hit as with model-viewer. I can't remember that being the case with past versions though, but might be mistaken. I definitely didn't have as many (or actually, zero) crashes when I started working with model-viewer.
Also, screen framerate does seem to have some but not much influence on GPU utilisation:
At 240 Hz:

At 60 Hz:

One note: while #1169 should fix performance for dual-GPU systems (most Optimus laptops probably), the other part of this issue was the memory leaks and crashes that occured. These probably still happen the same on lower-end GPUs. Have you looked into those as well @elalish?
@soraryu I have attempted, but haven't met with much success yet. If you manage to find a potential source, let me know and I'll look into it. Chrome's memory info is not making anything particularly clear to me.
@cdata I still have the same issues, but it happens after changing the src with 5 GLB models. Chrome crashes on Android and Safari just reloads the page on iOS. They are loaded in cycle, but each time the RAM fills up by a large amount. One model is about 5 MB, but Chrome memory goes up like 100 MB. I've also opened an issue https://github.com/google/model-viewer/issues/1674