SlideShare une entreprise Scribd logo
1  sur  18
Tips and Tricks to
attack memory
problem in Android
Programming
Phạm Cao Trí – tripc@vng.com.vn
Lead Engineer – Zalo team – Zing
Agenda
• 1. Memory problems: Why?
• 2. Android Bitmaps: A closer look and some tips
• 3. Memory leaking: Demos, Detection methods
and Solutions.
1. Memory
problems: Why?
1.1. Memory problem:
Why?
• Limited heap size: 16MB, 32MB, 64MB,..
• Usually in decoding or manipulating bitmaps.
• Painful in solving memory problem for low-end devices
2. Android Bitmaps:
A closer look and
some tips
2.1. Android Bitmaps
• Java object
• Decode in native via Skia library (SkBitmap)
• Allocation:
• Pre Honeycomb (< 3.0): Native heap but “report” size to Dalvik VM.
• Honeycomb and beyond (>= 3.0): VM/Java heap
2.1. Android Bitmaps (con’t)
• BitmapFactory.java: decodeResource ->.. -> nativeDecodeAsset
• BitmapFactory.cpp: nativeDecodeAsset -> doDecode
2.1. Android Bitmaps (con’t)
• Pixel allocation policy lies within Graphics.cpp:
2.1. Android Bitmaps (con’t)
• Graphics.cpp: createBitmap -> Bitmap.java: Bitmap constructor
• Pre-Honeycomb (< 3.0):
• Honeycomb and beyond (>= 3.0):
2.1. Android Bitmaps (con’t)
• Pre-Gingerbread GC (< 2.3):
• Stop all app’s threads – not stop all applications
• Full heap collection
• Pause times often > 100ms
• Gingerbread and beyond (>= 2.3):
• Mostly concurrent
• Partial collections
• Pause times usually < 5ms
• Garbage collection in Android does no compacting.
2.2. Bitmap cache
• Architecture
2.2. Bitmap cache (con’t)
• Small tips:
• A better version:
• Honeycomb or later: android:largeHeap
3. Memory leaking:
Demos, Detection
methods and Solutions
3.1. Garbage collection
• Basic algorithm: Mark and Sweep
• Does no compacting
• Memory leak ?
3.2. Demo memory leak
• Demo
• Detection Method:
• Code Review, Log and Heuristics
3.3. Demo memory leak
• Detection Method:
• MAT
• Solution
3.4. Common ways to leak
• Context Leak
• Handler Leak or Inner Class leak Outer
• Solution
Tips and tricks to attack memory problem in android programming

Contenu connexe

En vedette

Stream upload and asynchronous job processing in large scale systems
Stream upload and asynchronous job processing  in large scale systemsStream upload and asynchronous job processing  in large scale systems
Stream upload and asynchronous job processing in large scale systems
Zalo_app
 
Window phone programing pain and how to deal with
Window phone programing   pain and how to deal withWindow phone programing   pain and how to deal with
Window phone programing pain and how to deal with
Zalo_app
 
Server log, monitoring and qo s platform of a messaging app
Server   log, monitoring and qo s platform of a messaging appServer   log, monitoring and qo s platform of a messaging app
Server log, monitoring and qo s platform of a messaging app
Zalo_app
 
Experience lessons from architecture of zalo real time system
Experience lessons from architecture of zalo real time systemExperience lessons from architecture of zalo real time system
Experience lessons from architecture of zalo real time system
Zalo_app
 
Advance Android Programming - learning beyond basics
Advance Android Programming - learning beyond basicsAdvance Android Programming - learning beyond basics
Advance Android Programming - learning beyond basics
ayman diab
 
Memory problems in android programming
Memory problems in android programmingMemory problems in android programming
Memory problems in android programming
AiTi Education
 
Design a scalable social network: Problems and solutions
Design a scalable social network: Problems and solutionsDesign a scalable social network: Problems and solutions
Design a scalable social network: Problems and solutions
Chau Thanh
 

