Memory management in Andoid
-
Upload
monkop-inc -
Category
Software
-
view
955 -
download
0
Transcript of Memory management in Andoid
Fabián Baptista @fbaptista
Android Memory Management
Fabián Baptista @fbaptista
Android Memory Management
Talk about…Android ArchitectureConcepts
How it works
Tools and logs
Under the hood…• Since Android is built on top of a Linux
Kernel, we should know a little bit about memory management on Linux …
• Other key topics that helps a lot:– Virtual Memory– Android Activity Lifecycle– Garbage Collection–Memory Heap
Architecture
Linux Kernel• Linux 2.6 + Google architecture changes.• Android core functionality is based on
top of Linux– Hardware interface & drivers–Memory Management– Process Management– Networking– Security– Etc.
Architecture
Android Runtime• Dalvik Virtual Machine– Like a special Java VM – Optimized for mobile world– Not .class -> .dex (bulit from a .class)
–Multiple instances of the VM running
• Core Libraries–Most of the functionalities of the Java SE
libraries
Architecture
Virtual Memory
• Maps process memory addresses (virtual) into physical addresses.
• Hardware: Address translation inside CPU (called MMU)
• Software: Extend physical memory size
Virtual Memory - Key Benefits• Avoid process to handle a shared
space.• More security & isolation• More memory available• Easier programming / hidden
fragmentation
Concepts• Swap Space– Extend physical (full) memory with HD
• Paging– Uses HD to store & retrieve “pages” of memory.– Allow non-contiguous address space for process – One of the most important part of typical Virtual
Memory implementation• Shared Memory• Heap
Garbage Collection
Garbage Collection
Android Way• Memory:– No Swap space, Paging–Memory mapping
• Shared Memory– Framework code & resources– Intercommunication (processes)– Intensively used
Android Way• New drivers:– ASHMEM (re-implemented)– Low Memory Killer (handle OOM situations)
• Empty Process–When a process is terminated, not all
memory is released.– Reduces response time when user access
same process.
Garbage Collection1. Pre Gingerbread:– GC “stops the world”– Full heap collection– Pause time often > 100ms
2. Gingerbread and beyond:– Concurrent (mostly)– Partial Collections– Pause time usually < 5ms
Garbage Collection - MsgTypes
• GC_CONCURRENT: Occurs when heap is growing (to avoid enlarge heap in time)
• GC_EXPLICIT: Triggered when an application calls System.gc(). Don’t use it!
• GC_FOR_MALLOC: Triggered when the heap is full and the application needs more memory. (stops app)
• GC_HPROF_DUMP_HEAP: Triggered when an HPROF file is created for memory analysis
• GC_BEFORE_OOM: (Undocumented) It’s supposed to be triggered when dalvik heap usage is over 90%
Android App Memory• Dalvik heap– Defines logical heap size–Memory can grow to a defined limit
(defined by the system)– One single virtual memory range– Physically non-contiguous (analyzed on GC)
• Logical size is different than Physical size• Hard to know exactly how much RAM
your process are using
Memory Numbers…
Proportional Set Size (PSS)• PSS is considered by Android as your
physical footprint
• Example of Dalvik Heap usage:– 20MB of (Private)– 12MB of Shared (between 4 process)– PSS = 20 + (12/4) = 23
Memory Numbers…
Private RAM• Private (Dirty and Clean) is memory
used by the App.• All heap allocations (Dalvik + Native)• Memory that will be available if your
process is destroyed.• Can’t be paged into storage
Memory Numbers…
Low Memory Killer (Linux)• Look for “bad” process (heuristics):– High memory consuming is bad– Long running process are good– Process with a lot of child process are
bad– Root processes are good (3% off)
Low Memory Killer (Android)
5 - Empty Process
4 - Background Process
3 - Started Services Process
2 - Visible Process
1 - Active Process Critical Priority
High Priority
Low Priority
Low Memory Killer (Android)
Threshold varies based on RAM size. (Loaded during booting time from init.rc file)
Memory Pages(4KB each)
3 Memory Tips 4 Apps1. Use Services in the right way– Only one process should take care of UI– Take care about dependencies
2. Release memory when– Your UI becomes hidden– Memory becomes tight
(ComponentCallbacks)
3. Use the right Bitmaps for each screen resolution
Services• Application component without UI.• Can be used to perform long-running
operations in background.• Inter-process communication• Provide a client-server model to bind
with
Services State
Started• startService()
Bound• bindService()
Services Started> startService();
• Can run indefinitely• Even if the activity who started it is destroyed
• Once it is finished, the service should stop itself
Services Bound> bindService();
• Client / Server interface to:• Request service• Get results• IPC
• Bind by one• Is destroyed only when all of them unbind
Release Memory On…• interface hidden:– implement onTrimMemory() callback
(Activity classes)• memory becomes tight:– onTrimMemory() (added in API level 14)– onLowMemory() (for older versions)
Bitmaps• Load the bitmap that your app
needs!– Scale down image to your resolution– Or you need more than 1 resolution per
image
• API_level <= 10:
Bitmaps• Load the bitmap that your app
needs!– Scale down image to your resolution– Or you need more than 1 resolution per
image
• API_level >= 11: (Honeycomb & higher)
Tools & logs– Logs:
• Dalvik Log Messages: All Garbage Collection logs• ART: Logged when GC are deemed slow:
– GC Pauses > 5ms– GC duration > 100ms– Explicit GCs
– Android Device Monitor for tracking Allocations– MAT: Eclipse Memory Analyzer Tool– Monkop: Memory Footprints on real devices
• GC behavior• RAM footprint• Threads• CPU / Network and others
References• http://www.android-app-market.com/android-
architecture.html• https://developer.android.com/training/articles/
memory.html• Android Memory Management
https://gist.github.com/blixtra/ca6e1f289462fe2679af• Google I/O 2011: Memory management for Android
Appshttps://www.youtube.com/watch?v=_CruQY55HOk
• Low Ram – Android 4.4 Optimizationshttp://source.android.com/devices/tech/low-ram.html
Thank you