Clean Android WebView Caching
Article Summary
Microsoft Teams Android engineers cut WebView initialization time by 70% using a clever caching technique. Here's how they avoided memory leaks while sharing WebViews across activities.
The Teams Android app hosts multiple third-party web apps, each requiring costly WebView initialization (3.8s P95). Engineer Vishal Ratna shares how the team solved this performance bottleneck without leaking activity contexts.
Key Takeaways
- WebView initialization dropped from 3.8s to 1.2s (70% improvement)
- MutableContextWrapper enables safe WebView pooling with application context
- Pool swaps activity context on obtain() and application context on release()
- Approach prevents memory leaks while maintaining most WebView functionality
- Solution works for apps needing multiple WebView instances across activities
By creating WebViews with application context wrapped in MutableContextWrapper, Teams achieved 70% faster load times without memory leaks.
About This Article
The Microsoft Teams Android app had a 3.8s P95 latency problem during WebView JavaScript initialization. Each ShellActivity instance needed its own WebView setup to run hosted third-party applications, which made things slow.
Vishal Ratna's team built a WebViewPool that caches WebView instances using MutableContextWrapper. When you call obtain() or release(), the pool swaps in the activity context dynamically instead of creating new instances each time.
After the fix, WebView initialization dropped to 1.2s in production. That's a 70% improvement, and the team confirmed there were no activity context memory leaks.