En vedette (20)

Stream upload and asynchronous job processing in large scale systems
Stream upload and asynchronous job processing  in large scale systemsStream upload and asynchronous job processing  in large scale systems
Stream upload and asynchronous job processing in large scale systems
 
Inside Zalo: Developing a mobile messenger for the audience of millions - VN ...
Inside Zalo: Developing a mobile messenger for the audience of millions - VN ...Inside Zalo: Developing a mobile messenger for the audience of millions - VN ...
Inside Zalo: Developing a mobile messenger for the audience of millions - VN ...
 
Window phone programing pain and how to deal with
Window phone programing   pain and how to deal withWindow phone programing   pain and how to deal with
Window phone programing pain and how to deal with
 
Server log, monitoring and qo s platform of a messaging app
Server   log, monitoring and qo s platform of a messaging appServer   log, monitoring and qo s platform of a messaging app
Server log, monitoring and qo s platform of a messaging app
 
Sơ lược kiến trúc hệ thống Zing Me
Sơ lược kiến trúc hệ thống Zing MeSơ lược kiến trúc hệ thống Zing Me
Sơ lược kiến trúc hệ thống Zing Me
 
Building ZingMe News Feed System
Building ZingMe News Feed SystemBuilding ZingMe News Feed System
Building ZingMe News Feed System
 
Experience lessons from architecture of zalo real time system
Experience lessons from architecture of zalo real time systemExperience lessons from architecture of zalo real time system
Experience lessons from architecture of zalo real time system
 
How to create a chat application on Android platform?
How to create a chat application on Android platform? How to create a chat application on Android platform?
How to create a chat application on Android platform?
 
Non Conventional Android Programming (English)
Non Conventional Android Programming (English)Non Conventional Android Programming (English)
Non Conventional Android Programming (English)
 
Advance Android application development workshop day 1
Advance Android application development workshop day 1Advance Android application development workshop day 1
Advance Android application development workshop day 1
 
Advance Android Programming - learning beyond basics
Advance Android Programming - learning beyond basicsAdvance Android Programming - learning beyond basics
Advance Android Programming - learning beyond basics
 
Memory problems in android programming
Memory problems in android programmingMemory problems in android programming
Memory problems in android programming
 
Software proposal on android
Software proposal on androidSoftware proposal on android
Software proposal on android
 
Android Workshop 2013
Android Workshop 2013Android Workshop 2013
Android Workshop 2013
 
Design a scalable social network: Problems and solutions
Design a scalable social network: Problems and solutionsDesign a scalable social network: Problems and solutions
Design a scalable social network: Problems and solutions
 
Asynchronous Programming in Android
Asynchronous Programming in AndroidAsynchronous Programming in Android
Asynchronous Programming in Android
 
Android UI Reference
Android UI ReferenceAndroid UI Reference
Android UI Reference
 
Advance Android application development workshop day 3
Advance Android application development workshop day 3Advance Android application development workshop day 3
Advance Android application development workshop day 3
 
Web Development Fundamentals
Web Development FundamentalsWeb Development Fundamentals
Web Development Fundamentals
 
Tất tần tật về zalo page
Tất tần tật về zalo pageTất tần tật về zalo page
Tất tần tật về zalo page
 

Similaire à Tips and tricks to attack memory problem in android programming

Marmalade: bittersweet experience
Marmalade: bittersweet experienceMarmalade: bittersweet experience
Marmalade: bittersweet experience
Max Klyga
 
Marmalade bittersweet experience
Marmalade bittersweet experienceMarmalade bittersweet experience
Marmalade bittersweet experience
Alexander Degtyarev
 
DefCon 2012 - Gaining Access to User Android Data
DefCon 2012 - Gaining Access to User Android DataDefCon 2012 - Gaining Access to User Android Data
DefCon 2012 - Gaining Access to User Android Data
Michael Smith
 
