SlideShare a Scribd company logo
Identifying memory leaks in
Android applications
Zachary Blair
Doesn’t Java handle memory management?!!!1




               http://memegenerator.net/instance/27508171
Garbage collection !=
Immunity from memory leaks!
Java/Android Garbage Collection
• All objects created with new are stored on the heap


• GC periodically disposes of objects that are eligible for
  garbage collection


• Android GC uses a mark-and-sweep algorithm
What is “eligible for garbage collection”?
• Not reachable from any live threads or any static
  references
 − There are no references to the object!

• Cyclic references are not counted.
What is “eligible for garbage collection”?


Simple Example:



Integer volume = new Integer(100);
                                          Integer
System.out.println(volume);                 100

volume = null;
                                     Volume
What is “eligible for garbage collection”?


Simple Example:



Integer volume = new Integer(100);
                                          Integer
System.out.println(volume);                 100

volume = null;
                                     Volume
What is “eligible for garbage collection”?


Simple Example:



Integer volume = new Integer(100);
                                            Integer
System.out.println(volume);                   100

volume = null;                       null

                                     Volume
What is “eligible for garbage collection”?


Simple Example:



Integer volume = new Integer(100);           Integer
                                            Garbage
System.out.println(volume);                 collected!

volume = null;                       null

                                     Volume
What is “eligible for garbage collection”?
• Lists, Maps, and Sets are prime suspects for memory
  leaks, because they can accidentally retain references to
  unused objects!



Integer volume = new Integer(100);
                                            Integer
System.out.println(volume);                   100

volumeList.add(volume);              null

volume = null;                       Volume           volumeList
Haiku Break #1




    Memory leak, crash.
Lingering reference, why!?
      You make GC cry.
Detecting a memory leak
Clue #1: Your application crashes with an
java.lang.OutOfMemoryError after running for a long time.
Clue #2: You see frequent GC_ lines in logcat before the
crash.

  D/dalvikvm( 1325): GC_CONCURRENT freed 1971K,
  18% free 12382K/14983K, paused 3ms+7ms

                                           Heap statistics
Reason for garbage
collection:
GC_CONCURRENT
GC_FOR_MALLOC
GC_EXTERNAL_ALLOC
GC_HPROF_DUMP_HEAP
GC_EXPLICIT
Identifying the source of the leak
• Step #1
  Use DDMS (Dalvik Debug Monitor Server) to dump heap
  snapshots (an HPROF file)
 − android-sdkstoolsddms.bat
Identifying the source of the leak
• Step #2
  Use the Android SDK’s “hprof-conv” tool to convert the
  Android-specific HPROF file into a generic HPROF file
  that can be analyzed by Eclipse Memory Analyzer
Identifying the source of the leak
• Step #3
  Open the converted HPROF using Eclipse Memory
  Analyzer
Identifying the source of the leak
• Step #4
  Analyze the results using the “Histogram” view
Identifying the source of the leak
• Step #4
  Analyze the results using the “Histogram” view


• Identifies types of objects allocated!
• Doesn’t know whether they will eventually be freed!
• We must compare two HPROF snapshots to identify
  which objects are responsible for a leak.
Identifying the source of the leak
• Step #5
  Exercise your app in a way that tends to cause it to
  exhibit the memory leak, and then use DDMS to dump
  another heap snapshot


• Step #6
  Use “hprof-conv” to convert the HPROF file
Identifying the source of the leak
• Step #7
  Open both HPROF files in Eclipse Memory Analyzer and
  compare their histograms
Identifying the source of the leak
• Step #7
  Open both HPROF files in Eclipse Memory Analyzer and
  compare their histograms
 − Compare the number of instances and sizes of each type of object
   between the two snapshots.
 − A sustained increase in the number of objects may indicate a leak!
Identifying the source of the leak
• Step #7
  Open both HPROF files in Eclipse Memory Analyzer and
  compare their histograms
 − Compare the number of instances and sizes of each type of object
   between the two snapshots.
 − An unexplained increase in the number of objects may indicate a
   leak!
Identifying the source of the leak
• Step #7

Shallow heap: the size of the objects themselves.
Retained heap: the amount that would be freed if we freed
these objects (greater than shallow heap because
referenced objects may also be freed).
Haiku Break #2



         Unfreed memory,
  Delta of two HPROF dumps
      Who references thou?
Fixing the leak
• The leak must be the result of unused objects still having
  references to them so, you can do one of:
 − Stop creating the objects
 − Stop maintaining references to the objects
 − Use weak references (see WeakHashMap) so that those
   references are invalidated if they are the only ones to the object.
 − Make sure to close() open streams and connections
More Information
• Google I/O 2011: Memory management for Android
  Apps https://www.youtube.com/watch?v=_
  CruQY55HOk&feature=relmfu
