memory management in android

27
Memory Management Memory Management in Android in Android AnDevCon Santa Clara 2015

Upload: opersys-inc

Post on 15-Jan-2017

960 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Memory Management in Android

Memory ManagementMemory Management

in Androidin Android

AnDevCon Santa Clara 2015

Page 2: Memory Management in Android

CC-BY-SA 3.0 - Attribution requirements and misc., PLEASE READ:

This slide must remain as-is in this specific location (slide #1), everything else you are free tochange; including the logo :-)Use of figures in other documents must feature the below "Originals at" URL immediatelyunder that figure and the below copyright notice where appropriate.You are FORBIDDEN from using the default "About" slide as-is or any of its contents.

Copyright (C) 2014-2015, Opersys inc.

These slides created by: Karim Yaghmour

Originals at: http://www.opersys.com/training/

Page 3: Memory Management in Android

AboutAbout

Page 4: Memory Management in Android

Introduced Linux Trace Toolkit in 1999Originated Adeos and relayfs (kernel/relay.c)Ara Android Arch OversightTraining, Custom Dev, Consulting, ...

Page 5: Memory Management in Android

"Note that memory usage on modern operating systems like Linux is an extremelycomplicated and difficult to understand area. In fact the chances of you actuallycorrectly interpreting whatever numbers you get is extremely low. (Pretty muchevery time I look at memory usage numbers with other engineers, there is alwaysa long discussion about what they actually mean that only results in a vagueconclusion.)"

-- Dianne Hackborn, Feb 19, 2010, Stackoverflow

Page 6: Memory Management in Android

AgendaAgenda

Architecture Recap1. Kernel's overall role2. Kernel driver interfaces3. Kernel user-space interfaces4. Low-memory conditions5. Bionic6. App Dev Considerations7. Tools8. Misc.9. References10.

Page 7: Memory Management in Android

Demo HardwareDemo Hardware

Nexus 7 (Flo 2013)Qualcomm Snapdragon S4 Pro -- APQ8064Krait CPU, 4-core, 1.51 GHz, 2MB L2 cache2 GB on-board DDR3 (PCDDR 533MHz)16 GB eMMCCombined power/usb

Page 8: Memory Management in Android

Architecture RecapArchitecture Recap

AOSPSystem startupMemory layout

Page 9: Memory Management in Android

1. AOSP1. AOSP

Page 10: Memory Management in Android

2. System startup2. System startup

Page 11: Memory Management in Android

3. Memory layout3. Memory layout

Page 12: Memory Management in Android

Kernel's overall roleKernel's overall role

Manage physical memoryManage virtual-to-physical mappingsProcess isolation:

Protect the hardware from user-spaceProtect processes from each other

Driver capabilities:Memory-mapped registersDMAMTD maps

Filesystem cacheFile mappingSwapping

Page 13: Memory Management in Android

InternalsInternals

Architecture-independent:

Architecture-specific:

Per-task struct (include/linux/sched.h):

The memory structures (include/linux/mm_types.h):

mm/

arch/arm/mm/

struct task_struct {...

struct mm_struct *mm, *active_mm;...

struct mm_struct {struct vm_area_struct * mmap; /* list of VMAs */struct rb_root mm_rb;struct vm_area_struct * mmap_cache; /* last find_vma result */

Page 14: Memory Management in Android

Kernel driver interfacesKernel driver interfaces

Memory-mapped registersDMAMTD mapsION

Page 15: Memory Management in Android

Kernel user-space interfacesKernel user-space interfaces

brkmmap/munmap

Page 16: Memory Management in Android

Low-memory conditionsLow-memory conditions

OOM killeroom_adjAndroid low-mem driveroom_adj set during initModifications to oom_adj at runtime by framework

Page 17: Memory Management in Android

BionicBionic

malloc()/free()Comes from Doug Lea's dlmallocPublic DomainSee bionic/libc/upstream-dlmalloc/Tutorial/doc:

Dates back to 1987Uses CALL_MORECORE() macro do allocations

Based on sbrk()dlopen()/dlsym()

http://g.oswego.edu/dl/html/malloc.htmlhttps://en.wikipedia.org/wiki/C_dynamic_memory_allocation

Page 18: Memory Management in Android

Flags to debug/observe malloc/free Linux

Enable native monitoring by DDMS:

Open ~/.android/ddms.cfgAdd line stating: "native=true"

$ adb shell setprop libc.debug.malloc 1$ adb shell stop$ adb shell start

Page 19: Memory Management in Android

App Dev ConsiderationsApp Dev Considerations

Recommendations given by Google

Measuring app mem usage

Getting system mem usage

android.os.Debugandroid:largeHeap="true"

https://developer.android.com/training/articles/memory.html

https://developer.android.com/reference/android/app/ActivityManager.html#getProcessMemoryInfo%28int[]%29

public MemoryInfo[] getProcessMemoryInfo (int[] pids)

https://developer.android.com/reference/android/app/ActivityManager.html#getMemoryInfo%28android.app.ActivityManager.MemoryInfo%29

public void getMemoryInfo (ActivityManager.MemoryInfo outInfo)

Page 20: Memory Management in Android

ToolsTools

KernelNativeDalvik/ARTFramework

Page 21: Memory Management in Android

8.1 Kernel8.1 Kernel

Overall memory usePhysical-mapped reg address rangesFS cache"fragmentation"/proc interface

RSS, VSS, USS, PSS, etc./proc/[pid]/maps, semantics of

Page 22: Memory Management in Android

8.2 Native8.2 Native

dumpstatelibrankprocrankprocmemshowmaptombstonesdebuggerdcore files

Page 23: Memory Management in Android

8.3 Dalvik/ART8.3 Dalvik/ART

Heap size measurementAPI in apps to get access to heap size from Runtime

MAT/Eclipsedalvik.vm.heapsize

https://developer.android.com/reference/java/lang/Runtime.html#maxMemory%28%29

public long maxMemory ()

Page 24: Memory Management in Android

8.4. Framework8.4. Framework

dumpsys meminfodumpsys procinfo

Page 25: Memory Management in Android

Misc.Misc.

DDOS on memoryKitKat efforts for low-memSecurity aspectsHAL use of mmapSwap space?

zMAPION

CMA

Page 26: Memory Management in Android

ReferencesReferences

developer.android.comsource.android.com

strong/weak/soft/phantom references wrt Java GC: - -

-->

http://source.android.com/devices/native-memory.htmlhttp://source.android.com/devices/low-ram.htmlhttps://developer.android.com/tools/debugging/debugging-memory.htmlhttps://developer.android.com/training/articles/memory.htmlhttp://android-developers.blogspot.com/2011/03/memory-analysis-for-android.htmlhttp://android-developers.blogspot.com/2009/02/track-memory-allocations.htmlhttps://stackoverflow.com/questions/2298208/how-to-discover-memory-usage-of-my-application-in-androidhttp://elinux.org/Android_Memory_Usagehttps://www.youtube.com/watch?v=_CruQY55HOkhttp://blog.yojimbocorp.com/2012/10/03/view-android-application-memory-usage-with-eclipse-ddms-plugin/https://lwn.net/Articles/480055/https://lwn.net/Articles/565469/

https://weblogs.java.net/blog/2006/05/04/understanding-weak-references http://docs.oracle.com/javase/7/docs/api/java/lang/ref/package-summary.html

Page 27: Memory Management in Android

Thank You!Thank You!

[email protected]