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.

Profiling tools and Android Performance patterns

1,131 views

Published on

Profiling is one of the most important steps in the development cycle of any app. Although this is well known, we as developers usually don’t know the potential of every tool that we have to help us. In this presentation we would like to introduce the most useful tools at our disposal that we may use to profile the GPU and memory performance, the most typical problems that our apps might have and how to solve them making use of the android performance patterns.

Published in: Software
  • Be the first to comment

Profiling tools and Android Performance patterns

  1. 1. Profiling tools and Android performance patterns Providing a good user experience
  2. 2. The mobile agency for food retail
  3. 3. Jonathan Maldonado Contreras @Jonazan2 Android Developer
  4. 4. “Mobile internet will become the most important medium in people's lives.” Founded in 2002
  5. 5. • Adding emotion to transactional loyalty • 120 developers, designers & specialists, HQ in Amsterdam, NL • Operating in 3 continents: Europe, North America and Asia The magic happens at the intersection IceMobile Creativity Technology
  6. 6. Performance is about user experience Apps nowadays are all about fancy animations, complex transitions and custom views And the experience must be the most robust and similar as possible in any device
  7. 7. CPU GPU BatteryMemory Network
  8. 8. GPU profiling Understanding the rendering pipeline
  9. 9. Our apps must run at 60 frames per second ———— 1000ms 60 frames = 16,66ms per frame In this 16,66 ms we have to: • Do any computation • Update our screen to the GPU • Draw the screen • Deal with GC events
  10. 10. Settings Developer options Profile GPU rendering Show on bars • Green: 16ms barrier • Blue: create and update the views display list • Purple (4.0+): time transferring resources to rendering thread • Red: android 2d renderer in OpenGL • Orange: CPU’s waiting time Profiling GPU rendering 16ms
  11. 11. A lot of frame drops? Check for: • Unnecessary invalidations that may trigger a cascade of invalidations. (GPU View Updates can help profiling) • RelativeLayouts in a high hierarchy level • Nested weights in LinearLayouts • Unnecessary overdraw • Customs views not properly made Patterns to avoid bad performance
  12. 12. • Use <merge> as root in your layout • Avoid unnecessary layouts • Don’t make allocations or heavy operations in OnDraw • Remove unnecessary calls to invalidate() • Consider creating your own ViewGroup invalidate() hierarchy invalidations onDraw() Patterns for custom views Make them as simple as possible OnMeasure/OnLayout…
  13. 13. • True color: No overdraw • Blue: Overdrawn once • Green: Overdrawn twice • Pink: Overdrawn three times • Red: Overdrawn four times Profiling GPU overdraw
  14. 14. • Depends on the GPU architecture: Nvidia’s Tegra is different than ImaginationTech’s SGX and so on. • Hierarchy Viewer (ViewServer by @RomainGuy for any phone) and Tracer Steps to follow when you have too much overdraw: • Simplify your drawables • Use 9patch with transparent parts • Caution with setting alpha in your views About overdraw
  15. 15. Memory profiling Understanding the garbage collector
  16. 16. Displays: • Free and allocated memory in real time • GC events Allows us: • Cause GC events • Start the allocation tracker • Dump the Java Heap Memory monitor
  17. 17. • If our app is using an excessive amount of memory • When your app is running out of memory • If a GC event occurs and the memory is not properly free • If the app is slow due to a vast amount of GC events, a GC cascade What the memory monitor can tell us Runtime totalMemory() maxMemory() current heap size (bytes) max heap size (bytes)
  18. 18. Heap dump We can use the heap dump to check: • The current memory state for our app • The amount of objects • Type of those objects • Hierarchy between references
  19. 19. Allocation Tracker Allocation tracker displays: • Which classes and methods are allocating which objects • It’s possible to see allocated memory in each thread of your app
  20. 20. Allocation tracker Heap Memory monitor All integrated within Android Studio
  21. 21. Using our profiling tools in Android Studio: 1. Open memory monitor before opening an activity 2. Start the allocation tracker and dump the heap after loading the activity 3. Go to a different activity and force a GC event 4. Stop allocation tracker, dump the heap...and investigate Discovering memory leaks Allocation space in the HEAP * The allocation space is different between Dalvik andART Allocation space in the HEAP GC EVENT Leaked Memory Leaked Memory Allocated Memory Allocated Memory Needed Memory Needed Memory OUT OF MEMORY ERROR
  22. 22. Don’t leak contexts in inner classes. Don’t leak views inside an activity. Don’t use WeakHashMap as cache. Only the keys are weak references. Use the proper context. AppContext != View context Use static classes instead of Inner classes. Use WeakReference or SoftReference. Release callbacks in a proper way (handlers, observers...) Avoiding memory leaks
  23. 23. The principal cause of GC cascades is memory churn: high number of objects get allocated and potentially free in a short period of time. GC cascades: discovering and avoiding memory churn 16ms 16ms 16ms 16ms GCUPDATE GCUPDATE UP D Frame drop GC ATE draw draw draw
  24. 24. • Don’t allocate a large amount of unnecessary objects • Immutable classes: String • Autoboxing: Integer, Boolean… • Don’t allocate in onDraw • Consider using Object Pools Patterns to avoid memory churn
  25. 25. • SparseArray family to avoid autoboxing • Hundred of objects, not thousands • Use for with size and get • Integer constants • Unnecessary objects: • Inmutable classes: String • Autoboxing: Integer, Boolean… • Foreach and iterators • Enums Patterns to use in critic areas of the app inner loops animations custom views
  26. 26. • Handle Bitmaps in a proper way: • Decode bitmaps to the desire size • BitmapFactory.Options (inSampleSize, inDensity, inTargetDensity) • Load bitmaps in memory at the dimensions is going to be displayed • Don’t scale if you don’t need to (createScaledBitmap(btimap, int, int). • Use LRU cache. Handling bitmaps inSampleSize density
  27. 27. Performance is necessary for every app “If we can write a map in a single line and you can put a value and get a value, we don’t care how long it takes...” Conference attendant at CppCon2014 “Ok, [...] people who don’t care about how long it takes is also the reason why I’ve to wait 30 seconds for Word to boot up” Mike Acton Engine director at Insomniac Games
  28. 28. Tools, not rules
  29. 29. • Android Performance Case Study by @RomainGuy • Performance Profiling Tools - https://developer.android.com/tools/ performance/index.html • Android performance patterns videos by Colt McAnlis (@duhroach) • Google + community: http://goo.gl/g7mxmI References
  30. 30. @icemobile /company/icemobileicemobile.com/tech Thank you!

×