43

Memory Management for Android Apps

  • View
    25

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Memory Management for Android Apps
Page 2: Memory Management for Android Apps

Memory Management for Android AppsPatrick Dubroy (dubroy.com · @dubroy)May 11, 2011

Page 3: Memory Management for Android Apps

3

192MB RAM

Page 4: Memory Management for Android Apps

4

1GB RAM

Page 5: Memory Management for Android Apps

5

G1320x480

Xoom1280x800

Page 6: Memory Management for Android Apps

6

Page 7: Memory Management for Android Apps

7

Work expandsto fill the time available.

Software

memory

Page 8: Memory Management for Android Apps

Overview• Changes in Gingerbread and Honeycomb

– heap size– GC– bitmaps

• Understanding heap usage– logs– memory leaks– Eclipse Memory Analyzer (MAT)

8

Page 9: Memory Management for Android Apps

Expectations• Android• Dalvik heap• Garbage collection• OutOfMemoryError

9

Page 10: Memory Management for Android Apps

Heap Size• Heap size limits

– G1: 16MB– Droid: 24MB– Nexus One: 32MB– Xoom: 48MB

• ActivityManager.getMemoryClass()

10

Page 11: Memory Management for Android Apps

Large Heaps• Honeycomb adds “largeHeap” option in AndroidManifest.xml:

– Degrades performance! Use only if you understand why you need it.

<application android:name="com.example.foobar" android:largeHeap="true" ... </application>

ActivityManager.getLargeMemoryClass()

11

Page 12: Memory Management for Android Apps

Garbage Collection

12

GC Roots

Page 13: Memory Management for Android Apps

Garbage Collection

13

GC Roots

Page 14: Memory Management for Android Apps

Garbage Collection

14

GC Roots

Page 15: Memory Management for Android Apps

Garbage Collection

15

GC Roots

Page 16: Memory Management for Android Apps

Garbage Collection• Bigger heaps = longer pauses?• Pre-Gingerbread GC:

– Stop-the-world– Full heap collection– Pause times often > 100ms

• Gingerbread and beyond:– Concurrent (mostly)– Partial collections– Pause times usually < 5ms

16

Page 17: Memory Management for Android Apps

BitmapsOld way (pre-Honeycomb):– freed via recycle() or finalizer– hard to debug– full, stop-the-world GCs

17

ManagedNative

Page 18: Memory Management for Android Apps

BitmapsOld way (pre-Honeycomb):– freed via recycle() or finalizer– hard to debug– full, stop-the-world GCs

New way:– freed synchronously by GC– easier to debug– concurrent & partial GCs

18

ManagedNative

Page 19: Memory Management for Android Apps

Overview• Changes in Gingerbread and Honeycomb

– heap size– GC– bitmaps

• Understanding heap usage– logs– memory leaks– Eclipse Memory Analyzer (MAT)

19

Page 20: Memory Management for Android Apps

Overview• Changes in Gingerbread and Honeycomb

– heap size– GC– bitmaps

• Understanding heap usage– logs– memory leaks– Eclipse Memory Analyzer (MAT)

20

Page 21: Memory Management for Android Apps

D/dalvikvm( 9050): GC_CONCURRENT freed 2049K, 65% free 3571K/9991K, external 4703K/5261K, paused 2ms+2ms

Interpreting Log Messages

21

Page 22: Memory Management for Android Apps

Interpreting Log Messages

D/dalvikvm( 9050): GC_CONCURRENT freed 2049K, 65% free 3571K/9991K, external 4703K/5261K, paused 2ms+2ms

• Reason for GC– GC_CONCURRENT– GC_FOR_MALLOC– GC_EXTERNAL_ALLOC– GC_HPROF_DUMP_HEAP– GC_EXPLICIT

22

Page 23: Memory Management for Android Apps

Interpreting Log Messages

D/dalvikvm( 9050): GC_CONCURRENT freed 2049K, 65% free 3571K/9991K, external 4703K/5261K, paused 2ms+2ms

• Reason for GC• Amount freed

23

Page 24: Memory Management for Android Apps

Interpreting Log Messages

D/dalvikvm( 9050): GC_CONCURRENT freed 2049K, 65% free 3571K/9991K, external 4703K/5261K, paused 2ms+2ms