Thank You

More Related Content

What's hot (20)

Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it works
Mindfire Solutions
 
Understanding Java Garbage Collection - And What You Can Do About It
Understanding Java Garbage Collection - And What You Can Do About ItUnderstanding Java Garbage Collection - And What You Can Do About It
Understanding Java Garbage Collection - And What You Can Do About It
Azul Systems Inc.
 
Detecting Memory Leaks in Android App
Detecting Memory Leaks in Android AppDetecting Memory Leaks in Android App
Detecting Memory Leaks in Android App
Dinesh Prajapati
 
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management....NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
NETFest
 
RxJS - The Reactive Extensions for JavaScript
RxJS - The Reactive Extensions for JavaScriptRxJS - The Reactive Extensions for JavaScript
RxJS - The Reactive Extensions for JavaScript
Viliam Elischer
 
JetBrains Australia 2019 - Exploring .NET’s memory management – a trip down m...
JetBrains Australia 2019 - Exploring .NET’s memory management – a trip down m...JetBrains Australia 2019 - Exploring .NET’s memory management – a trip down m...
JetBrains Australia 2019 - Exploring .NET’s memory management – a trip down m...
Maarten Balliauw
 
My Gentle Introduction to RxJS
My Gentle Introduction to RxJSMy Gentle Introduction to RxJS
My Gentle Introduction to RxJS
Mattia Occhiuto
 
Hadoop cluster performance profiler
Hadoop cluster performance profilerHadoop cluster performance profiler
Hadoop cluster performance profiler
Ihor Bobak
 
Reactive Android: RxJava and beyond
Reactive Android: RxJava and beyondReactive Android: RxJava and beyond
Reactive Android: RxJava and beyond
Fabio Tiriticco
 
Building Sexy Real-Time Analytics Systems - Erlang Factory NYC / Toronto 2013
Building Sexy Real-Time Analytics Systems - Erlang Factory NYC / Toronto 2013Building Sexy Real-Time Analytics Systems - Erlang Factory NYC / Toronto 2013
Building Sexy Real-Time Analytics Systems - Erlang Factory NYC / Toronto 2013
lpgauth
 
Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event Systems
Leonardo Borges
 
Performance Monitoring for Docker Environments - Docker Amsterdam June Meetup
Performance Monitoring for Docker Environments - Docker Amsterdam June MeetupPerformance Monitoring for Docker Environments - Docker Amsterdam June Meetup
Performance Monitoring for Docker Environments - Docker Amsterdam June Meetup
Stijn Polfliet
 
Quantifying the Performance of Garbage Collection vs. Explicit Memory Management
Quantifying the Performance of Garbage Collection vs. Explicit Memory ManagementQuantifying the Performance of Garbage Collection vs. Explicit Memory Management
Quantifying the Performance of Garbage Collection vs. Explicit Memory Management
Emery Berger
 
Real world functional reactive programming
Real world functional reactive programmingReal world functional reactive programming
Real world functional reactive programming
Eric Polerecky
 
An Introduction to RxJava
An Introduction to RxJavaAn Introduction to RxJava
An Introduction to RxJava
Sanjay Acharya
 
Introduction to c part -3
Introduction to c   part -3Introduction to c   part -3
Introduction to c part -3
baabtra.com - No. 1 supplier of quality freshers
 
RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015
Ben Lesh
 
Debugging Complex Systems - Erlang Factory SF 2015
Debugging Complex Systems - Erlang Factory SF 2015Debugging Complex Systems - Erlang Factory SF 2015
Debugging Complex Systems - Erlang Factory SF 2015
lpgauth
 
The Popper Experimentation Protocol and CLI tool
The Popper Experimentation Protocol and CLI toolThe Popper Experimentation Protocol and CLI tool
The Popper Experimentation Protocol and CLI tool
Ivo Jimenez
 
あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法
x1 ichi
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it works
Mindfire Solutions
 
Understanding Java Garbage Collection - And What You Can Do About It
Understanding Java Garbage Collection - And What You Can Do About ItUnderstanding Java Garbage Collection - And What You Can Do About It
Understanding Java Garbage Collection - And What You Can Do About It
Azul Systems Inc.
 
Detecting Memory Leaks in Android App
Detecting Memory Leaks in Android AppDetecting Memory Leaks in Android App
Detecting Memory Leaks in Android App
Dinesh Prajapati
 
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management....NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
NETFest
 
RxJS - The Reactive Extensions for JavaScript
RxJS - The Reactive Extensions for JavaScriptRxJS - The Reactive Extensions for JavaScript
RxJS - The Reactive Extensions for JavaScript
Viliam Elischer
 
