SlideShare a Scribd company logo
1 of 43
Download to read offline
Memory Management for Android Apps
Patrick Dubroy (dubroy.com · @dubroy)
May 11, 2011
3
192MB RAM
4
1GB RAM
5
G1
320x480
Xoom
1280x800
6
7
Work expands
to fill the time available.
Software
memory
Overview
• Changes in Gingerbread and Honeycomb
– heap size
– GC
– bitmaps
• Understanding heap usage
– logs
– memory leaks
– Eclipse Memory Analyzer (MAT)
8
Expectations
• Android
• Dalvik heap
• Garbage collection
• OutOfMemoryError
9
Heap Size
• Heap size limits
– G1: 16MB
– Droid: 24MB
– Nexus One: 32MB
– Xoom: 48MB
• ActivityManager.getMemoryClass()
10
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
Garbage Collection
12
GC Roots
Garbage Collection
13
GC Roots
Garbage Collection
14
GC Roots
Garbage Collection
15
GC Roots
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
Bitmaps
Old way (pre-Honeycomb):
–freed via recycle() or finalizer
–hard to debug
–full, stop-the-world GCs
17
Managed
Native
Bitmaps
Old 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
Managed
Native
Overview
• Changes in Gingerbread and Honeycomb
– heap size
– GC
– bitmaps
• Understanding heap usage
– logs
– memory leaks
– Eclipse Memory Analyzer (MAT)
19
Overview
• Changes in Gingerbread and Honeycomb
– heap size
– GC
– bitmaps
• Understanding heap usage
– logs
– memory leaks
– Eclipse Memory Analyzer (MAT)
20
D/dalvikvm( 9050): GC_CONCURRENT freed 2049K, 65% free 3571K/
9991K, external 4703K/5261K, paused 2ms+2ms
Interpreting Log Messages
21
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
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
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
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
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
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
Memory Leaks
• GC does not prevent leaks!
• Leak: ref to an unused object preventing GC
• References to Activity (Context)
– View, Drawable, ...
28
Memory Leaks
29
Activity
ViewGroup
Views
Eclipse Memory Analyzer (MAT)
• Download from http://eclipse.org/mat/
• “Shallow heap” and “retained heap”
30
100
100
100 100
Eclipse Memory Analyzer (MAT)
• Download from http://eclipse.org/mat/
• “Shallow heap” and “retained heap”
31
100
100
100 100
R = 100
Eclipse Memory Analyzer (MAT)
• Download from http://eclipse.org/mat/
• “Shallow heap” and “retained heap”
32
100
100 100
100
R = 100
Eclipse Memory Analyzer (MAT)
• Download from http://eclipse.org/mat/
• “Shallow heap” and “retained heap”
33
100
100
100 100
R = 400
Dominator Tree
• Dominator: closest object on every path to node
34
A
B
C E
D
A
B C D
E
Demo: Debugging a memory leak with MAT
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
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
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
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
40
MainActivity
Leaky
MainActivity
class
Demo: Debugging a memory leak with MAT
Memory Leaks
• References to Activity, Context, View, Drawable, ...
• Non-static inner classes (e.g. Runnable)
• Caches
42
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

More Related Content

What's hot

Introduction to Garbage Collection
Introduction to Garbage CollectionIntroduction to Garbage Collection
Introduction to Garbage CollectionArtur Mkrtchyan
 
Garbage collection in JVM
Garbage collection in JVMGarbage collection in JVM
Garbage collection in JVMaragozin
 
Performance tuning jvm
Performance tuning jvmPerformance tuning jvm
Performance tuning jvmPrem Kuppumani
 
Understanding Java Garbage Collection
Understanding Java Garbage CollectionUnderstanding Java Garbage Collection
Understanding Java Garbage CollectionAzul Systems Inc.
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it worksMindfire Solutions
 
[Vietnam Mobile Day 2013] - Memory management for android applications
[Vietnam Mobile Day 2013] - Memory management for android applications[Vietnam Mobile Day 2013] - Memory management for android applications
[Vietnam Mobile Day 2013] - Memory management for android applicationsAiTi Education
 
Vietnam Mobile Day 2013: Memory Management For Android Apps
Vietnam Mobile Day 2013: Memory Management For Android AppsVietnam Mobile Day 2013: Memory Management For Android Apps
Vietnam Mobile Day 2013: Memory Management For Android AppsGameLandVN
 
