SlideShare a Scribd company logo
Tuning Android
 Applications
    Copyright © 2011 CommonsWare, LLC
What I'll Be Yammering About
●   Now
    –   Traceview
    –   MAT
●   After the Keynote
    –   Battery and Bandwidth
    –   Top Ten Tuning Tips


                      Copyright © 2011 CommonsWare, LLC
Traceview
  Copyright © 2011 CommonsWare, LLC
Two Types of CPU Problems
●   Generally slow
    –   Foreground
    –   Background
●   UI “janky” (sluggish, jerky, non-responsive)
    –   Work being done on the main application thread
        that perhaps should not be


                      Copyright © 2011 CommonsWare, LLC
Finding Your CPU Problems
●   StrictMode
    –   Reports problems with main application thread
●   Roll-Your-Own Timing
    –   E.g., TimingLogger
●   Traceview
    –   Analyze method traces collected from VM
    –   Shows you where all the time goes
                      Copyright © 2011 CommonsWare, LLC
Collecting Trace Data
●   Start/Stop from DDMS
    –   Choose your process
    –   Toggle toolbar button
●   Start/Stop from Java
    –   android.os.Debug
    –   startMethodTracing()
    –   stopMethodTracing()

                      Copyright © 2011 CommonsWare, LLC
Loading Traceview
●   DDMS in Eclipse
    –   Stopping collection auto-loads Traceview
●   Standalone DDMS
    –   Stopping collection auto-loads standalone
        Traceview (Android 2.2+)
●   Trace-on-External-Storage
    –   Import into Eclipse
    –   Run traceview from command line
                      Copyright © 2011 CommonsWare, LLC
The Traceview Window
●   Top = Timeline
    –   Rows = threads
    –   Shows which threads were active at which times
        during test period
●   Bottom = CPU Consumers
    –   Call tree of methods, showing which methods
        are consuming what amount of time, calls
    –   Color coding matches timeline
                      Copyright © 2011 CommonsWare, LLC
Finding Hot Spots
●   Sort table
    –   Exclusive = time taken up just in this method
    –   Inclusive = time taken up in method and any
        methods it invokes (tree)
●   Find your code
    –   Appearing in left column
    –   In “parents” of other things consuming lots of
        time
                      Copyright © 2011 CommonsWare, LLC
Limitations
●   Disables JIT
    –   Result: all Java code slower than normal
●   Does Not Profile Native Code
    –   Result: native code a “black box”
●   Overall Result: Absolute Times Inaccurate
    –   Looking for relative times compared to other
        Java code

                      Copyright © 2011 CommonsWare, LLC
Memory Analysis
  with MAT
     Copyright © 2011 CommonsWare, LLC
Two Types of Memory Issues
●   Running out of heap
    –   OutOfMemoryError
●   Running out of stack
    –   StackOverflowError
    –   If stack trace shows mostly Android UI rendering
        code, use Hierarchy View to identify deepest
        portions of view hierachy, try to eliminate layers

                       Copyright © 2011 CommonsWare, LLC
Finding Your Heap Problems
●   Allocation Tracking
    –   Through DDMS, records all allocations
         ●   Exception: primitive arrays
    –   See who allocates what, try to figure out why
●   Memory Analyzer (MAT)
    –   Examine heap dump
    –   Find out what is not getting garbage collected
    –   Find out how many objects exist, sizes
                           Copyright © 2011 CommonsWare, LLC
Collecting Heap Dumps
●   From DDMS
    –   Select your process
    –   Click toolbar button
●   From Java
    –   android.os.Debug
    –   dumpHprofData()


                      Copyright © 2011 CommonsWare, LLC
Loading MAT
●   DDMS/Eclipse
    –   Dump automatically loaded into MAT
●   Standalone DDMS
    –   Run hprof-conv on HPROF file, then open in
        MAT
●   Java
    –   Copy file from emulator, run hprof-conv,
        open
                     Copyright © 2011 CommonsWare, LLC
Finding Your Leaks
●   Completely Closed Application
    –   All activities destroyed via BACK (or equivalent)
    –   In theory, none of your objects should appear
    –   Open the Histogram
    –   Enter package in “Regex” (com.yourfirm.*)
    –   See what crops up
         ●   “Merge Shortest Path to GC Roots” context menu,
             excluding weak references, to find out what is
             holding these Copyright © 2011 CommonsWare, LLC
                           things