JetBrains Australia 2019 - Exploring .NET’s memory management – a trip down m...
JetBrains Australia 2019 - Exploring .NET’s memory management – a trip down m...JetBrains Australia 2019 - Exploring .NET’s memory management – a trip down m...
JetBrains Australia 2019 - Exploring .NET’s memory management – a trip down m...
Maarten Balliauw
 
My Gentle Introduction to RxJS
My Gentle Introduction to RxJSMy Gentle Introduction to RxJS
My Gentle Introduction to RxJS
Mattia Occhiuto
 
Hadoop cluster performance profiler
Hadoop cluster performance profilerHadoop cluster performance profiler
Hadoop cluster performance profiler
Ihor Bobak
 
Reactive Android: RxJava and beyond
Reactive Android: RxJava and beyondReactive Android: RxJava and beyond
Reactive Android: RxJava and beyond
Fabio Tiriticco
 
Building Sexy Real-Time Analytics Systems - Erlang Factory NYC / Toronto 2013
Building Sexy Real-Time Analytics Systems - Erlang Factory NYC / Toronto 2013Building Sexy Real-Time Analytics Systems - Erlang Factory NYC / Toronto 2013
Building Sexy Real-Time Analytics Systems - Erlang Factory NYC / Toronto 2013
lpgauth
 
Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event Systems
Leonardo Borges
 
Performance Monitoring for Docker Environments - Docker Amsterdam June Meetup
Performance Monitoring for Docker Environments - Docker Amsterdam June MeetupPerformance Monitoring for Docker Environments - Docker Amsterdam June Meetup
Performance Monitoring for Docker Environments - Docker Amsterdam June Meetup
Stijn Polfliet
 
Quantifying the Performance of Garbage Collection vs. Explicit Memory Management
Quantifying the Performance of Garbage Collection vs. Explicit Memory ManagementQuantifying the Performance of Garbage Collection vs. Explicit Memory Management
Quantifying the Performance of Garbage Collection vs. Explicit Memory Management
Emery Berger
 
Real world functional reactive programming
Real world functional reactive programmingReal world functional reactive programming
Real world functional reactive programming
Eric Polerecky
 
An Introduction to RxJava
An Introduction to RxJavaAn Introduction to RxJava
An Introduction to RxJava
Sanjay Acharya
 
RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015
Ben Lesh
 
Debugging Complex Systems - Erlang Factory SF 2015
Debugging Complex Systems - Erlang Factory SF 2015Debugging Complex Systems - Erlang Factory SF 2015
Debugging Complex Systems - Erlang Factory SF 2015
lpgauth
 
The Popper Experimentation Protocol and CLI tool
The Popper Experimentation Protocol and CLI toolThe Popper Experimentation Protocol and CLI tool
The Popper Experimentation Protocol and CLI tool
Ivo Jimenez
 
あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法
x1 ichi
 

Viewers also liked (20)

Android - Preventing common memory leaks
Android - Preventing common memory leaksAndroid - Preventing common memory leaks
Android - Preventing common memory leaks
Ali Muzaffar
 
Memory leak
Memory leakMemory leak
Memory leak
Anandraj Kulkarni
 
Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android Applications
Lokesh Ponnada
 
lightning talk : Android memory leak
lightning talk :  Android memory leaklightning talk :  Android memory leak
lightning talk : Android memory leak
Deivison Sporteman
 
Memory management for_android_apps
Memory management for_android_appsMemory management for_android_apps
Memory management for_android_apps
Bin Shao
 
LCA13: Memory Hotplug on Android
LCA13: Memory Hotplug on AndroidLCA13: Memory Hotplug on Android
LCA13: Memory Hotplug on Android
Linaro
 
