Android: Managing Memory

Slides:



Advertisements
Similar presentations
Android Application Development A Tutorial Driven Course.
Advertisements

Paging: Design Issues. Readings r Silbershatz et al: ,
FILE SYSTEM IMPLEMENTATION
ONYX RIP Version Technical Training General. Overview General Messaging and What’s New in X10 High Level Print and Cut & Profiling Overviews In Depth.
CSC 213 – Large Scale Programming. Today’s Goals  Consider what new does & how Java works  What are traditional means of managing memory?  Why did.
Reducing Pause Time of Conservative Collectors Toshio Endo (National Institute of Informatics) Kenjiro Taura (Univ. of Tokyo)
Android Tools & Wireless ADB Αντρέας Λύμπουρας Θεόφιλος Φωκάς Ζαχαρίας Χ’’Λάμπρου.
Fixing Memory Leaks in Android Applications using DDMS & MAT Presented by Sravankumar Reddy Javaji CS286 Mobile Programming.
Memory issues in production systems. Production system Restricted access Application, DB, Application server, log files Debugging, monitoring Investigation.
Hastings Purify: Fast Detection of Memory Leaks and Access Errors.
All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
CSE506: Operating Systems Block Cache. CSE506: Operating Systems Address Space Abstraction Given a file, which physical pages store its data? Each file.
On the limits of partial compaction Anna Bendersky & Erez Petrank Technion.
File System Implementation: beyond the user’s view A possible file system layout on a disk.
1 Virtual Memory vs. Physical Memory So far, all of a job’s virtual address space must be in physical memory However, many parts of programs are never.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
Memory Allocation and Garbage Collection. Why Dynamic Memory? We cannot know memory requirements in advance when the program is written. We cannot know.
Computer Organization Cs 147 Prof. Lee Azita Keshmiri.
Android Tutorial Android Written in Java Utilizes Dalvik VM – Just in time (JIT) compilation since Android 2.2.
Page 1 © 2001 Hewlett-Packard Company Tools for Measuring System and Application Performance Introduction GlancePlus Introduction Glance Motif Glance Character.
UFCFX5-15-3Mobile Device Development Android Development Environments and Windows.
Of Bytes, Cycles and Battery Life. Who am [2] [1]
Android 2: Introduction to the Technology Kirk Scott 1.
UI Design Patterns & Best Practices Mike Wolfson July 22, 2010.
CS378 - Mobile Computing App Project Overview. App Project Teams of 2 or 3 students Develop an Android application of your choosing subject to instructor.
Computers in the real world Objectives Understand what is meant by memory Difference between RAM and ROM Look at how memory affects the performance of.
Web/App Performance How to keep you out of the News
CS 153 Design of Operating Systems Spring 2015 Lecture 17: Paging.
Ideas to Improve SharePoint Usage 4. What are these 4 Ideas? 1. 7 Steps to check SharePoint Health 2. Avoid common Deployment Mistakes 3. Analyze SharePoint.
Chapter 3.5 Memory and I/O Systems. 2 Memory Management Memory problems are one of the leading causes of bugs in programs (60-80%) MUCH worse in languages.
Guide to Linux Installation and Administration, 2e1 Chapter 10 Managing System Resources.
Lecture Topics: 11/17 Page tables TLBs Virtual memory flat page tables
Chapter 8 – Main Memory (Pgs ). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.
Mobile Application Optimization. Sanath Nandasiri Programmer.(Not a singer) (Exilesoft)
CSC 213 – Large Scale Programming. Today’s Goals  Consider what new does & how Java works  What are traditional means of managing memory?  Why did.
Tracking Tasks and Processes. GET_TASK Make a new app, WatchProcesses – Add permission GET_TASK This permission allows the app to collect lots of information.
Developer TECH REFRESH 15 Junho 2015 #pttechrefres h Understand your end-users and your app with Application Insights.
Computer Systems Week 14: Memory Management Amanda Oddie.
Review °Apply Principle of Locality Recursively °Manage memory to disk? Treat as cache Included protection as bonus, now critical Use Page Table of mappings.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
CSC 213 – Large Scale Programming. Explicit Memory Management  Traditional form of memory management  Used a lot, but fallen out of favor  malloc /
CSCI 156: Lab 11 Paging. Our Simple Architecture Logical memory space for a process consists of 16 pages of 4k bytes each. Your program thinks it has.
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
Virtual Memory 1 Computer Organization II © McQuain Virtual Memory Use main memory as a “cache” for secondary (disk) storage – Managed jointly.
Guide to Parallel Operating Systems with Windows 7 and Linux Chapter 10 Operating System Management.
Memory Management for Android Apps Min Ji, Kim Embedded Software Lab.
Android Kernel Extensions
Free Transactions with Rio Vista
Processes and threads.
Protecting Memory What is there to protect in memory?
CSC 322 Operating Systems Concepts Lecture - 12: by
Protecting Memory What is there to protect in memory?
Android Studio, Android System Basics and Git
Virtual Memory - Part II
CLR MD A New Swiss Army Knife tool for Advanced Debugging
Operating Systems ECE344 Lecture 11: SSD Ding Yuan
KERNEL ARCHITECTURE.
Introduction to Smart Search
Chapter 9: Virtual-Memory Management
Page Replacement.
Application Development A Tutorial Driven Course
CS399 New Beginnings Jonathan Walpole.
Free Transactions with Rio Vista
Android Topics UI Thread and Limited processing resources
CSCE 313 – Introduction to UNIx process
File-System Structure
Memory Leaks in Blink Photo © 2011 J. Ronald Lee.
Lecture 3: Main Memory.
Virtual Memory Use main memory as a “cache” for secondary (disk) storage Managed jointly by CPU hardware and the operating system (OS) Programs share main.
Presentation transcript:

