Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Memory , Where is all My RAM

Android Memory , Where is all My RAM

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

Yossi Elkrief

December 08, 2015
Tweet

More Decks by Yossi Elkrief

Other Decks in Programming

Transcript

  1. 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
  2. Rights reserved Android Memory Android 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.
  3. Rights reserved • The Java heap • The native heap

    • Android shared memory - ashmem. Android Memory Regions
  4. 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.
  5. 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
  6. Rights reserved How Android Manages Memory • Sharing Memory •

    App is Forked from Zygote. • Zygote starts on system boot , loads common framework code and resources.
  7. 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.
  8. Rights reserved How Android Manages Memory • Restricting App Memory

    • Hard limit heap size for each app • Heap size limit varies between devices • ActivityManager.getMemoryClass ()
  9. Rights reserved How Android Manages Memory • Switching Apps •

    Android keeps processes cached • Least-recently used (LRU) cache
  10. 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.
  11. Rights reserved Memory Leak • Application tries to exceed the

    available memory, • We get Out of Memory.
  12. Rights reserved Viewing Overall Memory Allocations • Private RAM •

    Clean • Dirty • Proportional Set Size (PSS) adb shell dumpsys meminfo <package_name>
  13. 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.
  14. 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.
  15. 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
  16. 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)>
  17. 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.
  18. Rights reserved References • SoftReference • WeakReference • PhantomReference. •

    "Weakness" less restrictions on the garbage collector as to when it is allowed to actually
  19. 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.
  20. 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.
  21. 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.
  22. Rights reserved Process Stats • Available since KitKat • Settings

    > Developer options > Process Stats. • Usage is quite simple. • Try it out now
  23. Rights reserved • Anonymous Inner Class - keeps a reference

    to instance of parent. • Activity leak • Threads leak • Handler leak
  24. 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.
  25. Rights reserved LeakCanary • A memory leak detection library for

    Android and Java. • Built by Square • Easy to integrate • Full open source project - on Github • https://github.com/square/leakcanary
  26. Rights reserved Tips to Go • Avoid Memory Churn. •

    Avoid Objects allocation inside onDraw(). • Keep an eye on your References. • Avoid Soft References for caching
  27. 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
  28. 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
  29. Rights reserved Resources •developer.android.com •Multitasking-android-way •process stats
 Roboto font. 


    Inside Techsmith images • Dennis Cornwell
 
 Keynote animation and style • Cyril Mottier https://github.com/MaTriXy/RecyclerviewDemos • Yossi Elkrief