Slide 1

Slide 1 text

Hasan Hosgel Android Developer @ Kitchen Stories anyone can cook #PERFMATTERS for Android

Slide 2

Slide 2 text

Hasan Hosgel #dfist Why we are developers?

Slide 3

Slide 3 text

Hasan Hosgel #dfist Why we are developers? ESPECIALLY ANDROID

Slide 4

Slide 4 text

Hasan Hosgel #dfist Source: https://www.flickr.com/photos/theinfamousgdub/1765952198 Source: https://www.flickr.com/photos/theinfamousgdub/1765952198

Slide 5

Slide 5 text

Hasan Hosgel #dfist We are smart! Source: https://www.flickr.com/photos/theinfamousgdub/1765952198 Source: https://www.flickr.com/photos/theinfamousgdub/1765952198

Slide 6

Slide 6 text

Hasan Hosgel #dfist

Slide 7

Slide 7 text

Hasan Hosgel #dfist We Love Android!

Slide 8

Slide 8 text

Hasan Hosgel #dfist Source: Photo by Sujaki-F

Slide 9

Slide 9 text

Hasan Hosgel #dfist We have Passion! Source: Photo by Sujaki-F

Slide 10

Slide 10 text

Hasan Hosgel #dfist We have Passion! Source: Photo by Sujaki-F

Slide 11

Slide 11 text

Hasan Hosgel #dfist We have Passion! Source: Photo by Sujaki-F We love challenges!

Slide 12

Slide 12 text

Hasan Hosgel #dfist Source: https://www.flickr.com/photos/68751915@N05/6793826885

Slide 13

Slide 13 text

Hasan Hosgel #dfist We love Money! Source: https://www.flickr.com/photos/68751915@N05/6793826885

Slide 14

Slide 14 text

Hasan Hosgel #dfist We love to delight our users Source: http://agenciabrasil.ebc.com.br/geral/foto/2014-07/cerimonia-de-premiacao-da-copa-do-mundo-no-brasil

Slide 15

Slide 15 text

Hasan Hosgel #dfist User Expectations Source: https://ssl.www8.hp.com/ww/en/secure/pdf/4aa5-7696enw.pdf

Slide 16

Slide 16 text

Hasan Hosgel #dfist User Expectations •  Mobile App Users are impatient Source: https://ssl.www8.hp.com/ww/en/secure/pdf/4aa5-7696enw.pdf

Slide 17

Slide 17 text

Hasan Hosgel #dfist User Expectations •  Mobile App Users are impatient •  61% app start < 4 s Source: https://ssl.www8.hp.com/ww/en/secure/pdf/4aa5-7696enw.pdf

Slide 18

Slide 18 text

Hasan Hosgel #dfist User Expectations •  Mobile App Users are impatient •  61% app start < 4 s •  49% app start < 2 s Source: https://ssl.www8.hp.com/ww/en/secure/pdf/4aa5-7696enw.pdf

Slide 19

Slide 19 text

Hasan Hosgel #dfist User Expectations •  Mobile App Users are impatient •  61% app start < 4 s •  49% app start < 2 s •  Users are intolerant of issues and are quick to uninstall mobile apps Source: https://ssl.www8.hp.com/ww/en/secure/pdf/4aa5-7696enw.pdf

Slide 20

Slide 20 text

Hasan Hosgel #dfist User Expectations •  Mobile App Users are impatient •  61% app start < 4 s •  49% app start < 2 s •  Users are intolerant of issues and are quick to uninstall mobile apps •  80% three attempts or less Source: https://ssl.www8.hp.com/ww/en/secure/pdf/4aa5-7696enw.pdf

Slide 21

Slide 21 text

Hasan Hosgel #dfist User Expectations •  Mobile App Users are impatient •  61% app start < 4 s •  49% app start < 2 s •  Users are intolerant of issues and are quick to uninstall mobile apps •  80% three attempts or less •  53% apps with severe issues like crashes, freezes or errors Source: https://ssl.www8.hp.com/ww/en/secure/pdf/4aa5-7696enw.pdf

Slide 22

Slide 22 text

Hasan Hosgel #dfist User Expectations •  Mobile App Users are impatient •  61% app start < 4 s •  49% app start < 2 s •  Users are intolerant of issues and are quick to uninstall mobile apps •  80% three attempts or less •  53% apps with severe issues like crashes, freezes or errors •  36% heavy battery usage Source: https://ssl.www8.hp.com/ww/en/secure/pdf/4aa5-7696enw.pdf