[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
AiTi Education
 
Memory management in Android
Memory management in AndroidMemory management in Android
Memory management in Android
Keyhan Asghari
 
Memory management in Andoid
Memory management in AndoidMemory management in Andoid
Memory management in Andoid
Monkop Inc
 
Know about Android Operating System
Know about Android Operating SystemKnow about Android Operating System
Know about Android Operating System
Trailukya Dutta
 
Android Design
Android DesignAndroid Design
Android Design
Shyamala Prayaga
 
Memory leak analyse
Memory leak analyseMemory leak analyse
Memory leak analyse
Bernd Zuther
 
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
 
[NEXT] Android Profiler 사용법
[NEXT] Android Profiler 사용법 [NEXT] Android Profiler 사용법
[NEXT] Android Profiler 사용법
YoungSu Son
 
AsyncTask アンチパターン
AsyncTask アンチパターンAsyncTask アンチパターン
AsyncTask アンチパターン
Hiroshi Kurokawa
 
Memory Management in Android
Memory Management in AndroidMemory Management in Android
Memory Management in Android
Opersys inc.
 
Android memory fundamentals
Android memory fundamentalsAndroid memory fundamentals
Android memory fundamentals
Taras Leskiv
 
Memory Management in Android
Memory Management in AndroidMemory Management in Android
Memory Management in Android
Opersys inc.
 
OpenCV Introduction
OpenCV IntroductionOpenCV Introduction
OpenCV Introduction
Zachary Blair
 
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
 
Android - Preventing common memory leaks
Android - Preventing common memory leaksAndroid - Preventing common memory leaks
Android - Preventing common memory leaks
Ali Muzaffar
 
Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android Applications
Lokesh Ponnada
 
lightning talk : Android memory leak
lightning talk :  Android memory leaklightning talk :  Android memory leak
lightning talk : Android memory leak
Deivison Sporteman
 
Memory management for_android_apps
Memory management for_android_appsMemory management for_android_apps
Memory management for_android_apps
Bin Shao
 
LCA13: Memory Hotplug on Android
LCA13: Memory Hotplug on AndroidLCA13: Memory Hotplug on Android
LCA13: Memory Hotplug on Android
Linaro
 
[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
AiTi Education
 
Memory management in Android
Memory management in AndroidMemory management in Android
Memory management in Android
Keyhan Asghari
 
Memory management in Andoid
Memory management in AndoidMemory management in Andoid
Memory management in Andoid
Monkop Inc
 
Know about Android Operating System
Know about Android Operating SystemKnow about Android Operating System
Know about Android Operating System
Trailukya Dutta
 
Memory leak analyse
Memory leak analyseMemory leak analyse
Memory leak analyse
Bernd Zuther
 
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
 
[NEXT] Android Profiler 사용법
[NEXT] Android Profiler 사용법 [NEXT] Android Profiler 사용법
[NEXT] Android Profiler 사용법
YoungSu Son
 
AsyncTask アンチパターン
AsyncTask アンチパターンAsyncTask アンチパターン
AsyncTask アンチパターン
Hiroshi Kurokawa
 
Memory Management in Android
Memory Management in AndroidMemory Management in Android
Memory Management in Android
Opersys inc.
 
Android memory fundamentals
Android memory fundamentalsAndroid memory fundamentals
Android memory fundamentals
Taras Leskiv
 
Memory Management in Android
Memory Management in AndroidMemory Management in Android
Memory Management in Android
Opersys inc.
 
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
 

Similar to Identifying memory leaks in Android applications (20)

Sporar
SporarSporar
Sporar
oscon2007
 
Memory management
Memory managementMemory management
Memory management
Kuban Dzhakipov
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVM
Rafael Winterhalter
 
Java Memory Analysis: Problems and Solutions
Java Memory Analysis: Problems and SolutionsJava Memory Analysis: Problems and Solutions
Java Memory Analysis: Problems and Solutions
"Mikhail "Misha"" Dmitriev
 
Memory leak workshop
Memory leak workshopMemory leak workshop
Memory leak workshop
Mircea-Andrei Albu
 
performance optimization: Memory
performance optimization: Memoryperformance optimization: Memory
performance optimization: Memory
晓东 杜
 
Java Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningJava Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and Tuning
Carol McDonald
 
TechGIG_Memory leaks in_java_webnair_26th_july_2012
TechGIG_Memory leaks in_java_webnair_26th_july_2012TechGIG_Memory leaks in_java_webnair_26th_july_2012
TechGIG_Memory leaks in_java_webnair_26th_july_2012
Ashish Bhasin
 
Garbage Collection, Tuning And Monitoring JVM In EBS 11i And R12
Garbage Collection, Tuning And Monitoring JVM In EBS 11i And R12Garbage Collection, Tuning And Monitoring JVM In EBS 11i And R12
Garbage Collection, Tuning And Monitoring JVM In EBS 11i And R12
sidg75
 
Top 5 Java Performance Problems Presentation!
Top 5 Java Performance Problems Presentation!Top 5 Java Performance Problems Presentation!
Top 5 Java Performance Problems Presentation!
Tier1 app
 
Exploring Java Heap Dumps (Oracle Code One 2018)
Exploring Java Heap Dumps (Oracle Code One 2018)Exploring Java Heap Dumps (Oracle Code One 2018)
Exploring Java Heap Dumps (Oracle Code One 2018)
Ryan Cuprak
 
C++ memory leak detection
C++ memory leak detectionC++ memory leak detection
C++ memory leak detection
Võ Hòa
 
Eclipse Memory Analyzer Tool
Eclipse Memory Analyzer ToolEclipse Memory Analyzer Tool
Eclipse Memory Analyzer Tool
littleeye
 
Mule memory leak issue
Mule memory leak issueMule memory leak issue
Mule memory leak issue
JeeHyunLim
 
Effective memory management
Effective memory managementEffective memory management
Effective memory management
Yurii Kotov
 
Effective memory management
Effective memory managementEffective memory management
Effective memory management
Denis Zhuchinski
 
Javasession10
Javasession10Javasession10
Javasession10
Rajeev Kumar
 
(JVM) Garbage Collection - Brown Bag Session
(JVM) Garbage Collection - Brown Bag Session(JVM) Garbage Collection - Brown Bag Session
(JVM) Garbage Collection - Brown Bag Session
Jens Hadlich
 
Android Memory , Where is all My RAM
Android Memory , Where is all My RAM Android Memory , Where is all My RAM
Android Memory , Where is all My RAM
Yossi Elkrief
 
Memory management
Memory managementMemory management
Memory management
mitesh_sharma
 
A topology of memory leaks on the JVM
A topology of memory leaks on the JVMA topology of memory leaks on the JVM
A topology of memory leaks on the JVM
Rafael Winterhalter
 
performance optimization: Memory
performance optimization: Memoryperformance optimization: Memory
performance optimization: Memory
晓东 杜
 
Java Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningJava Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and Tuning
Carol McDonald
 
TechGIG_Memory leaks in_java_webnair_26th_july_2012
TechGIG_Memory leaks in_java_webnair_26th_july_2012TechGIG_Memory leaks in_java_webnair_26th_july_2012
TechGIG_Memory leaks in_java_webnair_26th_july_2012
Ashish Bhasin
 
Garbage Collection, Tuning And Monitoring JVM In EBS 11i And R12
Garbage Collection, Tuning And Monitoring JVM In EBS 11i And R12Garbage Collection, Tuning And Monitoring JVM In EBS 11i And R12
Garbage Collection, Tuning And Monitoring JVM In EBS 11i And R12
sidg75
 
Top 5 Java Performance Problems Presentation!
Top 5 Java Performance Problems Presentation!Top 5 Java Performance Problems Presentation!
Top 5 Java Performance Problems Presentation!
Tier1 app
 
Exploring Java Heap Dumps (Oracle Code One 2018)
Exploring Java Heap Dumps (Oracle Code One 2018)Exploring Java Heap Dumps (Oracle Code One 2018)
Exploring Java Heap Dumps (Oracle Code One 2018)
Ryan Cuprak
 
C++ memory leak detection
C++ memory leak detectionC++ memory leak detection
C++ memory leak detection
Võ Hòa
 
Eclipse Memory Analyzer Tool
Eclipse Memory Analyzer ToolEclipse Memory Analyzer Tool
Eclipse Memory Analyzer Tool
littleeye
 
Mule memory leak issue
Mule memory leak issueMule memory leak issue
Mule memory leak issue
JeeHyunLim
 
Effective memory management
Effective memory managementEffective memory management
Effective memory management
Yurii Kotov
 
Effective memory management
Effective memory managementEffective memory management
Effective memory management
Denis Zhuchinski
 
(JVM) Garbage Collection - Brown Bag Session
(JVM) Garbage Collection - Brown Bag Session(JVM) Garbage Collection - Brown Bag Session
(JVM) Garbage Collection - Brown Bag Session
Jens Hadlich
 
Android Memory , Where is all My RAM
Android Memory , Where is all My RAM Android Memory , Where is all My RAM
Android Memory , Where is all My RAM
Yossi Elkrief
 

Recently uploaded (20)

Eating Our Own Dog Food: How to be taken seriously when it comes to adding va...
Eating Our Own Dog Food: How to be taken seriously when it comes to adding va...Eating Our Own Dog Food: How to be taken seriously when it comes to adding va...
Eating Our Own Dog Food: How to be taken seriously when it comes to adding va...
UXPA Boston
 
AI and Gender: Decoding the Sociological Impact
AI and Gender: Decoding the Sociological ImpactAI and Gender: Decoding the Sociological Impact
AI and Gender: Decoding the Sociological Impact
SaikatBasu37
 
Storage Setup for LINSTOR/DRBD/CloudStack
Storage Setup for LINSTOR/DRBD/CloudStackStorage Setup for LINSTOR/DRBD/CloudStack
Storage Setup for LINSTOR/DRBD/CloudStack
ShapeBlue
 
Artificial Intelligence (Kecerdasan Buatan).pdf
Artificial Intelligence (Kecerdasan Buatan).pdfArtificial Intelligence (Kecerdasan Buatan).pdf
Artificial Intelligence (Kecerdasan Buatan).pdf
NufiEriKusumawati
 
Build your own NES Emulator... with Kotlin
Build your own NES Emulator... with KotlinBuild your own NES Emulator... with Kotlin
Build your own NES Emulator... with Kotlin
Artur Skowroński
 
A simple Introduction to Algorithmic Fairness
A simple Introduction to Algorithmic FairnessA simple Introduction to Algorithmic Fairness
A simple Introduction to Algorithmic Fairness
Paolo Missier
 
Assurance Best Practices: Unlocking Proactive Network Operations
Assurance Best Practices: Unlocking Proactive Network OperationsAssurance Best Practices: Unlocking Proactive Network Operations
Assurance Best Practices: Unlocking Proactive Network Operations
ThousandEyes
 
STKI Annual Israel IT Market Study 2025 .
STKI Annual Israel IT Market Study 2025 .STKI Annual Israel IT Market Study 2025 .
STKI Annual Israel IT Market Study 2025 .
Dr. Jimmy Schwarzkopf
 
MCP Dev Summit - Pragmatic Scaling of Enterprise GenAI with MCP
MCP Dev Summit - Pragmatic Scaling of Enterprise GenAI with MCPMCP Dev Summit - Pragmatic Scaling of Enterprise GenAI with MCP
MCP Dev Summit - Pragmatic Scaling of Enterprise GenAI with MCP
Sambhav Kothari
 
Wondershare Filmora 14.3.2 Crack + License Key Free for Windows PC
Wondershare Filmora 14.3.2 Crack + License Key Free for Windows PCWondershare Filmora 14.3.2 Crack + License Key Free for Windows PC
Wondershare Filmora 14.3.2 Crack + License Key Free for Windows PC
Mudasir
 
MuleSoft RTF & Flex Gateway on AKS – Setup, Insights & Real-World Tips
MuleSoft RTF & Flex Gateway on AKS – Setup, Insights & Real-World TipsMuleSoft RTF & Flex Gateway on AKS – Setup, Insights & Real-World Tips
MuleSoft RTF & Flex Gateway on AKS – Setup, Insights & Real-World Tips
Patryk Bandurski
 
Building Agents with LangGraph & Gemini
Building Agents with LangGraph &  GeminiBuilding Agents with LangGraph &  Gemini
Building Agents with LangGraph & Gemini
HusseinMalikMammadli
 
Multi-Agent AI Systems: Architectures & Communication (MCP and A2A)
Multi-Agent AI Systems: Architectures & Communication (MCP and A2A)Multi-Agent AI Systems: Architectures & Communication (MCP and A2A)
Multi-Agent AI Systems: Architectures & Communication (MCP and A2A)
HusseinMalikMammadli
 
Stretching CloudStack over multiple datacenters
Stretching CloudStack over multiple datacentersStretching CloudStack over multiple datacenters
Stretching CloudStack over multiple datacenters
ShapeBlue
 
PSEP - Salesforce Power of the Platform.pdf
PSEP - Salesforce Power of the Platform.pdfPSEP - Salesforce Power of the Platform.pdf
PSEP - Salesforce Power of the Platform.pdf
ssuser3d62c6
 
AI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AI
AI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AIAI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AI
AI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AI
Buhake Sindi
 
Interactive SQL: SQL, Features of SQL, DDL & DML
Interactive SQL: SQL, Features of SQL,  DDL & DMLInteractive SQL: SQL, Features of SQL,  DDL & DML
Interactive SQL: SQL, Features of SQL, DDL & DML
IsakkiDeviP
 
Four Principles for Physically Interpretable World Models (poster)
Four Principles for Physically Interpretable World Models (poster)Four Principles for Physically Interpretable World Models (poster)
Four Principles for Physically Interpretable World Models (poster)
Ivan Ruchkin
 
Dr Schwarzkopf presentation on STKI Summit A
Dr Schwarzkopf presentation on STKI Summit ADr Schwarzkopf presentation on STKI Summit A
Dr Schwarzkopf presentation on STKI Summit A
Dr. Jimmy Schwarzkopf
 
Breaking it Down: Microservices Architecture for PHP Developers
Breaking it Down: Microservices Architecture for PHP DevelopersBreaking it Down: Microservices Architecture for PHP Developers
Breaking it Down: Microservices Architecture for PHP Developers
pmeth1
 
Eating Our Own Dog Food: How to be taken seriously when it comes to adding va...
Eating Our Own Dog Food: How to be taken seriously when it comes to adding va...Eating Our Own Dog Food: How to be taken seriously when it comes to adding va...
Eating Our Own Dog Food: How to be taken seriously when it comes to adding va...
UXPA Boston
 
AI and Gender: Decoding the Sociological Impact
AI and Gender: Decoding the Sociological ImpactAI and Gender: Decoding the Sociological Impact
AI and Gender: Decoding the Sociological Impact
SaikatBasu37
 
Storage Setup for LINSTOR/DRBD/CloudStack
Storage Setup for LINSTOR/DRBD/CloudStackStorage Setup for LINSTOR/DRBD/CloudStack
Storage Setup for LINSTOR/DRBD/CloudStack
ShapeBlue
 
Artificial Intelligence (Kecerdasan Buatan).pdf
Artificial Intelligence (Kecerdasan Buatan).pdfArtificial Intelligence (Kecerdasan Buatan).pdf
Artificial Intelligence (Kecerdasan Buatan).pdf
NufiEriKusumawati
 
Build your own NES Emulator... with Kotlin
Build your own NES Emulator... with KotlinBuild your own NES Emulator... with Kotlin
Build your own NES Emulator... with Kotlin
Artur Skowroński
 
A simple Introduction to Algorithmic Fairness
A simple Introduction to Algorithmic FairnessA simple Introduction to Algorithmic Fairness
A simple Introduction to Algorithmic Fairness
Paolo Missier
 
Assurance Best Practices: Unlocking Proactive Network Operations
Assurance Best Practices: Unlocking Proactive Network OperationsAssurance Best Practices: Unlocking Proactive Network Operations
Assurance Best Practices: Unlocking Proactive Network Operations
ThousandEyes
 
STKI Annual Israel IT Market Study 2025 .
STKI Annual Israel IT Market Study 2025 .STKI Annual Israel IT Market Study 2025 .
STKI Annual Israel IT Market Study 2025 .
Dr. Jimmy Schwarzkopf
 
MCP Dev Summit - Pragmatic Scaling of Enterprise GenAI with MCP
MCP Dev Summit - Pragmatic Scaling of Enterprise GenAI with MCPMCP Dev Summit - Pragmatic Scaling of Enterprise GenAI with MCP
MCP Dev Summit - Pragmatic Scaling of Enterprise GenAI with MCP
Sambhav Kothari
 
Wondershare Filmora 14.3.2 Crack + License Key Free for Windows PC
Wondershare Filmora 14.3.2 Crack + License Key Free for Windows PCWondershare Filmora 14.3.2 Crack + License Key Free for Windows PC
Wondershare Filmora 14.3.2 Crack + License Key Free for Windows PC
Mudasir
 
MuleSoft RTF & Flex Gateway on AKS – Setup, Insights & Real-World Tips
MuleSoft RTF & Flex Gateway on AKS – Setup, Insights & Real-World TipsMuleSoft RTF & Flex Gateway on AKS – Setup, Insights & Real-World Tips
MuleSoft RTF & Flex Gateway on AKS – Setup, Insights & Real-World Tips
Patryk Bandurski
 
Building Agents with LangGraph & Gemini
Building Agents with LangGraph &  GeminiBuilding Agents with LangGraph &  Gemini
Building Agents with LangGraph & Gemini
HusseinMalikMammadli
 
Multi-Agent AI Systems: Architectures & Communication (MCP and A2A)
Multi-Agent AI Systems: Architectures & Communication (MCP and A2A)Multi-Agent AI Systems: Architectures & Communication (MCP and A2A)
Multi-Agent AI Systems: Architectures & Communication (MCP and A2A)
HusseinMalikMammadli
 
Stretching CloudStack over multiple datacenters
Stretching CloudStack over multiple datacentersStretching CloudStack over multiple datacenters
Stretching CloudStack over multiple datacenters
ShapeBlue
 
PSEP - Salesforce Power of the Platform.pdf
PSEP - Salesforce Power of the Platform.pdfPSEP - Salesforce Power of the Platform.pdf
PSEP - Salesforce Power of the Platform.pdf
ssuser3d62c6
 
AI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AI
AI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AIAI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AI
AI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AI
Buhake Sindi
 
Interactive SQL: SQL, Features of SQL, DDL & DML
Interactive SQL: SQL, Features of SQL,  DDL & DMLInteractive SQL: SQL, Features of SQL,  DDL & DML
Interactive SQL: SQL, Features of SQL, DDL & DML
IsakkiDeviP
 
Four Principles for Physically Interpretable World Models (poster)
Four Principles for Physically Interpretable World Models (poster)Four Principles for Physically Interpretable World Models (poster)
Four Principles for Physically Interpretable World Models (poster)
Ivan Ruchkin
 
Dr Schwarzkopf presentation on STKI Summit A
Dr Schwarzkopf presentation on STKI Summit ADr Schwarzkopf presentation on STKI Summit A
Dr Schwarzkopf presentation on STKI Summit A
Dr. Jimmy Schwarzkopf
 
Breaking it Down: Microservices Architecture for PHP Developers
Breaking it Down: Microservices Architecture for PHP DevelopersBreaking it Down: Microservices Architecture for PHP Developers
Breaking it Down: Microservices Architecture for PHP Developers
pmeth1
 

Identifying memory leaks in Android applications

  • 1. Identifying memory leaks in Android applications Zachary Blair
  • 2. Doesn’t Java handle memory management?!!!1 http://memegenerator.net/instance/27508171
  • 3. Garbage collection != Immunity from memory leaks!
  • 4. Java/Android Garbage Collection • All objects created with new are stored on the heap • GC periodically disposes of objects that are eligible for garbage collection • Android GC uses a mark-and-sweep algorithm
  • 5. What is “eligible for garbage collection”? • Not reachable from any live threads or any static references − There are no references to the object! • Cyclic references are not counted.
  • 6. What is “eligible for garbage collection”? Simple Example: Integer volume = new Integer(100); Integer System.out.println(volume); 100 volume = null; Volume
  • 7. What is “eligible for garbage collection”? Simple Example: Integer volume = new Integer(100); Integer System.out.println(volume); 100 volume = null; Volume
  • 8. What is “eligible for garbage collection”? Simple Example: Integer volume = new Integer(100); Integer System.out.println(volume); 100 volume = null; null Volume
  • 9. What is “eligible for garbage collection”? Simple Example: Integer volume = new Integer(100); Integer Garbage System.out.println(volume); collected! volume = null; null Volume
  • 10. What is “eligible for garbage collection”? • Lists, Maps, and Sets are prime suspects for memory leaks, because they can accidentally retain references to unused objects! Integer volume = new Integer(100); Integer System.out.println(volume); 100 volumeList.add(volume); null volume = null; Volume volumeList
  • 11. Haiku Break #1 Memory leak, crash. Lingering reference, why!? You make GC cry.
  • 12. Detecting a memory leak Clue #1: Your application crashes with an java.lang.OutOfMemoryError after running for a long time. Clue #2: You see frequent GC_ lines in logcat before the crash. D/dalvikvm( 1325): GC_CONCURRENT freed 1971K, 18% free 12382K/14983K, paused 3ms+7ms Heap statistics Reason for garbage collection: GC_CONCURRENT GC_FOR_MALLOC GC_EXTERNAL_ALLOC GC_HPROF_DUMP_HEAP GC_EXPLICIT
  • 13. Identifying the source of the leak • Step #1 Use DDMS (Dalvik Debug Monitor Server) to dump heap snapshots (an HPROF file) − android-sdkstoolsddms.bat
  • 14. Identifying the source of the leak • Step #2 Use the Android SDK’s “hprof-conv” tool to convert the Android-specific HPROF file into a generic HPROF file that can be analyzed by Eclipse Memory Analyzer
  • 15. Identifying the source of the leak • Step #3 Open the converted HPROF using Eclipse Memory Analyzer
  • 16. Identifying the source of the leak • Step #4 Analyze the results using the “Histogram” view
  • 17. Identifying the source of the leak • Step #4 Analyze the results using the “Histogram” view • Identifies types of objects allocated! • Doesn’t know whether they will eventually be freed! • We must compare two HPROF snapshots to identify which objects are responsible for a leak.
  • 18. Identifying the source of the leak • Step #5 Exercise your app in a way that tends to cause it to exhibit the memory leak, and then use DDMS to dump another heap snapshot • Step #6 Use “hprof-conv” to convert the HPROF file
  • 19. Identifying the source of the leak • Step #7 Open both HPROF files in Eclipse Memory Analyzer and compare their histograms
  • 20. Identifying the source of the leak • Step #7 Open both HPROF files in Eclipse Memory Analyzer and compare their histograms − Compare the number of instances and sizes of each type of object between the two snapshots. − A sustained increase in the number of objects may indicate a leak!
  • 21. Identifying the source of the leak • Step #7 Open both HPROF files in Eclipse Memory Analyzer and compare their histograms − Compare the number of instances and sizes of each type of object between the two snapshots. − An unexplained increase in the number of objects may indicate a leak!
  • 22. Identifying the source of the leak • Step #7 Shallow heap: the size of the objects themselves. Retained heap: the amount that would be freed if we freed these objects (greater than shallow heap because referenced objects may also be freed).
  • 23. Haiku Break #2 Unfreed memory, Delta of two HPROF dumps Who references thou?
  • 24. Fixing the leak • The leak must be the result of unused objects still having references to them so, you can do one of: − Stop creating the objects − Stop maintaining references to the objects − Use weak references (see WeakHashMap) so that those references are invalidated if they are the only ones to the object. − Make sure to close() open streams and connections
  • 25. More Information • Google I/O 2011: Memory management for Android Apps https://www.youtube.com/watch?v=_ CruQY55HOk&feature=relmfu