Microsoft Vishal Ratna Aug 18, 2020

Clean Android WebView Caching

M4 Related OWASP risk: Insufficient Input/Output Validation Learn more →

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

Critical Insight

By creating WebViews with application context wrapped in MutableContextWrapper, Teams achieved 70% faster load times without memory leaks.

The article includes production-ready code for both single and fixed-size WebView pools, plus the critical tradeoffs you need to know before implementing this pattern.

About This Article

Problem

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.

Solution

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.

Impact

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.