Slide 23

Slide 23 text

Hasan Hosgel #dfist User Expectations •  Users blame the app and the company who made it Source: https://ssl.www8.hp.com/ww/en/secure/pdf/4aa5-7696enw.pdf

Slide 24

Slide 24 text

Hasan Hosgel #dfist User Expectations •  Users blame the app and the company who made it •  55% app is responsible for performance issues Source: https://ssl.www8.hp.com/ww/en/secure/pdf/4aa5-7696enw.pdf

Slide 25

Slide 25 text

Hasan Hosgel #dfist User Expectations •  Users blame the app and the company who made it •  55% app is responsible for performance issues •  37% Stated that app crashes or errors make them think less of a company’s brand Source: https://ssl.www8.hp.com/ww/en/secure/pdf/4aa5-7696enw.pdf

Slide 26

Slide 26 text

Hasan Hosgel #dfist Performance Impacts Source: http://www.mobilejoomla.com/media/press/responsive-vs-serverside/Responsive-Design-vs-Server-Side-Solutions-Infographic.jpg https://www.flickr.com/photos/9009139@N08/1263954439

Slide 27

Slide 27 text

Hasan Hosgel #dfist Performance Impacts •  500 ms delay Source: http://www.mobilejoomla.com/media/press/responsive-vs-serverside/Responsive-Design-vs-Server-Side-Solutions-Infographic.jpg https://www.flickr.com/photos/9009139@N08/1263954439

Slide 28

Slide 28 text

Hasan Hosgel #dfist Performance Impacts •  500 ms delay •  bounce rate -4.7% -- conversion rate -1.9% Source: http://www.mobilejoomla.com/media/press/responsive-vs-serverside/Responsive-Design-vs-Server-Side-Solutions-Infographic.jpg https://www.flickr.com/photos/9009139@N08/1263954439

Slide 29

Slide 29 text

Hasan Hosgel #dfist Performance Impacts •  500 ms delay •  bounce rate -4.7% -- conversion rate -1.9% •  1,000 ms delay Source: http://www.mobilejoomla.com/media/press/responsive-vs-serverside/Responsive-Design-vs-Server-Side-Solutions-Infographic.jpg https://www.flickr.com/photos/9009139@N08/1263954439

Slide 30

Slide 30 text

Hasan Hosgel #dfist Performance Impacts •  500 ms delay •  bounce rate -4.7% -- conversion rate -1.9% •  1,000 ms delay •  bounce rate -8.3% -- Conversion rate -3.5% Source: http://www.mobilejoomla.com/media/press/responsive-vs-serverside/Responsive-Design-vs-Server-Side-Solutions-Infographic.jpg https://www.flickr.com/photos/9009139@N08/1263954439

Slide 31

Slide 31 text

Hasan Hosgel #dfist Performance Impacts •  500 ms delay •  bounce rate -4.7% -- conversion rate -1.9% •  1,000 ms delay •  bounce rate -8.3% -- Conversion rate -3.5% •  100ms delay mean for Amazon -1% revenue Source: http://www.mobilejoomla.com/media/press/responsive-vs-serverside/Responsive-Design-vs-Server-Side-Solutions-Infographic.jpg https://www.flickr.com/photos/9009139@N08/1263954439

Slide 32

Slide 32 text

Hasan Hosgel #dfist Performance Impacts •  500 ms delay •  bounce rate -4.7% -- conversion rate -1.9% •  1,000 ms delay •  bounce rate -8.3% -- Conversion rate -3.5% •  100ms delay mean for Amazon -1% revenue •  11% scream at there device Source: http://www.mobilejoomla.com/media/press/responsive-vs-serverside/Responsive-Design-vs-Server-Side-Solutions-Infographic.jpg https://www.flickr.com/photos/9009139@N08/1263954439

Slide 33

Slide 33 text

Hasan Hosgel #dfist Performance Impacts •  500 ms delay •  bounce rate -4.7% -- conversion rate -1.9% •  1,000 ms delay •  bounce rate -8.3% -- Conversion rate -3.5% •  100ms delay mean for Amazon -1% revenue •  11% scream at there device •  4 % throw their phones Source: http://www.mobilejoomla.com/media/press/responsive-vs-serverside/Responsive-Design-vs-Server-Side-Solutions-Infographic.jpg https://www.flickr.com/photos/9009139@N08/1263954439

Slide 34

