Debugging OOMs: SQLite Query Optimization
Article Summary
Brian Terczynski from Thumbtack tracked down a brutal Android OOM bug that was crashing apps at startup—and the culprit wasn't a memory leak. It was hiding in plain sight: their SQLite queries.
Thumbtack's Android team faced mysterious OutOfMemory crashes that left no obvious traces. Traditional debugging tools like Leak Canary and Android Studio Profiler found nothing. The breakthrough came from examining alternate thread stack traces and discovering users with massive event log backlogs.
Key Takeaways
- Query was loading 50,000+ events into memory just to count them (50MB+ wasted)
- Replaced list operations with SQL COUNT and LIMIT, dropping OOMs by over 90%
- Crashing thread was innocent; real issue was hidden in background logging worker
- Missing diagnostic logs were the key clue to locating the problem
Critical Insight
Switching from in-memory list operations to proper SQL queries (COUNT and LIMIT) eliminated 90%+ of OOM crashes and dramatically improved crash-free user rates.