Introduction of Java GC Tuning and Java Java Mission Control
Introduction of Java GC Tuning and Java Java Mission ControlIntroduction of Java GC Tuning and Java Java Mission Control
Introduction of Java GC Tuning and Java Java Mission ControlLeon Chen
 
GC in Ruby. RubyC, Kiev, 2014.
GC in Ruby. RubyC, Kiev, 2014.GC in Ruby. RubyC, Kiev, 2014.
GC in Ruby. RubyC, Kiev, 2014.Timothy Tsvetkov
 
Java profiling Do It Yourself
Java profiling Do It YourselfJava profiling Do It Yourself
Java profiling Do It Yourselfaragozin
 
Tuning Java for Big Data
Tuning Java for Big DataTuning Java for Big Data
Tuning Java for Big DataScott Seighman
 
JVM and Garbage Collection Tuning
JVM and Garbage Collection TuningJVM and Garbage Collection Tuning
JVM and Garbage Collection TuningKai Koenig
 
Tuning Java GC to resolve performance issues
Tuning Java GC to resolve performance issuesTuning Java GC to resolve performance issues
Tuning Java GC to resolve performance issuesSergey Podolsky
 
GC Tuning in the HotSpot Java VM - a FISL 10 Presentation
GC Tuning in the HotSpot Java VM - a FISL 10 PresentationGC Tuning in the HotSpot Java VM - a FISL 10 Presentation
GC Tuning in the HotSpot Java VM - a FISL 10 PresentationLudovic Poitou
 
淺談 Java GC 原理、調教和 新發展
淺談 Java GC 原理、調教和新發展淺談 Java GC 原理、調教和新發展
淺談 Java GC 原理、調教和 新發展Leon Chen
 

What's hot (19)

Garbage First & You
Garbage First & YouGarbage First & You
Garbage First & You
 
Introduction to Garbage Collection
Introduction to Garbage CollectionIntroduction to Garbage Collection
Introduction to Garbage Collection
 
Garbage collection in JVM
Garbage collection in JVMGarbage collection in JVM
Garbage collection in JVM
 
Performance tuning jvm
Performance tuning jvmPerformance tuning jvm
Performance tuning jvm
 
Understanding Java Garbage Collection
Understanding Java Garbage CollectionUnderstanding Java Garbage Collection
Understanding Java Garbage Collection
 
Basics of JVM Tuning
Basics of JVM TuningBasics of JVM Tuning
Basics of JVM Tuning
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it works
 
[Vietnam Mobile Day 2013] - Memory management for android applications
[Vietnam Mobile Day 2013] - Memory management for android applications[Vietnam Mobile Day 2013] - Memory management for android applications
[Vietnam Mobile Day 2013] - Memory management for android applications
 
Vietnam Mobile Day 2013: Memory Management For Android Apps
Vietnam Mobile Day 2013: Memory Management For Android AppsVietnam Mobile Day 2013: Memory Management For Android Apps
Vietnam Mobile Day 2013: Memory Management For Android Apps
 
Introduction of Java GC Tuning and Java Java Mission Control
Introduction of Java GC Tuning and Java Java Mission ControlIntroduction of Java GC Tuning and Java Java Mission Control
Introduction of Java GC Tuning and Java Java Mission Control
 
GC in Ruby. RubyC, Kiev, 2014.
GC in Ruby. RubyC, Kiev, 2014.GC in Ruby. RubyC, Kiev, 2014.
GC in Ruby. RubyC, Kiev, 2014.
 
Java profiling Do It Yourself
Java profiling Do It YourselfJava profiling Do It Yourself
Java profiling Do It Yourself
 
Tuning Java for Big Data
Tuning Java for Big DataTuning Java for Big Data
Tuning Java for Big Data
 
JVM and Garbage Collection Tuning
JVM and Garbage Collection TuningJVM and Garbage Collection Tuning
JVM and Garbage Collection Tuning
 
Tuning Java GC to resolve performance issues
Tuning Java GC to resolve performance issuesTuning Java GC to resolve performance issues
Tuning Java GC to resolve performance issues
 
GC Tuning in the HotSpot Java VM - a FISL 10 Presentation
GC Tuning in the HotSpot Java VM - a FISL 10 PresentationGC Tuning in the HotSpot Java VM - a FISL 10 Presentation
GC Tuning in the HotSpot Java VM - a FISL 10 Presentation
 
Java GC, Off-heap workshop
Java GC, Off-heap workshopJava GC, Off-heap workshop
Java GC, Off-heap workshop
 