Slide 34 text

Hasan Hosgel #dfist

Slide 35

Slide 35 text

Let‘s find some performance improvements

Slide 36

Slide 36 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O

Slide 37

Slide 37 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O

Slide 38

Slide 38 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O •  Keep the work of the main thread à ANR

Slide 39

Slide 39 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O •  Keep the work of the main thread à ANR •  Avoid GPU overdrawings

Slide 40

Slide 40 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O •  Keep the work of the main thread à ANR •  Avoid GPU overdrawings •  https://www.youtube.com/watch?v=T52v50r-JfE

Slide 41

Slide 41 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O •  Keep the work of the main thread à ANR •  Avoid GPU overdrawings •  https://www.youtube.com/watch?v=T52v50r-JfE •  Avoid nested multi-pass layouts

Slide 42

Slide 42 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O •  Keep the work of the main thread à ANR •  Avoid GPU overdrawings •  https://www.youtube.com/watch?v=T52v50r-JfE •  Avoid nested multi-pass layouts •  http://goo.gl/Q5te24

Slide 43

Slide 43 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O •  Keep the work of the main thread à ANR •  Avoid GPU overdrawings •  https://www.youtube.com/watch?v=T52v50r-JfE •  Avoid nested multi-pass layouts •  http://goo.gl/Q5te24 •  Consider RenderScript for performance critical code

Slide 44

Slide 44 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O

Slide 45

Slide 45 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O •  Do not allocate memory inside onDraw()

Slide 46

Slide 46 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O •  Do not allocate memory inside onDraw() •  Avoid Leaks

Slide 47

Slide 47 text

Hasan Hosgel #dfist LeakCanary to the Rescue https://github.com/square/leakcanary

Slide 48

Slide 48 text

Hasan Hosgel #dfist Setup LeakCanary debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1'! releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1’! build.gradle public class DFIstApplication extends Application {! @Override! public void onCreate() {! super.onCreate();! LeakCanary.install(this);! }! }! DFIstApplication.java

Slide 49

Slide 49 text

Hasan Hosgel #dfist Use Your Debug Build Normally

Slide 50

Slide 50 text

Hasan Hosgel #dfist Analyze Memory

Slide 51

Slide 51 text

Hasan Hosgel #dfist Analyze Heap Dump

Slide 52

Slide 52 text

Hasan Hosgel #dfist Analyze Heap Dump

Slide 53

Slide 53 text

Hasan Hosgel #dfist Analyze Heap Dump since AS 1.5

Slide 54

Slide 54 text

Hasan Hosgel #dfist Analyze Memory

Slide 55

Slide 55 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O •  Do not allocate memory inside onDraw() •  Avoid Leaks •  Avoid listener à EventBus

Slide 56

Slide 56 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O •  Do not allocate memory inside onDraw() •  Avoid Leaks •  Avoid listener à EventBus •  Use SparseArray* family instead of JDK Collections/ Maps

Slide 57

Slide 57 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O •  Do not allocate memory inside onDraw() •  Avoid Leaks •  Avoid listener à EventBus •  Use SparseArray* family instead of JDK Collections/ Maps •  Be aware that enums need more resource, but use them, if it will make sense for you

Slide 58

Slide 58 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O

Slide 59

Slide 59 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O •  Activate gzip compression on incoming and outgoing network traffic or FlatBuffer

Slide 60

Slide 60 text

Hasan Hosgel #dfist Analyze First Start

Slide 61

Slide 61 text

Hasan Hosgel #dfist Analyze Second Start

Slide 62

Slide 62 text

Hasan Hosgel #dfist Analyze Second Start

Slide 63

Slide 63 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O •  Activate gzip compression on incoming and outgoing network traffic or FlatBuffer •  Cache data on disk (image, http responses) with reasonable TTL

Slide 64

Slide 64 text

Hasan Hosgel #dfist Finally Second Start

Slide 65

Slide 65 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O •  Activate gzip compression on incoming and outgoing network traffic or FlatBuffer •  Cache data on disk (image, http responses) with reasonable TTL •  Server side cache headers like ETag & Last-Modified

Slide 66

Slide 66 text

Hasan Hosgel #dfist Possible improvements CPU Memory I/O •  Activate gzip compression on incoming and outgoing network traffic or FlatBuffer •  Cache data on disk (image, http responses) with reasonable TTL •  Server side cache headers like ETag & Last-Modified •  Use JobScheduler API to batch across system or better use push notifications for updateinformation