Android: Managing Memory Russell Myers

Me Russell Myers Amazon Fire Phone Web (recent) Middleware – applications and components Web (recent) Intranet sites for Comcast, Verizon, etc.

Overview

How much do we have? It’s been bad (Google I/O 2011 Memory Management For Android Applications) G1 (first Android phone): 16 MB Droid: 24 MB Nexus One: 32 MB Xoom (first “real” Android tablet): 48 MB These days 512 MB – 2 GB (reasonable targets) Android “L” will likely see an increase in this as 64-bit devices come in. KitKat could invite more low end devices as well. By the way, no memory swapping!

Is it really that much? (hint: No) From my Nexus 7 (2013) (all numbers in PSS) adb shell dumpsys meminfo System “stuff” 126350 kB: com.google.android.googlequicksearchbox 75594 kB: surfaceflinger 46574 kB: com.android.systemui 45217 kB: system 45054 kB: com.google.android.inputmethod.latin 13073 kB: com.google.process.location 12074 kB: zygote 9011 kB: com.android.phone (really!?) Other apps 68521 kB: com.facebook.katana 36805 kB: com.amazon.kindle 85455 kB: com.android.chrome … The bigger they are, the faster they fall. Low-Memory Killer (LMK) (http://lwn.net/Articles/317814/) Kills applications before the device runs out of memory; not when. “Androidism” Threshold heuristics Recency Size Priority Can be different per device.

Tablets are worse Pushing more pixels ARGB_8888 Nexus 5: 1920 x 1080 Fire Phone: 1280 x 720 Nexus 7 (2013): 1920 x 1200 Kindle Fire HDX 7: 1920 x 1200 Kindle Fire HDX 8.9: 2560 x 1600 ARGB_8888 Fire Phone - ~3.5 MB Kindle Fire HDX 8.9: ~15.6 MB More views or bigger views More advanced interfaces

Ramifications Crashes Out of memory exceptions Can’t free memory fast enough, causing ANRS Application terminated more frequently in background Restarts (thus slower starts) more often. Potential loss of state Can cause these problems for other applications as well! More power consumption

Ramifications (continued) More GC invocations! GC invocations get logged to logcat: Example: D/Dalvikvm GC_EXPLICIT freed 1328K 35 free 26195 39879K paused 3ms 5ms total 122ms There are a few types of garbage collection: GC_CONCURRENT - Happens every so often based on Android's perception of heap size. Some aspects of this are concurrent, but not all. GC_FOR_ALLOC - Happens when GC_CONCURRENT either couldn't free enough space in time for an allocation, or the allocation exceeded expectations. GC_BEFORE_OOM - A more aggressive GC call that happens right before a memory error. This occurs if GC_FOR_ALLOC fails. GC_EXPLICIT - Explicit GC call. (if you want this control, go unmanaged)

Looking at memory

Quick look adb shell dumpsys meminfo <package name> Shared – Shared memory Private – Stuff that’s just yours or “yet to be shared”. RSS – Resident Set Size PSS – Proportional Set Size Activities Views

Memory leaks A leak is a situation in which you have unexpected growth. A leak can be bounded or unbounded. You care about both! Bounded being “after a lot of use X grows to 300 MB but stays there”. Unbounded being “things continue to grow and grow” Rate of leak matters How does the leak occur? Usage pattern Background interaction

Potential causes Context leaks Bitmap leaks Singleton references Activities Views Bitmap leaks Singleton references Listeners Observers Async operations (runnables, handlers, threads) Inner classes hold references to parent classes

Testing for memory leaks watch --interval=2.0 “adb shell dumpsys meminfo <package name>” Acceptance tests / automated UI tests that run through common interactions. Espresso Monkeyrunner Note the seeds used in case you need to reproduce! Not smart, but randomized.

Testing (continued) Understand the lifecycle of components of your application. Activities / Views shouldn’t hang around. Done with that Bitmap? When does that listener need to be deregistered? Make sure you’re actually cleaning up in a safe area. (Activity#onStop() may not be called for example!) Cache only what you absolutely need! There will be growth! GC doesn’t constantly run (that’d be very bad) Overhead should (hopefully) remain low.

Found a leak! Root causing. What’s leaking? Eclipse MAT Activities, Views, Contexts, “managed stuff” NDK for unmanaged Embedded into Eclipse Update site: http://download.eclipse.org/mat/1.3.1/update-site/ No conversions necessary. (hprof-conv) Stand-alone tool http://www.eclipse.org/mat/downloads.php NDK for unmanaged stuff

Using MAT Open hprof files. Interesting views Histogram Dominator Tree Top consumers Comparing two hprofs from Histogram

Still using MAT Weak references don’t prevent garbage collection. Shallow heap – Object size in bytes. Retained heap – Size objects that could be freed by freeing the object. Considers other references

/proc/[pid]/smaps smaps () { pid=$(adb shell ps | grep "$1" | awk '{print $2}'); adb shell cat /proc/$pid/smaps | tr -d '\r’ # Parsing problems } “This file shows memory consumption for each of the process’s mappings” (proc man page) Useful for non-heap items (and in some situations when the NDK can’t help) There’s a lot of other fun stuff in /proc by the way. Check it out.

Estimation What are you going to take up in the worst case? What does a “worst case” even mean? How does your application grow? Notes app – Number of notes, size of notes, extra details to note, number of points in time notes occurred, etc. Books library – Number of books, covers of a book, book content, different in-memory caches of books. Game – Textures, models, players, monsters, pieces, etc. Measure costs along different points in that growth pattern to get an estimation and understand impact of different dimensions of growth. Supported devices (and as a result, image sizes)

Good link http://developer.android.com/training/articles/memory.html

Thanks! Twitter: @russell_myers E-mail: me@russellmyers.com