Memory management
Memory managementMemory management
Memory management
 
淺談 Java GC 原理、調教和 新發展
淺談 Java GC 原理、調教和新發展淺談 Java GC 原理、調教和新發展
淺談 Java GC 原理、調教和 新發展
 

Viewers also liked

lightning talk : Android memory leak
lightning talk :  Android memory leaklightning talk :  Android memory leak
lightning talk : Android memory leakDeivison Sporteman
 
Android - Preventing common memory leaks
Android - Preventing common memory leaksAndroid - Preventing common memory leaks
Android - Preventing common memory leaksAli Muzaffar
 
Memory management in Andoid
Memory management in AndoidMemory management in Andoid
Memory management in AndoidMonkop Inc
 
Identifying memory leaks in Android applications
Identifying memory leaks in Android applicationsIdentifying memory leaks in Android applications
Identifying memory leaks in Android applicationsZachary Blair
 
Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android ApplicationsLokesh Ponnada
 
Memory management in Android
Memory management in AndroidMemory management in Android
Memory management in AndroidKeyhan Asghari
 
Android memory fundamentals
Android memory fundamentalsAndroid memory fundamentals
Android memory fundamentalsTaras Leskiv
 
Forensic Memory Analysis of Android's Dalvik Virtual Machine
Forensic Memory Analysis of Android's Dalvik Virtual MachineForensic Memory Analysis of Android's Dalvik Virtual Machine
Forensic Memory Analysis of Android's Dalvik Virtual MachineSource Conference
 
Memory Analysis of the Dalvik (Android) Virtual Machine
Memory Analysis of the Dalvik (Android) Virtual MachineMemory Analysis of the Dalvik (Android) Virtual Machine
Memory Analysis of the Dalvik (Android) Virtual MachineAndrew Case
 
Detect all memory leaks with LeakCanary!
 Detect all memory leaks with LeakCanary! Detect all memory leaks with LeakCanary!
Detect all memory leaks with LeakCanary!Pierre-Yves Ricau
 
Memory Management in Android
Memory Management in AndroidMemory Management in Android
Memory Management in AndroidOpersys inc.
 
LCA13: Memory Hotplug on Android
LCA13: Memory Hotplug on AndroidLCA13: Memory Hotplug on Android
LCA13: Memory Hotplug on AndroidLinaro
 
AsyncTask アンチパターン
AsyncTask アンチパターンAsyncTask アンチパターン
AsyncTask アンチパターンHiroshi Kurokawa
 
2015.04.24 Updated > Android Security Development - Part 1: App Development
2015.04.24 Updated > Android Security Development - Part 1: App Development 2015.04.24 Updated > Android Security Development - Part 1: App Development
2015.04.24 Updated > Android Security Development - Part 1: App Development Cheng-Yi Yu
 
Leak canaryで メモリリーク調査
Leak canaryで メモリリーク調査Leak canaryで メモリリーク調査
Leak canaryで メモリリーク調査baroqueworksdev
 
Android Performance Best Practices
Android Performance Best Practices Android Performance Best Practices
Android Performance Best Practices Amgad Muhammad
 
Os Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual MemoryOs Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual Memorysgpraju
 

Viewers also liked (19)

lightning talk : Android memory leak
lightning talk :  Android memory leaklightning talk :  Android memory leak
lightning talk : Android memory leak
 
Android - Preventing common memory leaks
Android - Preventing common memory leaksAndroid - Preventing common memory leaks
Android - Preventing common memory leaks
 
Memory management in Andoid
Memory management in AndoidMemory management in Andoid
Memory management in Andoid
 
Identifying memory leaks in Android applications
Identifying memory leaks in Android applicationsIdentifying memory leaks in Android applications
Identifying memory leaks in Android applications
 
Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android Applications
 
Memory management in Android
Memory management in AndroidMemory management in Android
Memory management in Android
 
Memory leak
Memory leakMemory leak
Memory leak
 
Android memory fundamentals
Android memory fundamentalsAndroid memory fundamentals
Android memory fundamentals
 
Forensic Memory Analysis of Android's Dalvik Virtual Machine
Forensic Memory Analysis of Android's Dalvik Virtual MachineForensic Memory Analysis of Android's Dalvik Virtual Machine
Forensic Memory Analysis of Android's Dalvik Virtual Machine
 
Memory Analysis of the Dalvik (Android) Virtual Machine
Memory Analysis of the Dalvik (Android) Virtual MachineMemory Analysis of the Dalvik (Android) Virtual Machine
Memory Analysis of the Dalvik (Android) Virtual Machine
 