Slide 67

Slide 67 text

Hasan Hosgel #dfist Source: http://www.flickr.com/photos/16210667@N02/9172895225

Slide 68

Slide 68 text

Hasan Hosgel #dfist Optimizing ArrayList ! public List getDFIstNames(List attendees) {! ! List names = new ArrayList<>();! ! for (Attendee attendee : attendees) {! names.add(attendee.name);! }! ! return names;! }! ! JAVA

Slide 69

Slide 69 text

Hasan Hosgel #dfist Optimizing ArrayList ! public List getDFIstNames(List attendees) {! ! List names = new ArrayList<>(attendees.size());! ! for (Attendee attendee : attendees) {! names.add(attendee.name);! }! ! return names;! }! ! JAVA

Slide 70

Slide 70 text

Hasan Hosgel #dfist Optimizing ArrayList ! @Override public boolean add(E object) {! Object[] a = array;! int s = size;! if (s == a.length) {! Object[] newArray = new Object[s +! (s < (MIN_CAPACITY_INCREMENT / 2) ?! MIN_CAPACITY_INCREMENT : s >> 1)];! System.arraycopy(a, 0, newArray, 0, s);! array = a = newArray;! }! a[s] = object;! size = s + 1;! modCount++;! return true;! }! ! JAVA

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

Hasan Hosgel #dfist Optimizing ArrayList void addDFIstSpeaker(ArrayList attendees, List speakers) {! ! ! ! for (String speaker : speakers) {! attendees.add(speaker);! }! ! }! ! JAVA

Slide 73

Slide 73 text

Hasan Hosgel #dfist Optimizing ArrayList void addDFIstSpeaker(ArrayList attendees, List speakers) {! ! attendees.ensureCapacity(attendees.size() + speakers.size());! ! for (String speaker : speakers) {! attendees.add(speaker);! }! ! }! ! JAVA

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

Hasan Hosgel #dfist Optimizing Bundle public static DFIstScheduleFragment instance(int startMode) {! final DFIstScheduleFragment fragment = new DFIstScheduleFragment();! ! final Bundle bundle = new Bundle();! bundle.putInt(START_MODE, startMode);! fragment.setArguments(bundle);! ! return fragment;! }! ! JAVA

Slide 76

Slide 76 text

Hasan Hosgel #dfist Optimizing Bundle public static DFIstScheduleFragment instance(int startMode) {! final DFIstScheduleFragment fragment = new DFIstScheduleFragment();! ! final Bundle bundle = new Bundle(1);! bundle.putInt(START_MODE, startMode);! fragment.setArguments(bundle);! ! return fragment;! }! ! JAVA

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

Hasan Hosgel #dfist Optimizing StringBuilder public StringBuilder getDFIstNames(List names) {! ! StringBuilder builder = new StringBuilder();! ! for (String name : names) {! if (builder.length() > 0) {! builder.append(", ");! }! builder.append(name);! }! return builder;! }! ! JAVA

Slide 79

Slide 79 text

Hasan Hosgel #dfist Optimizing StringBuilder public StringBuilder getDFIstNames(List names) {! ! StringBuilder builder = new StringBuilder(names.size() * 13);! ! for (String name : names) {! if (builder.length() > 0) {! builder.append(", ");! }! builder.append(name);! }! return builder;! }! ! JAVA

Slide 80

Slide 80 text

Hasan Hosgel #dfist Optimizing StringBuilder final void append0(char[] chars) {! int newCount = count + chars.length;! if (newCount > value.length) {! enlargeBuffer(newCount);! }! System.arraycopy(chars, 0, value, count, chars.length);! count = newCount;! }! JAVA

Slide 81

Slide 81 text

Hasan Hosgel #dfist Optimizing StringBuilder public StringBuilder addDFIstOrganizer(StringBuilder names, List organizers) {! ! names.ensureCapacity(names.length() + organizers.size() * 13);! ! for (String organizer : organizers) {! if (names.length() > 0) {! names.append(", ");! }! names.append(organizer);! }! }! JAVA

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

Hasan Hosgel #dfist Optimizing Moar – The Loop public StringBuilder addDFIstOrganizer(StringBuilder names, List organizers) {! ! names.ensureCapacity(names.length() + organizers.size() * 13);! ! for (String organizer : organizers) {! if (names.length() > 0) {! names.append(", ");! }! names.append(organizer);! }! }! JAVA

Slide 84

