Top Banner
Memory Management Memory Management in Android in Android Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/... 1 of 25 14-11-19 01:26 PM
25
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Memory Management in Android

Memory ManagementMemory Management

in Androidin Android

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

1 of 25 14-11-19 01:26 PM

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, Opersys inc.

These slides created by: Karim Yaghmour

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

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

2 of 25 14-11-19 01:26 PM

Page 3: Memory Management in Android

AboutAbout

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

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

3 of 25 14-11-19 01:26 PM

Page 4: 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

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

4 of 25 14-11-19 01:26 PM

Page 5: Memory Management in Android

AgendaAgenda

Architecture Recap1. Kernel's role overall2. Kernel driver interfaces3. Kernel user-space interfaces4. Low-memory conditions5. Bionic6. Tools7. App Dev Considerations8. Misc.9.

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

5 of 25 14-11-19 01:26 PM

Page 6: Memory Management in Android

Architecture RecapArchitecture Recap

AOSPSystem startupMemory layout

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

6 of 25 14-11-19 01:26 PM

Page 7: Memory Management in Android

1. AOSP1. AOSP

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

7 of 25 14-11-19 01:26 PM

Page 8: Memory Management in Android

2. System startup2. System startup

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

8 of 25 14-11-19 01:26 PM

Page 9: Memory Management in Android

3. Memory layout3. Memory layout

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

9 of 25 14-11-19 01:26 PM

Page 10: 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

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

10 of 25 14-11-19 01:26 PM

Page 11: 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 */

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

11 of 25 14-11-19 01:26 PM

Page 12: Memory Management in Android

Kernel driver interfacesKernel driver interfaces

Memory-mapped registersDMAMTD mapsION

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

12 of 25 14-11-19 01:26 PM

Page 13: Memory Management in Android

Kernel user-space interfacesKernel user-space interfaces

brkmmap/munmap

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

13 of 25 14-11-19 01:26 PM

Page 14: 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

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

14 of 25 14-11-19 01:26 PM

Page 15: 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

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

15 of 25 14-11-19 01:26 PM

Page 16: 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

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

16 of 25 14-11-19 01:26 PM

Page 17: 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)

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

17 of 25 14-11-19 01:26 PM

Page 18: Memory Management in Android

ToolsTools

KernelNativeDalvik/ARTFramework

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

18 of 25 14-11-19 01:26 PM

Page 19: 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

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

19 of 25 14-11-19 01:26 PM

Page 20: Memory Management in Android

8.2 Native8.2 Native

dumpstatelibrankprocrankprocmemshowmaptombstonesdebuggerdcore files

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

20 of 25 14-11-19 01:26 PM

Page 21: Memory Management in Android

8.3 Dalvik/ART8.3 Dalvik/ART

Heap size measurementAPI in apps to get access to heap sizeMAT/Eclipse

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

21 of 25 14-11-19 01:26 PM

Page 22: Memory Management in Android

8.4. Framework8.4. Framework

dumpsys meminfodumpsys procinfo

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

22 of 25 14-11-19 01:26 PM

Page 23: Memory Management in Android

Misc.Misc.

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

zMAPION

CMA

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

23 of 25 14-11-19 01:26 PM

Page 24: 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

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

24 of 25 14-11-19 01:26 PM

Page 25: Memory Management in Android

Thank You!Thank You!

[email protected]

Memory Management in Android file:///home/karim/opersys-dev/presentations/adc8/...

25 of 25 14-11-19 01:26 PM