Detect all memory leaks with LeakCanary!
 Detect all memory leaks with LeakCanary! Detect all memory leaks with LeakCanary!
Detect all memory leaks with LeakCanary!
 
Memory Management in Android
Memory Management in AndroidMemory Management in Android
Memory Management in Android
 
LCA13: Memory Hotplug on Android
LCA13: Memory Hotplug on AndroidLCA13: Memory Hotplug on Android
LCA13: Memory Hotplug on Android
 
AsyncTask アンチパターン
AsyncTask アンチパターンAsyncTask アンチパターン
AsyncTask アンチパターン
 
2015.04.24 Updated > Android Security Development - Part 1: App Development
2015.04.24 Updated > Android Security Development - Part 1: App Development 2015.04.24 Updated > Android Security Development - Part 1: App Development
2015.04.24 Updated > Android Security Development - Part 1: App Development
 
Leak canaryで メモリリーク調査
Leak canaryで メモリリーク調査Leak canaryで メモリリーク調査
Leak canaryで メモリリーク調査
 
Android Performance Best Practices
Android Performance Best Practices Android Performance Best Practices
Android Performance Best Practices
 
Os Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual MemoryOs Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual Memory
 
Operating Systems
Operating SystemsOperating Systems
Operating Systems
 

Similar to Memory management for_android_apps

Eclipse Memory Analyzer Tool
Eclipse Memory Analyzer ToolEclipse Memory Analyzer Tool
Eclipse Memory Analyzer Toollittleeye
 
Performance #1: Memory
Performance #1: MemoryPerformance #1: Memory
Performance #1: MemoryYonatan Levin
 
Adobe AEM Maintenance - Customer Care Office Hours
Adobe AEM Maintenance - Customer Care Office HoursAdobe AEM Maintenance - Customer Care Office Hours
Adobe AEM Maintenance - Customer Care Office HoursAndrew Khoury
 
Introduction to Big Data
Introduction to Big DataIntroduction to Big Data
Introduction to Big DataAlbert Bifet
 
Dissecting Real-World Database Performance Dilemmas
Dissecting Real-World Database Performance DilemmasDissecting Real-World Database Performance Dilemmas
Dissecting Real-World Database Performance DilemmasScyllaDB
 
Memory Usage in Android
Memory Usage in AndroidMemory Usage in Android
Memory Usage in AndroidPrashanth Nani
 
Alexander Dymo - RailsConf 2014 - Improve performance: Optimize Memory and Up...
Alexander Dymo - RailsConf 2014 - Improve performance: Optimize Memory and Up...Alexander Dymo - RailsConf 2014 - Improve performance: Optimize Memory and Up...
Alexander Dymo - RailsConf 2014 - Improve performance: Optimize Memory and Up...Alexander Dymo
 
Improving Apache Spark Downscaling
 Improving Apache Spark Downscaling Improving Apache Spark Downscaling
Improving Apache Spark DownscalingDatabricks
 
State of Java Elasticity. Tuning Java Efficiency - GIDS.JAVA LIVE 2020
State of Java Elasticity. Tuning Java Efficiency - GIDS.JAVA LIVE 2020State of Java Elasticity. Tuning Java Efficiency - GIDS.JAVA LIVE 2020
State of Java Elasticity. Tuning Java Efficiency - GIDS.JAVA LIVE 2020Jelastic Multi-Cloud PaaS
 
DECK36 - Log everything! and Realtime Datastream Analytics with Storm
DECK36 - Log everything! and Realtime Datastream Analytics with StormDECK36 - Log everything! and Realtime Datastream Analytics with Storm
DECK36 - Log everything! and Realtime Datastream Analytics with StormMike Lohmann
 
CICS Memory Objects and MEMLIMIT
 CICS Memory Objects and MEMLIMIT CICS Memory Objects and MEMLIMIT
CICS Memory Objects and MEMLIMITDavid Clancy
 
Faceting Optimizations for Solr: Presented by Toke Eskildsen, State & Univers...
Faceting Optimizations for Solr: Presented by Toke Eskildsen, State & Univers...Faceting Optimizations for Solr: Presented by Toke Eskildsen, State & Univers...
Faceting Optimizations for Solr: Presented by Toke Eskildsen, State & Univers...Lucidworks
 