One-Byte Modification for Breaking Memory Forensic Analysis
One-Byte Modification for Breaking Memory Forensic AnalysisOne-Byte Modification for Breaking Memory Forensic Analysis
One-Byte Modification for Breaking Memory Forensic Analysis
Takahiro Haruyama
 

Similaire à Tips and tricks to attack memory problem in android programming (20)

Facebook tricks for image handling in Android
Facebook tricks for image handling in AndroidFacebook tricks for image handling in Android
Facebook tricks for image handling in Android
 
Marmalade: bittersweet experience
Marmalade: bittersweet experienceMarmalade: bittersweet experience
Marmalade: bittersweet experience
 
VMs, Interpreters, JIT
VMs, Interpreters, JITVMs, Interpreters, JIT
VMs, Interpreters, JIT
 
Marmalade bittersweet experience
Marmalade bittersweet experienceMarmalade bittersweet experience
Marmalade bittersweet experience
 
JavaScript all the things - JavaScript fwdays 2018
JavaScript all the things - JavaScript fwdays 2018JavaScript all the things - JavaScript fwdays 2018
JavaScript all the things - JavaScript fwdays 2018
 
Ndk
NdkNdk
Ndk
 
The Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidThe Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with android
 
DefCon 2012 - Gaining Access to User Android Data
DefCon 2012 - Gaining Access to User Android DataDefCon 2012 - Gaining Access to User Android Data
DefCon 2012 - Gaining Access to User Android Data
 
Developing Next-Generation Games with Stage3D (Molehill)
Developing Next-Generation Games with Stage3D (Molehill) Developing Next-Generation Games with Stage3D (Molehill)
Developing Next-Generation Games with Stage3D (Molehill)
 
Performance Optimization for Sencha Touch
Performance Optimization for Sencha TouchPerformance Optimization for Sencha Touch
Performance Optimization for Sencha Touch
 
Surge2012
Surge2012Surge2012
Surge2012
 
Learn how to debug
Learn how to debugLearn how to debug
Learn how to debug
 
2004: Söldner - a Post Mortem
2004: Söldner - a Post Mortem2004: Söldner - a Post Mortem
2004: Söldner - a Post Mortem
 
Programming
ProgrammingProgramming
Programming
 
Memcached Code Camp 2009
Memcached Code Camp 2009Memcached Code Camp 2009
Memcached Code Camp 2009
 
Pitfalls of Object Oriented Programming by SONY
Pitfalls of Object Oriented Programming by SONYPitfalls of Object Oriented Programming by SONY
Pitfalls of Object Oriented Programming by SONY
 
You suck at Memory Analysis
You suck at Memory AnalysisYou suck at Memory Analysis
You suck at Memory Analysis
 
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
 
Brewing Your Own Game Engie eng
Brewing Your Own Game Engie engBrewing Your Own Game Engie eng
Brewing Your Own Game Engie eng
 
One-Byte Modification for Breaking Memory Forensic Analysis
One-Byte Modification for Breaking Memory Forensic AnalysisOne-Byte Modification for Breaking Memory Forensic Analysis
One-Byte Modification for Breaking Memory Forensic Analysis
 

Dernier

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Dernier (20)

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 

