Andromance - Android Performance


Published on

Android Performance Optimization presentation which is told on @ DevFest Ankara 2013, @ DevFest Eskişehir 2013, @ DevFest Konya 2014, @DevFest İstanbul 2014

Published in: Technology
1 Comment
  • In 27 you suggest foreach instead of for, but foreach creates iterable object and if you use it inside of onDraw methods there will be an object creation overhead, nope?
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Andromance - Android Performance

  1. 1. { Andromance Android Performance Optimization O. Mert Şimşek /orhunmertsimsek
  2. 2. Orhun Mert Şimşek • Co-Founder & CEO of 4pps • Android & iOS Developer • Educational Responsible at GDG Ankara • Android Evangelist
  3. 3. Let’s Boost the App
  4. 4. Use Splash If You Need It is better to show a splash, instead of grotty progress bars
  5. 5. internal Getters & Setters? Without a JIT, direct field access is about 3x faster than invoking a trivial getter. With the JIT (where direct field access is as cheap as accessing a local), direct field access is about 7x faster than invoking a trivial getter.* (*):
  6. 6. StrictMode If you think that you might be doing some bad things accidentally, you should use StrictMode It is commonly used to detect accidental network or disc access on the UI thread.
  7. 7. StrictMode public void onCreate() { if (DEVELOPER_MODE) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() .detectNetwork() // or .detectAll() for all detectable problems .penaltyLog() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects() .detectLeakedClosableObjects() .penaltyLog() .penaltyDeath() .build()); } super.onCreate(); } An example
  8. 8. DDMS
  9. 9. Use Hierarchy Viewer You can determine layers and some performance stats of UI with this tool Hard to use but very effective!
  10. 10. Use Hierarchy Viewer
  11. 11. Use Hierarchy Viewer
  12. 12. Use Less Objects, More Primitives Creating an object is extremely: EXPENSIVE! … but «primitive» is not! Using a 2 different integer array is more efficient than using object arrays like fooBar(int,int) badArray = new FooBar[] { new FooBar(5,8) , new FooBar(84,2) }; firstCoolArray = new int[] { 5, 84 }; secondCoolArray = new int[] { 8 , 2 };
  13. 13. How to Search a 2D Array? Search first for row, then column TO DO for (int i = 0; i < foo.length ; i++) { for (int j = 0; j < foo.length ; j++) { process(foo[i,j]); } } NOT TO DO for (int i = 0; i < foo.length ; i++) { for (int j = 0; j < foo.length ; j++) { process(foo[j,i]); } }
  14. 14. Use «Static» as Possible It is 15% - 20% faster! Static variables are initialized only once , at the start of the execution. And also use «static final» for your constants
  15. 15. «Lint» Usage The lint tool checks your Android project source files for potential bugs and optimization improvements for correctness, security, performance, usability, accessibility, and internationalization. *
  16. 16. «Lint» Usage In Eclipse it is easy to use, just one click! In command line: «lint [flags] <project directory>» «lint myproject» «lint --check MissingPrefix myproject»
  17. 17. Reusable Layouts It is commonly used for title bars, yes-no button panels etc. Very easy to implement: Create a layout and use it in another layout with «<include ../>» tag <include layout="@layout/titlebar"/>
  18. 18. Do Your Hard-Work on Background Threads It is forbidden to make network processes in main thread, since Android 3.0 How about Instagram? When you finished writing some hashtags and pressed to send button, It had been posted just a while ago!
  19. 19. View Holder for ListView Objects A ViewHolder object stores each of the component views inside the ListView object, so you can immediately access them without the need to look them up repeatedly. static class ViewHolder { TextView text; TextView timestamp; ImageView icon; } ViewHolder holder = new ViewHolder(); holder.icon = (ImageView) convertView.findViewById(; holder.text = (TextView) convertView.findViewById(; … Smooth!
  20. 20. XML Drawables PNG’s, JPG’s and others are big sized and a hard work for mobile device. But creating an image with XML is very cheap and it is more efficient than you expected. Using a 400 kb background image or using an Xml drawable which has size of just 300 bytes?
  21. 21. XML Drawables <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="" android:shape="rectangle"> <stroke android:width="3dp" android:color="#000000" /> <gradient android:endColor="#F5CD7A" android:startColor="#657892" android:angle="90" /> </shape>
  22. 22. Handling Overdraws The most important part of performance. – If you ask me! Since Android 4.2, we can see overdraws in our devices. Overdraw is simply, drawing a pixel more than once. It’s notation is also simple: • No color: No overdraw • Blue: 1x overdraw (pixel is drawed 2 times) • Green: 2x overdraw • Light Red: 3x overdraw • Dark Red: 4x or more overdraw (ALERT!)
  23. 23. Handling Overdraws Youtube Spotify
  24. 24. • Acceptable red areas • Mostly blue and green • Optimized by Romain Guy - The God of Android Performance Optimization
  25. 25. • JUST red areas • Whole screen is drawed more than 5 times • Probably haven’t checked for overdraws
  26. 26. Handling Overdraws How to avoid? Actually its secret is written at the last one • thumbnail.setBackgroundColor(0x0); • android:windowBackground="@null" • If a view will not used anymore, don’t make its visibility «INVISIBLE», make it «GONE» • And use a simple design!
  27. 27. Summary and Little Other Tips  Do use two parallel «int» arrays instead of array of objects (int,int)  Do use «primitive» instead of objects  Do use as possible as «static» methods  Do use «static final» for constants  Do use foreach instead of for  Do use as possible as Xml Drawables instead of images (jpg,png)  Don’t create unnecessary objects  Don’t allocate memory if you can work without it  Don’t use getters and setters inside the class  Don’t do your hard works on main thread and the most important one:  Don’t publish your app unless it doesn’t have performance problems!
  28. 28. Torment Is Over, That’s All! Thanks for listening! O. Mert Şimşek /orhunmertsimsek