performance

18
Yonatan Levin Android Academy

Upload: yonatan-levin

Post on 16-Apr-2017

165 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Performance

Yonatan Levin

Android Academy

Page 2: Performance

How to create great apps

that also working well

Page 3: Performance

OutOfMemory - Bitmap memory usageImage 1080X1920pixels, 124KB .png compressed Bitmap size = Width * Height * depth(4 bytes) = 8 MB!!!!

Reduce the size before loading it to the memory.BitmapFactory.Options options = new BitmapFactory.Options();

options.inJustDecodeBounds = true;

BitmapFactory.decodeResource(getResources(), R.id.myimage, options);

int imageHeight = options.outHeight;

int imageWidth = options.outWidth;

String imageType = options.outMimeType;

Page 4: Performance

OutOfMemory - Garbage collectorA large number of small allocations can also cause heap fragmentation

List<Object> mTempObjects = new ArrayList<Object>(); for(int i=0; i<10000; i++){ mTempObjects.add(new Object());}

Page 5: Performance

OutOfMemory - Garbage collector for(int i=0; i<mTempObjects.size(); i++){ Object c = mTempObjects.get(i); Log.d(TAG, "Object data:" + c.getValue()); }

10,000 references waiting to be collected by GC

Page 6: Performance

OutOfMemory - Garbage collector“To improve is to change; to be perfect is to change often.” – Winston Churchill

Object c; for(int i=0; i<mTempObjects.size(); i++){ c = mTempObjects.get(i); Log.d(TAG, "Object data:" + c.getValue());}

Page 7: Performance

OutOfMemory - Garbage collectorReuse of the same objectprivate StringBuilder buildSomething(StringBuilder sb){ sb.append(readString(fileStream)); sb.append(readOrderDetails(AnotheFileStream)); return sb; }No short term temporary object for String

Page 8: Performance

Memory overheadObject with just one int take 16 bytes at minimum

Class Integer {private int value;

}Object overhead (8 bytes ) + Overhead of dlmalloc(8 bytes) + data (n bytes) = >16 bytes

Page 9: Performance

Memory overheadclass HashMap$HashMapEntry<K,V> {

K key;V value;int hash;HashMapEntry<K,V> next;

}Total: 8 bytes (Object) + 8(dlmalloc) + 4*4(members) = 32 bytes

Page 10: Performance

Primitive Type vs ObjectInteger (16 bytes) vs int(4 bytes)Boolean(16 bytes) vs boolean(4 bytes)

or bit-field(1 bit) //even

better!

Page 11: Performance

Enums vs IntsVery simple - don’t use Enumspublic final static enum Things {

THING_1,THING_2,

}; == + 1,112 bytes

Page 12: Performance

Enums vs IntsUse final static variable:public static int THING_1 = 1;public static int THING_2 = 2;------------------------128 bytes

Page 13: Performance

Discover by yourselfimport java.lang.instrument.Instrumentation;

public class ObjectSizeFetcher {

private static Instrumentation instrumentation;

public static void premain(String args, Instrumentation inst) { instrumentation = inst; }

public static long getObjectSize(Object o) { return instrumentation.getObjectSize(o); } }

Use getObjectSize:

public class C { private int x; private int y;

public static void main(String [] args) {

System.out.println(ObjectSizeFetcher.getObjectSize(new C())); } }

Page 14: Performance

Anonymous and Inner Classes~500 bytes of codebutton.setOnClickListener(new Runnable() {

public void run(){//do some stuff

}});unregister as soon as possible

Page 15: Performance

Service- Very usefull, but overused.- Bind to Lifecycle

- Stop it when no need.- Use IntentService instead (will stop when job is

done)

Page 16: Performance

- Release memory when your activity no longer visible

- Don’t hold direct references. Use WeakReference

- Stop all your handlers, threads in onPause/onStop

- Configuration changes

Memory Leak

Page 17: Performance

ProguardTool to shrink, optimize and obfuscate the code.

Very usefull, hard to configure.

Page 18: Performance

[email protected]://plus.google.com/+YonatanLevin