Tips and tricks to attack memory problem in android programming

  • 1. Tips and Tricks to attack memory problem in Android Programming Phạm Cao Trí – tripc@vng.com.vn Lead Engineer – Zalo team – Zing
  • 2. Agenda • 1. Memory problems: Why? • 2. Android Bitmaps: A closer look and some tips • 3. Memory leaking: Demos, Detection methods and Solutions.
  • 4. 1.1. Memory problem: Why? • Limited heap size: 16MB, 32MB, 64MB,.. • Usually in decoding or manipulating bitmaps. • Painful in solving memory problem for low-end devices
  • 5. 2. Android Bitmaps: A closer look and some tips
  • 6. 2.1. Android Bitmaps • Java object • Decode in native via Skia library (SkBitmap) • Allocation: • Pre Honeycomb (< 3.0): Native heap but “report” size to Dalvik VM. • Honeycomb and beyond (>= 3.0): VM/Java heap
  • 7. 2.1. Android Bitmaps (con’t) • BitmapFactory.java: decodeResource ->.. -> nativeDecodeAsset • BitmapFactory.cpp: nativeDecodeAsset -> doDecode
  • 8. 2.1. Android Bitmaps (con’t) • Pixel allocation policy lies within Graphics.cpp:
  • 9. 2.1. Android Bitmaps (con’t) • Graphics.cpp: createBitmap -> Bitmap.java: Bitmap constructor • Pre-Honeycomb (< 3.0): • Honeycomb and beyond (>= 3.0):
  • 10. 2.1. Android Bitmaps (con’t) • Pre-Gingerbread GC (< 2.3): • Stop all app’s threads – not stop all applications • Full heap collection • Pause times often > 100ms • Gingerbread and beyond (>= 2.3): • Mostly concurrent • Partial collections • Pause times usually < 5ms • Garbage collection in Android does no compacting.
  • 11. 2.2. Bitmap cache • Architecture
  • 12. 2.2. Bitmap cache (con’t) • Small tips: • A better version: • Honeycomb or later: android:largeHeap
  • 13. 3. Memory leaking: Demos, Detection methods and Solutions
  • 14. 3.1. Garbage collection • Basic algorithm: Mark and Sweep • Does no compacting • Memory leak ?
  • 15. 3.2. Demo memory leak • Demo • Detection Method: • Code Review, Log and Heuristics
  • 16. 3.3. Demo memory leak • Detection Method: • MAT • Solution
  • 17. 3.4. Common ways to leak • Context Leak • Handler Leak or Inner Class leak Outer • Solution

Notes de l'éditeur

  1. Phầnthứnhất: chúng ta sẽtìmhiểutổngquátvềbộnhớtrong Android, tạisaotronglậptrìnhcho di động, vàtronglậptrìnhtrong Android lại hay xảyracácvấnđềvềbộnhớPhầnthứhai: Chúng ta sẽcùngnhaugiảiphẫuđốitượng Android Bitmap, đốitượngdùngđểhiểnthịhìnhảnhtrong Android. Thườngthì 80-90% vấnđềvềbộnhớsẽcóliênquanđếnđốitượngnày.Phần thứ ba: Mìnhsẽnóivềmộtlỗiphổbiếnvềbộnhớ, đólàlỗi leak memory. Sẽcómộtvàivídụ demos vềlỗi leak mem, cáchpháthiệncũngnhưlàcácgiảipháp
  2. Câuhỏicơbản, ngồi ở đâychứngtỏđãtừngbịThường hay bịvấnđềvềbộnhớkhi decode vàthaotáctrênhìnhảnhnhư copy, xoaychẳnghạn.Tìnhhuốngthườnggặp: tạomộtứngdụngvàmuốntrangtrímộtvàihìnhảnhđểứngdụngđẹphơn -&gt; Search trênStackOverflowvàtìmđượcvài thread hướngdẫnlàphảidùngImageViewvàBitmapFactoryđể decode ảnhthànhđốitượng bitmap vàgắnvàoImageViewDecode mộtvàihìnhđầukhôngcóvấnđềgì, nhưngkhichươngtrìnhchạymộtthờigianchúng ta bắtđầugặpvấnđềvềbộnhớ. Thườngthìsẽgặplỗinày.Cácbạnbắtđầutìmhiểuđểgiảiquyếtlỗinày. Ởnhữnggiaiđoạnđầuthìcácbạnsẽcảmthấykhálàứcchếkhimàgiảiquyếtvấnđềbộnhớnày, đặcbiệtlàtrêncácmáyyếuchỉcó 16MB heap.
  3. - Cuối slide nóithêmvề try/catch khioutmem
  4. -Giảithíchvề memory leak từsơđồ Mark &amp; Sweep.