Slide 84 text

Hasan Hosgel #dfist Optimizing Moar – The Loop public StringBuilder addDFIstOrganizer(StringBuilder names, List organizers) {! ! names.ensureCapacity(names.length() + organizers.size() * 13);! ! for (int i = 0; i < organizers.size(); i++) {! String organizer = organizers.get(i);! if (names.length() > 0) {! names.append(", ");! }! names.append(organizer);! }! }! JAVA

Slide 85

Slide 85 text

Hasan Hosgel #dfist Optimizing Moar – The Loop public StringBuilder addDFIstOrganizer(StringBuilder names, List organizers) {! ! names.ensureCapacity(names.length() + organizers.size() * 13);! ! for (int i = 0, organizersSize = organizers.size(); i < organizersSize; i++) {! String organizer = organizers.get(i);! if (names.length() > 0) {! names.append(", ");! }! names.append(organizer);! }! }! JAVA

Slide 86

Slide 86 text

Hasan Hosgel #dfist Optimizing Moar – The Loop public StringBuilder addDFIstOrganizer(StringBuilder names, List organizers) {! ! final int size = organizers.size();! names.ensureCapacity(names.length() + size * 13);! ! for (int i = 0; i < size; i++) {! String organizer = organizers.get(i);! if (names.length() > 0) {! names.append(", ");! }! names.append(organizer);! }! }! JAVA

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

Hasan Hosgel #dfist Optimizing Much Moar – Method Invocation public void onViewCreated(View view, Bundle savedInstanceState) {! super.onViewCreated(view, savedInstanceState);! ! ! ! text.setTextColor(attendee.isOrganizer() ?! getResources().getColor(R.color.accent) :! getResources().getColor(R.color.black));! avatar.setImageResource(attendee.getAvatar());! welcomeText.setText(getString(! R.string.hello_world, attendee.name));! }! JAVA

Slide 89

Slide 89 text

Hasan Hosgel #dfist Optimizing Much Moar – Method Invocation public void onViewCreated(View view, Bundle savedInstanceState) {! super.onViewCreated(view, savedInstanceState);! ! final Resources res = getResources();! ! text.setTextColor(attendee.isOrganizer() ?! res.getColor(R.color.accent) :! res.getColor(R.color.black));! avatar.setImageResource(attendee.getAvatar());! welcomeText.setText(getString(! R.string.hello_world, attendee.name));! }! JAVA

Slide 90

Slide 90 text

Hasan Hosgel #dfist Optimizing Much Moar – Method Invocation public void onViewCreated(View view, Bundle savedInstanceState) {! super.onViewCreated(view, savedInstanceState);! ! final Resources res = getResources();! ! text.setTextColor(attendee.isOrganizer() ?! res.getColor(R.color.accent) :! res.getColor(R.color.black));! avatar.setImageResource(attendee.getAvatar());! welcomeText.setText(res.getString(! R.string.hello_world, attendee.name));! }! JAVA

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

Source: https://www.flickr.com/photos/marcus_t_ward/10715983613

Slide 93

Slide 93 text

Source: http://www.bhmpics.com/success_kid-wallpapers.html

Slide 94

Slide 94 text

Source: http://www.flickr.com/photos/21496790@N06/5065834411/

Slide 95

Slide 95 text

Hasan Hosgel #dfist Contact: +HasanHosgel alosdev İlginiz için teşekkür ederim! We are hiring: http://www.kitchenstories.de/#jobs https://speakerdeck.com/alosdev/perfmatters-for-android- devfest-istanbul-2015

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

Hasan Hosgel #dfist Contact: +HasanHosgel alosdev İlginiz için teşekkür ederim! We are hiring: http://www.kitchenstories.de/#jobs https://speakerdeck.com/alosdev/perfmatters-for-android- devfest-istanbul-2015

Slide 101

Slide 101 text

Hasan Hosgel #dfist Sources •  https://ssl.www8.hp.com/ww/en/secure/pdf/4aa5-7696enw.pdf •  http://www.mobilejoomla.com/media/press/responsive-vs-serverside/Responsive- Design-vs-Server-Side-Solutions-Infographic.jpg •  https://speakerdeck.com/jakewharton/eliminating-code-overhead-square-hq-2015 •  http://periplanisi.com/android/2013/11/multi-pass-viewgroup-and-performance/ •  https://slideshare.net/dougsillars/android-app-performance-europe-2015 •  https://www.youtube.com/playlist?list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE