Situation: I'm working on a pretty decently complex single page Backbone app that could potentially be running for 8-12+ hours straight. Because of this, there's a need to ensure that the application won't leak and have a reputation for crashing after X hours or slow down dramatically.
The Application: The app is built on Backbone (mv*), Zepto (similar to jquery), Curl (amd loader) & Mustache (templating).
Problem: I've just conquered the event listeners. The garbage collector seems to be doing a fine job cleaning these guys up, but the DOM Node Count won't stop climbing.
Questions:
- Is there a proper way to dispose of DOM Nodes so that they will be properly garbage collected, or is this DOM Node Count a running total that will never decrease?
- Does anybody know of any of these frameworks to poorly handle DOM Nodes? Possibly Mustache?
- Is the DOM Node Count even a reliable figure?
I'm really just looking for a head start on my adventure to stop these DOM Nodes from rising. Any help or guidance would be greatly appreciated (and accordingly upvoted).
I assumed that once the event listeners were properly disposed of that the DOM Node Count would just manage itself, but this doesn't seem to be the case.
Tests
- First Test: 6.8 minutes, 110,000 DOM Nodes
Edit: Without the Timeline recording, I reran the same script to randomly mash links and took a screenshot at around the 7 minute mark. After GC came through I had these results.
- Second Test: 7.1 minutes, 141,000 DOM Nodes (Without the timeline recording)
Edit: After Fix:
After Upgrade Backbone and using listenTo and stopListening everywhere
- 7 minutes: 6,926 DOM Nodes (see marked answer below).
- 20 minutes: 6,000 DOM Nodes, 20 Event Listeners, Memory 20 MB.
- 25 minutes: 11,600 DOM Nodes, 44 Listeners, Memory 21.7 MB.
- 28 minutes: 9,000 DOM Nodes, 22 Event Listeners, Memory 21.7 MB.
- 30 minutes: 13,700 DOM Nodes, 123 Event Listeners, Memory 21.7.
- 31 minutes: 7,040 DOM Nodes, 30 Listeners, Memory 21.7.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…