It's always sunny with OpenJ9
It's always sunny with OpenJ9It's always sunny with OpenJ9
It's always sunny with OpenJ9DanHeidinga
 
Think Distributed: The Hazelcast Way
Think Distributed: The Hazelcast WayThink Distributed: The Hazelcast Way
Think Distributed: The Hazelcast WayRahul Gupta
 
Eric Lafortune - The Jack and Jill build system
Eric Lafortune - The Jack and Jill build systemEric Lafortune - The Jack and Jill build system
Eric Lafortune - The Jack and Jill build systemGuardSquare
 
運用CNTK 實作深度學習物件辨識 Deep Learning based Object Detection with Microsoft Cogniti...
運用CNTK 實作深度學習物件辨識 Deep Learning based Object Detection with Microsoft Cogniti...運用CNTK 實作深度學習物件辨識 Deep Learning based Object Detection with Microsoft Cogniti...
運用CNTK 實作深度學習物件辨識 Deep Learning based Object Detection with Microsoft Cogniti...Herman Wu
 
Am I reading GC logs Correctly?
Am I reading GC logs Correctly?Am I reading GC logs Correctly?
Am I reading GC logs Correctly?Tier1 App
 
Kandroid for nhn_deview_20131013_v5_final
Kandroid for nhn_deview_20131013_v5_finalKandroid for nhn_deview_20131013_v5_final
Kandroid for nhn_deview_20131013_v5_finalNAVER D2
 

Similar to Memory management for_android_apps (20)

Performance #1 memory
Performance #1   memoryPerformance #1   memory
Performance #1 memory
 
Eclipse Memory Analyzer Tool
Eclipse Memory Analyzer ToolEclipse Memory Analyzer Tool
Eclipse Memory Analyzer Tool
 
Performance #1: Memory
Performance #1: MemoryPerformance #1: Memory
Performance #1: Memory
 
Adobe AEM Maintenance - Customer Care Office Hours
Adobe AEM Maintenance - Customer Care Office HoursAdobe AEM Maintenance - Customer Care Office Hours
Adobe AEM Maintenance - Customer Care Office Hours
 
Introduction to Big Data
Introduction to Big DataIntroduction to Big Data
Introduction to Big Data
 
Dissecting Real-World Database Performance Dilemmas
Dissecting Real-World Database Performance DilemmasDissecting Real-World Database Performance Dilemmas
Dissecting Real-World Database Performance Dilemmas
 
Memory Usage in Android
Memory Usage in AndroidMemory Usage in Android
Memory Usage in Android
 
Alexander Dymo - RailsConf 2014 - Improve performance: Optimize Memory and Up...
Alexander Dymo - RailsConf 2014 - Improve performance: Optimize Memory and Up...Alexander Dymo - RailsConf 2014 - Improve performance: Optimize Memory and Up...
Alexander Dymo - RailsConf 2014 - Improve performance: Optimize Memory and Up...
 
Improving Apache Spark Downscaling
 Improving Apache Spark Downscaling Improving Apache Spark Downscaling
Improving Apache Spark Downscaling
 
A G1GC Saga-KCJUG.pptx
A G1GC Saga-KCJUG.pptxA G1GC Saga-KCJUG.pptx
A G1GC Saga-KCJUG.pptx
 
State of Java Elasticity. Tuning Java Efficiency - GIDS.JAVA LIVE 2020
State of Java Elasticity. Tuning Java Efficiency - GIDS.JAVA LIVE 2020State of Java Elasticity. Tuning Java Efficiency - GIDS.JAVA LIVE 2020
State of Java Elasticity. Tuning Java Efficiency - GIDS.JAVA LIVE 2020
 
DECK36 - Log everything! and Realtime Datastream Analytics with Storm
DECK36 - Log everything! and Realtime Datastream Analytics with StormDECK36 - Log everything! and Realtime Datastream Analytics with Storm
DECK36 - Log everything! and Realtime Datastream Analytics with Storm
 
CICS Memory Objects and MEMLIMIT
 CICS Memory Objects and MEMLIMIT CICS Memory Objects and MEMLIMIT
CICS Memory Objects and MEMLIMIT
 
Faceting Optimizations for Solr: Presented by Toke Eskildsen, State & Univers...
Faceting Optimizations for Solr: Presented by Toke Eskildsen, State & Univers...Faceting Optimizations for Solr: Presented by Toke Eskildsen, State & Univers...
Faceting Optimizations for Solr: Presented by Toke Eskildsen, State & Univers...
 
