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.

Tuning Android Applications (Part One)


Published on

From th

Published in: Technology

Tuning Android Applications (Part One)

  1. 1. Tuning Android Applications Copyright © 2011 CommonsWare, LLC
  2. 2. What Ill Be Yammering About● Now – Traceview – MAT● After the Keynote – Battery and Bandwidth – Top Ten Tuning Tips Copyright © 2011 CommonsWare, LLC
  3. 3. Traceview Copyright © 2011 CommonsWare, LLC
  4. 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. 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. 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. 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. 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. 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. 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. 11. Memory Analysis with MAT Copyright © 2011 CommonsWare, LLC
  12. 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. 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. 14. Collecting Heap Dumps● From DDMS – Select your process – Click toolbar button● From Java – android.os.Debug – dumpHprofData() Copyright © 2011 CommonsWare, LLC
  15. 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. 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. 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. 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. 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