Finding Where The Heap Went
●   Live Application
    –   Finding out where all the RAM is going
    –   Back in Histogram, sort by # of objects
    –   Looking for unusual stuff with lots of objects
         ●   Bitmaps
         ●   Your objects
         ●   Objects from third-party JAR


                            Copyright © 2011 CommonsWare, LLC
Finding Where The Heap Went
●   Live Application
    –   Bring up the Dominator Tree
    –   Sort by shallow heap (objects' own memory) or
        retained heap (memory object is responsible for)
    –   Again, hunt for unusual stuff with lots of heap




                       Copyright © 2011 CommonsWare, LLC
Limitations
●   Only Tracks Bitmaps on Honeycomb
    –   Previously was “native” memory, counted
        against heap but not visible to MAT
    –   Recommendation
         ●   Focus MAT use on Honeycomb
         ●   Secondary usage on earlier OS levels




                          Copyright © 2011 CommonsWare, LLC

More Related Content

PPTX
Lec 9-os-review
PDF
Deep Parameters Tuning for Android Mobile Apps
PDF
Objective-C for Java developers
PDF
Android performance tuning. Memory.
PDF
iOS Developer Overview - DevWeek 2014
PDF
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
PDF
Introduction to ART (Android Runtime)
PPT
iOS Application Testing
Lec 9-os-review
Deep Parameters Tuning for Android Mobile Apps
Objective-C for Java developers
Android performance tuning. Memory.
iOS Developer Overview - DevWeek 2014
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
Introduction to ART (Android Runtime)
iOS Application Testing

Viewers also liked (19)

PDF
Performance optimization for Android
PDF
LAS16-201: ART JIT in Android N
PDF
Tuning android for low ram devices
PPTX
Android Performance Best Practices
PDF
Objective-C for Java Developers
ODP
Java Garbage Collection, Monitoring, and Tuning
PPT
Jvm Performance Tunning
PPTX
Layer architecture of ios (1)
PDF
Building iOS App Project & Architecture
PDF
MySQL Performance Tuning: Top 10 Tips
PPTX
Google ART (Android RunTime)
PDF
Designing better user interfaces
PPT
Android & iOS Automation Using Appium
PDF
Architecting iOS Project
PDF
iOS for Android Developers (with Swift)
PDF
MySQL Tuning
PDF
Android vs iPhone - Differences in UI Patterns and Design
PPTX
Ios vs android
PDF
iOS design: a case study
Performance optimization for Android
LAS16-201: ART JIT in Android N
Tuning android for low ram devices
Android Performance Best Practices
Objective-C for Java Developers
Java Garbage Collection, Monitoring, and Tuning
Jvm Performance Tunning
Layer architecture of ios (1)
Building iOS App Project & Architecture
MySQL Performance Tuning: Top 10 Tips
Google ART (Android RunTime)
Designing better user interfaces
Android & iOS Automation Using Appium
Architecting iOS Project
iOS for Android Developers (with Swift)
MySQL Tuning
Android vs iPhone - Differences in UI Patterns and Design
Ios vs android
iOS design: a case study
Ad

Similar to Tuning Android Applications (Part One) (20)

PDF
Web Sphere Problem Determination Ext
PDF
Eclipse Memory Analyzer Tool
PDF
Tuning Android Applications (Part Deux)
PPT
Using and Extending Memory Analyzer into Uncharted Waters
ODP
Memory management
ODP
Android Nâng cao-Bài 9-Debug in Android Application Development
PPTX
Memory Analyzer Tool (MAT)
PDF
Memory management for_android_apps
PDF
X Means Y
PDF
TechGIG_Memory leaks in_java_webnair_26th_july_2012
PDF
ppt7_App Debug Framework_D2 to dowwnload.pdf
PDF
Tools and Tips to Diagnose Performance Issues
PDF
Android app to the challenge
PPT
Java Performance Monitoring & Tuning
PPTX
Eclispe daytoulouse combining the power of eclipse with android_fr_1024_768_s...
PPTX
Of Bytes, Cycles and Battery Life
PDF
Java Memory Analysis: Problems and Solutions
PDF
Memory Leak Profiling with NetBeans and HotSpot
PPTX
Lab: JVM Production Debugging 101
PPTX
Detecting Memory Leaks in Android App
Web Sphere Problem Determination Ext
Eclipse Memory Analyzer Tool
Tuning Android Applications (Part Deux)
Using and Extending Memory Analyzer into Uncharted Waters
Memory management
Android Nâng cao-Bài 9-Debug in Android Application Development
Memory Analyzer Tool (MAT)
Memory management for_android_apps
X Means Y
TechGIG_Memory leaks in_java_webnair_26th_july_2012
ppt7_App Debug Framework_D2 to dowwnload.pdf
Tools and Tips to Diagnose Performance Issues
Android app to the challenge
Java Performance Monitoring & Tuning
Eclispe daytoulouse combining the power of eclipse with android_fr_1024_768_s...
Of Bytes, Cycles and Battery Life
Java Memory Analysis: Problems and Solutions
Memory Leak Profiling with NetBeans and HotSpot
Lab: JVM Production Debugging 101
Detecting Memory Leaks in Android App
Ad

More from CommonsWare (20)

PDF
Gradle and Your Android Wearable Projects
PDF
Getting Android Developers for Your Wearables
PDF
When Microwatts Are Precious: Battery Tips for Wearable Apps
PDF
The Action Bar: Front to Back
PDF
Android Security: Defending Your Users
PDF
Secondary Screen Support Using DisplayManager
PDF
Mastering the Master Detail Pattern
PDF
Not Quite As Painful Threading
PDF
Android Development: The 20,000-Foot View
PDF
Maps V2... And You!
PDF
A Deep Dive Into ViewPager
PDF
Second-Screen Support in Android 4.2
PDF
Integrate Android Apps and Web Apps
PDF
From Android to the Mobile Web
PDF
The Wonderful World of Wearables
PDF
Securing User Data with SQLCipher
PDF
Beaming Data to Devices with NFC
PDF
What's New in Jelly Bean
PDF
Making Money at Mobile: 60 Business Models
PDF
AppsWorld Keynote
Gradle and Your Android Wearable Projects
Getting Android Developers for Your Wearables
When Microwatts Are Precious: Battery Tips for Wearable Apps
The Action Bar: Front to Back
Android Security: Defending Your Users
Secondary Screen Support Using DisplayManager
Mastering the Master Detail Pattern
Not Quite As Painful Threading
Android Development: The 20,000-Foot View
Maps V2... And You!
A Deep Dive Into ViewPager
Second-Screen Support in Android 4.2
Integrate Android Apps and Web Apps
From Android to the Mobile Web
The Wonderful World of Wearables
Securing User Data with SQLCipher
Beaming Data to Devices with NFC
What's New in Jelly Bean
Making Money at Mobile: 60 Business Models
AppsWorld Keynote

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Machine learning based COVID-19 study performance prediction
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Empathic Computing: Creating Shared Understanding
PPTX
A Presentation on Artificial Intelligence
PDF
Modernizing your data center with Dell and AMD
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Encapsulation theory and applications.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
cuic standard and advanced reporting.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
MYSQL Presentation for SQL database connectivity
Unlocking AI with Model Context Protocol (MCP)
The Rise and Fall of 3GPP – Time for a Sabbatical?
Machine learning based COVID-19 study performance prediction
Network Security Unit 5.pdf for BCA BBA.
NewMind AI Monthly Chronicles - July 2025
Empathic Computing: Creating Shared Understanding
A Presentation on Artificial Intelligence
Modernizing your data center with Dell and AMD
Encapsulation_ Review paper, used for researhc scholars
Encapsulation theory and applications.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
cuic standard and advanced reporting.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Big Data Technologies - Introduction.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Mobile App Security Testing_ A Comprehensive Guide.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
MYSQL Presentation for SQL database connectivity

Tuning Android Applications (Part One)

  • 1. Tuning Android Applications Copyright © 2011 CommonsWare, LLC
  • 2. What I'll Be Yammering About ● Now – Traceview – MAT ● After the Keynote – Battery and Bandwidth – Top Ten Tuning Tips Copyright © 2011 CommonsWare, LLC
  • 3. Traceview Copyright © 2011 CommonsWare, LLC
  • 4. Two Types of CPU Problems ● Generally slow – Foreground – Background ● UI “janky” (sluggish, jerky, non-responsive) – Work being done on the main application thread that perhaps should not be Copyright © 2011 CommonsWare, LLC
  • 5. Finding Your CPU Problems ● StrictMode – Reports problems with main application thread ● Roll-Your-Own Timing – E.g., TimingLogger ● Traceview – Analyze method traces collected from VM – Shows you where all the time goes Copyright © 2011 CommonsWare, LLC
  • 6. Collecting Trace Data ● Start/Stop from DDMS – Choose your process – Toggle toolbar button ● Start/Stop from Java – android.os.Debug – startMethodTracing() – stopMethodTracing() Copyright © 2011 CommonsWare, LLC
  • 7. Loading Traceview ● DDMS in Eclipse – Stopping collection auto-loads Traceview ● Standalone DDMS – Stopping collection auto-loads standalone Traceview (Android 2.2+) ● Trace-on-External-Storage – Import into Eclipse – Run traceview from command line Copyright © 2011 CommonsWare, LLC
  • 8. The Traceview Window ● Top = Timeline – Rows = threads – Shows which threads were active at which times during test period ● Bottom = CPU Consumers – Call tree of methods, showing which methods are consuming what amount of time, calls – Color coding matches timeline Copyright © 2011 CommonsWare, LLC
  • 9. Finding Hot Spots ● Sort table – Exclusive = time taken up just in this method – Inclusive = time taken up in method and any methods it invokes (tree) ● Find your code – Appearing in left column – In “parents” of other things consuming lots of time Copyright © 2011 CommonsWare, LLC
  • 10. Limitations ● Disables JIT – Result: all Java code slower than normal ● Does Not Profile Native Code – Result: native code a “black box” ● Overall Result: Absolute Times Inaccurate – Looking for relative times compared to other Java code Copyright © 2011 CommonsWare, LLC
  • 11. Memory Analysis with MAT Copyright © 2011 CommonsWare, LLC
  • 12. Two Types of Memory Issues ● Running out of heap – OutOfMemoryError ● Running out of stack – StackOverflowError – If stack trace shows mostly Android UI rendering code, use Hierarchy View to identify deepest portions of view hierachy, try to eliminate layers Copyright © 2011 CommonsWare, LLC
  • 13. Finding Your Heap Problems ● Allocation Tracking – Through DDMS, records all allocations ● Exception: primitive arrays – See who allocates what, try to figure out why ● Memory Analyzer (MAT) – Examine heap dump – Find out what is not getting garbage collected – Find out how many objects exist, sizes Copyright © 2011 CommonsWare, LLC
  • 14. Collecting Heap Dumps ● From DDMS – Select your process – Click toolbar button ● From Java – android.os.Debug – dumpHprofData() Copyright © 2011 CommonsWare, LLC
  • 15. Loading MAT ● DDMS/Eclipse – Dump automatically loaded into MAT ● Standalone DDMS – Run hprof-conv on HPROF file, then open in MAT ● Java – Copy file from emulator, run hprof-conv, open Copyright © 2011 CommonsWare, LLC
  • 16. Finding Your Leaks ● Completely Closed Application – All activities destroyed via BACK (or equivalent) – In theory, none of your objects should appear – Open the Histogram – Enter package in “Regex” (com.yourfirm.*) – See what crops up ● “Merge Shortest Path to GC Roots” context menu, excluding weak references, to find out what is holding these Copyright © 2011 CommonsWare, LLC things
  • 17. Finding Where The Heap Went ● Live Application – Finding out where all the RAM is going – Back in Histogram, sort by # of objects – Looking for unusual stuff with lots of objects ● Bitmaps ● Your objects ● Objects from third-party JAR Copyright © 2011 CommonsWare, LLC
  • 18. Finding Where The Heap Went ● Live Application – Bring up the Dominator Tree – Sort by shallow heap (objects' own memory) or retained heap (memory object is responsible for) – Again, hunt for unusual stuff with lots of heap Copyright © 2011 CommonsWare, LLC
  • 19. Limitations ● Only Tracks Bitmaps on Honeycomb – Previously was “native” memory, counted against heap but not visible to MAT – Recommendation ● Focus MAT use on Honeycomb ● Secondary usage on earlier OS levels Copyright © 2011 CommonsWare, LLC