It's always sunny with OpenJ9
It's always sunny with OpenJ9It's always sunny with OpenJ9
It's always sunny with OpenJ9
 
Think Distributed: The Hazelcast Way
Think Distributed: The Hazelcast WayThink Distributed: The Hazelcast Way
Think Distributed: The Hazelcast Way
 
Eric Lafortune - The Jack and Jill build system
Eric Lafortune - The Jack and Jill build systemEric Lafortune - The Jack and Jill build system
Eric Lafortune - The Jack and Jill build system
 
運用CNTK 實作深度學習物件辨識 Deep Learning based Object Detection with Microsoft Cogniti...
運用CNTK 實作深度學習物件辨識 Deep Learning based Object Detection with Microsoft Cogniti...運用CNTK 實作深度學習物件辨識 Deep Learning based Object Detection with Microsoft Cogniti...
運用CNTK 實作深度學習物件辨識 Deep Learning based Object Detection with Microsoft Cogniti...
 
Am I reading GC logs Correctly?
Am I reading GC logs Correctly?Am I reading GC logs Correctly?
Am I reading GC logs Correctly?
 
Kandroid for nhn_deview_20131013_v5_final
Kandroid for nhn_deview_20131013_v5_finalKandroid for nhn_deview_20131013_v5_final
Kandroid for nhn_deview_20131013_v5_final
 

Recently uploaded

Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 

Recently uploaded (20)

Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 

Memory management for_android_apps

  • 1.
  • 2. Memory Management for Android Apps Patrick Dubroy (dubroy.com · @dubroy) May 11, 2011
  • 6. 6
  • 7. 7 Work expands to fill the time available. Software memory
  • 8. Overview • Changes in Gingerbread and Honeycomb – heap size – GC – bitmaps • Understanding heap usage – logs – memory leaks – Eclipse Memory Analyzer (MAT) 8
  • 9. Expectations • Android • Dalvik heap • Garbage collection • OutOfMemoryError 9
  • 10. Heap Size • Heap size limits – G1: 16MB – Droid: 24MB – Nexus One: 32MB – Xoom: 48MB • ActivityManager.getMemoryClass() 10
  • 11. 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
  • 16. 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
  • 17. Bitmaps Old way (pre-Honeycomb): –freed via recycle() or finalizer –hard to debug –full, stop-the-world GCs 17 Managed Native
  • 18. Bitmaps Old 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 Managed Native
  • 19. Overview • Changes in Gingerbread and Honeycomb – heap size – GC – bitmaps • Understanding heap usage – logs – memory leaks – Eclipse Memory Analyzer (MAT) 19
  • 20. Overview • Changes in Gingerbread and Honeycomb – heap size – GC – bitmaps • Understanding heap usage – logs – memory leaks – Eclipse Memory Analyzer (MAT) 20
  • 21. D/dalvikvm( 9050): GC_CONCURRENT freed 2049K, 65% free 3571K/ 9991K, external 4703K/5261K, paused 2ms+2ms Interpreting Log Messages 21
  • 22. 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
  • 23. 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
  • 24. 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
  • 25. 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
  • 26. 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
  • 27. 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
  • 28. Memory Leaks • GC does not prevent leaks! • Leak: ref to an unused object preventing GC • References to Activity (Context) – View, Drawable, ... 28
  • 30. Eclipse Memory Analyzer (MAT) • Download from http://eclipse.org/mat/ • “Shallow heap” and “retained heap” 30 100 100 100 100
  • 31. Eclipse Memory Analyzer (MAT) • Download from http://eclipse.org/mat/ • “Shallow heap” and “retained heap” 31 100 100 100 100 R = 100
  • 32. Eclipse Memory Analyzer (MAT) • Download from http://eclipse.org/mat/ • “Shallow heap” and “retained heap” 32 100 100 100 100 R = 100
  • 33. Eclipse Memory Analyzer (MAT) • Download from http://eclipse.org/mat/ • “Shallow heap” and “retained heap” 33 100 100 100 100 R = 400
  • 34. Dominator Tree • Dominator: closest object on every path to node 34 A B C E D A B C D E
  • 35. Demo: Debugging a memory leak with MAT
  • 36. 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
  • 37. 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
  • 38. 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
  • 39. 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
  • 41. Demo: Debugging a memory leak with MAT
  • 42. Memory Leaks • References to Activity, Context, View, Drawable, ... • Non-static inner classes (e.g. Runnable) • Caches 42
  • 43. 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