• Reason for GC• Amount freed• Heap statistics

24

Page 25: Memory Management for Android Apps

Interpreting Log Messages

D/dalvikvm( 9050): GC_CONCURRENT freed 2049K, 65% free 3571K/9991K, external 4703K/5261K, paused 2ms+2ms

• Reason for GC• Amount freed• Heap statistics• External memory statistics

25

Page 26: Memory Management for Android Apps

Interpreting Log Messages

D/dalvikvm( 9050): GC_CONCURRENT freed 2049K, 65% free 3571K/9991K, external 4703K/5261K, paused 2ms+2ms

• Reason for GC• Amount freed• Heap statistics• External memory statistics• Pause time

26

Page 27: Memory Management for Android Apps

Heap Dumps• Binary dump of all objects• Create with:

– DDMS– android.os.Debug.dumpHprofData()

• Convert to standard HPROF format: hprof-conv orig.hprof converted.hprof

• Analyze with MAT, jhat, etc.

27

Page 28: Memory Management for Android Apps

Memory Leaks• GC does not prevent leaks!• Leak: ref to an unused object preventing GC• References to Activity (Context)

– View, Drawable, ...

28

Page 29: Memory Management for Android Apps

Memory Leaks

29

Activity

ViewGroup

Views

Page 30: Memory Management for Android Apps

Eclipse Memory Analyzer (MAT)• Download from http://eclipse.org/mat/• “Shallow heap” and “retained heap”

30

100

100

100 100

Page 31: Memory Management for Android Apps

Eclipse Memory Analyzer (MAT)• Download from http://eclipse.org/mat/• “Shallow heap” and “retained heap”

31

100

100

100 100

R = 100

Page 32: Memory Management for Android Apps

Eclipse Memory Analyzer (MAT)• Download from http://eclipse.org/mat/• “Shallow heap” and “retained heap”

32

100

100 100

100

R = 100

Page 33: Memory Management for Android Apps

Eclipse Memory Analyzer (MAT)• Download from http://eclipse.org/mat/• “Shallow heap” and “retained heap”

33

100

100

100 100

R = 400

Page 34: Memory Management for Android Apps

Dominator Tree• Dominator: closest object on every path to node

34

A

B

C E

D

A

B C D

E

Page 35: Memory Management for Android Apps

Demo: Debugging a memory leak with MAT

Page 36: Memory Management for Android Apps

public class MainActivity extends Activity implements ActionBar.TabListener {

static Leaky leak = null;

class Leaky { void doSomething() { System.out.println("Wheee!!!"); } }

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

if (leak == null) { leak = new Leaky(); } ...

36

Page 37: Memory Management for Android Apps

public class MainActivity extends Activity implements ActionBar.TabListener {

static Leaky leak = null;

class Leaky { void doSomething() { System.out.println("Wheee!!!"); } }

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

if (leak == null) { leak = new Leaky(); } ...

37

Page 38: Memory Management for Android Apps

public class MainActivity extends Activity implements ActionBar.TabListener {

static Leaky leak = null;

class Leaky { void doSomething() { System.out.println("Wheee!!!"); } }

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

if (leak == null) { leak = new Leaky(); } ...

38

Page 39: Memory Management for Android Apps

public class MainActivity extends Activity implements ActionBar.TabListener {

static Leaky leak = null;

class Leaky { void doSomething() { System.out.println("Wheee!!!"); } }

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

if (leak == null) { leak = new Leaky(); } ...

39

Page 40: Memory Management for Android Apps

40

MainActivity

Leaky

MainActivityclass

Page 41: Memory Management for Android Apps

Demo: Debugging a memory leak with MAT

Page 42: Memory Management for Android Apps

Memory Leaks• References to Activity, Context, View, Drawable, ...• Non-static inner classes (e.g. Runnable)• Caches

42

Page 43: Memory Management for Android Apps

Links• Articles on Android Developers Blog

– Memory Analysis for Android Applications– Avoiding Memory Leaks by Romain Guy

• Eclipse Memory Analyzer: http://www.eclipse.org/mat/• Markus Kohlerʼs Java Performance Blog: http://kohlerm.blogspot.com/

• Feedback on this talk:http://speakermeter.com/talks/memory-management-android

43