vs. cached - Cached processes can be killed - Order of killing is LRU • with other modifications • Cached processes help Android user experience *See “Processes and Threads” in API Guides documentation
- Share across processes, allows paging • zygote - First Dalvik process, from which all others fork - Preloads commonly used classes, resources - Common RAM shared across forked processes • ashmem - Large allocations shared instead of copied - Cursors, some graphics resources
identical pages between processes - Copy-on-write • Really useful with bitmaps - Bitmaps allocated on the Dalvik heap - byte[] allocations are zeroed out by the VM
overhead 8 Reference 4 Reference 4 int 4 Reference 4 class HashMap$HashMapEntry<K, V> { final K key; V value; final int hash; HashMapEntry<K, V> next; }
overhead 8 Reference 4 Reference 4 int 4 Reference 4 Total = 4 + 8 + 4 * 4 = 28 bytes class HashMap$HashMapEntry<K, V> { final K key; V value; final int hash; HashMapEntry<K, V> next; } Aligned total = 32 bytes
• Directly reduce available cached processes • Remember, no swap • Services should have well-defined durations • Services left running is a common application problem
width * height * depth - Optimize for size • Take care with caches of bitmaps • Android 3.0: bitmaps in Dalvik heap - Reuse when possible • See: http://developer.android.com/training/displaying-bitmaps/ manage-memory.html
- Allocation-free - Same API as HashMap (implements Map interface) - Available in support library • Drawback - Slower than HashMap, don’t use for large collections
- The heap will fragment! • Can shrink if unused space at end of range • madvise used to free individual pages inside of the range - returns memory to kernel • even if the heap size does not shrink
a while - 2. Look at heap - 3. Profit! • Caveats: - Use “adb shell dumpsys meminfo <app>” for initial overview - Finding large leaks is easy (sort by size) - finding systemic memory problems is often hard
time • DDMS allocation tracker: - Select app - “Start tracking” - Interact with app - “Get allocations” - Click to see stack • Good tool for jank, too!
time • DDMS allocation tracker: - Select app - “Start tracking” - Interact with app - “Get allocations” - Click to see stack • Good tool for jank, too! Great
1.5 MB USS - Ready to show UI: 4 MB USS - Showing UI: much more • Avoid multiple processes in general • Possible to run multiple apps in one process - Activity’s android:process attribute