38
Fabián Baptista @fbaptista Android Memory Management

Memory management in Andoid

Embed Size (px)

Citation preview

Page 1: Memory management in Andoid

Fabián Baptista @fbaptista

Android Memory Management

Page 2: Memory management in Andoid

Fabián Baptista @fbaptista

Android Memory Management

Page 3: Memory management in Andoid

Talk about…Android ArchitectureConcepts

How it works

Tools and logs

Page 4: Memory management in Andoid

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

Page 5: Memory management in Andoid

Architecture

Page 6: Memory management in Andoid

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.

Page 7: Memory management in Andoid

Architecture

Page 8: Memory management in Andoid

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

Page 9: Memory management in Andoid

Architecture

Page 10: Memory management in Andoid

Virtual Memory

• Maps process memory addresses (virtual) into physical addresses.

• Hardware: Address translation inside CPU (called MMU)

• Software: Extend physical memory size

Page 11: Memory management in Andoid

Virtual Memory - Key Benefits• Avoid process to handle a shared

space.• More security & isolation• More memory available• Easier programming / hidden

fragmentation

Page 12: Memory management in Andoid

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

Page 13: Memory management in Andoid

Garbage Collection

Page 14: Memory management in Andoid

Garbage Collection

Page 15: Memory management in Andoid

Android Way• Memory:– No Swap space, Paging–Memory mapping

• Shared Memory– Framework code & resources– Intercommunication (processes)– Intensively used

Page 16: Memory management in Andoid

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.

Page 17: Memory management in Andoid

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

Page 18: Memory management in Andoid

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%

Page 19: Memory management in Andoid

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

Page 20: Memory management in Andoid

Memory Numbers…

Page 21: Memory management in Andoid

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

Page 22: Memory management in Andoid

Memory Numbers…

Page 23: Memory management in Andoid

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

Page 24: Memory management in Andoid

Memory Numbers…

Page 25: Memory management in Andoid

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)

Page 26: Memory management in Andoid

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

Page 27: Memory management in Andoid

Low Memory Killer (Android)

Threshold varies based on RAM size. (Loaded during booting time from init.rc file)

Memory Pages(4KB each)

Page 28: Memory management in Andoid

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

Page 29: Memory management in Andoid

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

Page 30: Memory management in Andoid

Services State

Started• startService()

Bound• bindService()

Page 31: Memory management in Andoid

Services Started> startService();

• Can run indefinitely• Even if the activity who started it is destroyed

• Once it is finished, the service should stop itself

Page 32: Memory management in Andoid

Services Bound> bindService();

• Client / Server interface to:• Request service• Get results• IPC

• Bind by one• Is destroyed only when all of them unbind

Page 33: Memory management in Andoid

Release Memory On…• interface hidden:– implement onTrimMemory() callback

(Activity classes)• memory becomes tight:– onTrimMemory() (added in API level 14)– onLowMemory() (for older versions)

Page 34: Memory management in Andoid

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:

Page 35: Memory management in Andoid

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)

Page 36: Memory management in Andoid

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

Page 37: Memory management in Andoid

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

Page 38: Memory management in Andoid

Thank you