Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Android Memory , Where is all My RAM


Published on

Android Memory
Android RAM
Android best practices
Leak Canary
Android memory usage

Published in: Software

Android Memory , Where is all My RAM

  1. 1. Rights reserved Android Memory "Where is all my RAM"
  2. 2. Rights reserved Deep Dive Into State Restoration
  3. 3. Rights reserved +Yossi Elkrief 
 @elkriefy MaTriXy Android Group leader Tikal Proud Father Android developer since 2008 GDG Beer Sheva co-founder & leader Android Mentor @ Google Campus Android Lecturer & speaker LFC fan Androids collector
  4. 4. Rights reserved Android Memory
  5. 5. Rights reserved Android MemoryAndroid Memory • Android does not offer swap space for memory. • Android’s VM performs routine GC. • Don’t ignore when and where app allocates and releases memory. • The only way is analyze app’s memory usage with tools and proper care.
  6. 6. Rights reserved • The Java heap • The native heap • Android shared memory - ashmem. Android Memory Regions
  7. 7. Rights reserved Android Memory Regions • The Java heap per-application limited size by the device manufacturer. Memory is garbage-collected. • The native heap used by the C++ new operator.
 The app is limited only by the physical memory available on the device. There is no garbage collection. • Android shared memory - ashmem. 
 Android can “unpin” the memory rather than freeing it. This is a lazy free; the memory is freed only if the system actually needs more memory. When Android “pins” the memory back, old data will still be there if it hasn't been freed.
  8. 8. Rights reserved • android:largeHeap • All processes in application. • Shared user ID - all must have same Flag. • Better focus on reducing overall memory usage. • Does not guarantee a fixed increase in available memory. Increase Heap size
  9. 9. Rights reserved Viewing Heap Updates
  10. 10. Rights reserved How Android Manages Memory • Sharing Memory • App is Forked from Zygote. • Zygote starts on system boot , loads common framework code and resources.
  11. 11. Rights reserved How Android Manages Memory • Static data is mmapped into a process. 
 Allows page out when needed. • Android shares dynamic RAM across processes using explicitly allocated shared memory regions.
  12. 12. Rights reserved How Android Manages Memory • Restricting App Memory • Hard limit heap size for each app • Heap size limit varies between devices • ActivityManager.getMemoryClass ()
  13. 13. Rights reserved How Android Manages Memory • Switching Apps • Android keeps processes cached • Least-recently used (LRU) cache
  14. 14. Rights reserved
  15. 15. Rights reserved Memory Leak • Memory leak is when code has a reference to an object that is no longer needed, preventing the VM to perform the garbage collection for that reference. • Degradation in performance as the memory consumption increases.
  16. 16. Rights reserved Memory Leak • Application tries to exceed the available memory, • We get Out of Memory.
  17. 17. Rights reserved Viewing Overall Memory Allocations • Private RAM • Clean • Dirty • Proportional Set Size (PSS) adb shell dumpsys meminfo <package_name>
  18. 18. Rights reserved Private (Clean and Dirty) RAM • Memory that is being used by only your process. • RAM that the system can reclaim when your app’s process is destroyed. • Most important portion is “private dirty” RAM - the most expensive. • Used by only your process and its contents exist only in RAM so can’t be paged to storage. • Dalvik and native allocations will be private dirty RAM • Dalvik and native allocations you share with the Zygote process are shared dirty RAM.
  19. 19. Rights reserved Proportional Set Size (PSS) • Measurement of app’s RAM usage that takes into account sharing pages across processes. • RAM pages that are unique to process directly contribute to its PSS value. • Shared Pages contribute to the PSS value only in proportion to the amount of sharing. • For example, a page that is shared between two processes will contribute half of its size to the PSS of each process.
 • PSS from all processes actual memory usage in device. • Good Comparison against the RAM use of other processes.
  20. 20. Rights reserved GC Log Messages • logcat prints a message every GC (Only on Dalvik): • ART GC Log message. • Explicit GC • GC pause >5ms • GC duration > 100ms
  21. 21. Rights reserved Interpreting Log Messages • D/dalvikvm: <GC_Reason> <Amount_freed>, <Heap_stats>, <External_memory_stats>, <Pause_time>
  22. 22. Rights reserved Interpreting Log Messages • I/art: <GC_Reason> <GC_Name> <Objects_freed>(<Size_freed>) AllocSpace Objects, <Large_objects_freed>(<Large_object_size_freed>) <Heap_stats> LOS objects, <Pause_time(s)>
  23. 23. Rights reserved Tracking Allocations • Narrowing down memory issues. • Better understanding memory-hogging objects allocations. • Useful to analyze critical code paths. • For example - flinging a list in your app • Viewing all the allocations that need to be done for that behaviour. • What thread they are on. • Where they came from.
  24. 24. Rights reserved Tracking Allocations Demo
  25. 25. Rights reserved References • SoftReference • WeakReference • PhantomReference. • "Weakness" less restrictions on the garbage collector as to when it is allowed to actually
  26. 26. Rights reserved References • SoftReference • Object is not strongly reachable. • There is at least one path from the root set to the object that does traverse a java.lang.ref.SoftReference.
  27. 27. Rights reserved References • WeakReference • Object is neither strongly nor softly reachable. • There is at least one path from the root set to the object that does traverse a java.lang.ref.WeakReference.
  28. 28. Rights reserved References • PhantomReference. • Object is neither strongly, softly, nor weakly reachable. • The object is referenced by a java.lang.ref.PhantomReference instance. • The object has already been finalized.
  29. 29. Rights reserved Eclipse Memory Analyser Tool MAT
  30. 30. Rights reserved Process Stats: Understanding How Your App Uses RAM
  31. 31. Rights reserved Process Stats • Available since KitKat • Settings > Developer options > Process Stats. • Usage is quite simple. • Try it out now
  32. 32. Rights reserved Memory leaks Examples
  33. 33. Rights reserved • Anonymous Inner Class - keeps a reference to instance of parent. • Activity leak • Threads leak • Handler leak
  34. 34. Rights reserved Hunting memory leaks • Get an OutOfMemoryError (OOM) crashe/s . • Attempt to reproduce the problem. You need to figure out what navigation sequence triggers the leak, possibly by brute force. • Dump the heap when the OOM occurs. • Poke around the heap dump and find an object that should have been garbage collected. • Compute the shortest strong reference path from that object to the GC roots. • Figure out which reference in the path should not exist, and fix the memory leak.
  35. 35. Rights reserved
  36. 36. Rights reserved
  37. 37. Rights reserved
  38. 38. The story of LeakCanary
  39. 39. Rights reserved LeakCanary: Detect all memory leaks!
  40. 40. Rights reserved LeakCanary • A memory leak detection library for Android and Java. • Built by Square • Easy to integrate • Full open source project - on Github •
  41. 41. Rights reserved Tips to Go • Avoid Memory Churn. • Avoid Objects allocation inside onDraw(). • Keep an eye on your References. • Avoid Soft References for caching
  42. 42. Rights reserved • Avoid using non-static inner classes in an activity if instances of the inner class could outlive the activity's lifecycle. • Avoid passing anonymous inner class objects to
 static / singleton classes which keep a strong reference to it. • Don’t hold references to activities whenever possible. Tips to Go
  43. 43. Rights reserved • Use objects pool - created once and reused. • Blank activity with very little memory consumption, transition to it and force a GC. • Use Heap dump. • Use Allocation Tracker. before and after the transition. Tips to Go
  44. 44. Rights reserved • GC : Don’t call me…
  45. 45. Till next time, Yossi is satisfied
  46. 46. Rights reserved Thank you!
  47. 47. Rights reserved Resources • •Multitasking-android-way •process stats
 Roboto font. 
 Inside Techsmith images • Dennis Cornwell
 Keynote animation and style • Cyril Mottier • Yossi Elkrief