![Page 1: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/1.jpg)
Memory ManagementMemory Management
in Androidin Android
Android Builders Summit 2015
![Page 2: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/2.jpg)
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: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/3.jpg)
AboutAbout
![Page 4: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/4.jpg)
Introduced Linux Trace Toolkit in 1999Originated Adeos and relayfs (kernel/relay.c)Ara Android Arch OversightTraining, Custom Dev, Consulting, ...
![Page 5: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/5.jpg)
"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: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/6.jpg)
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: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/7.jpg)
Demo HardwareDemo Hardware
IFC6410Qualcomm Snapdragon S4 Pro – APQ8064Krait CPU, 4-core, 1.7 GHz, 2MB L2 cache2 GB on-board DDR3 (PCDDR 533MHz)4 GB eMMCSeparate power/usbRequires monitor/keyboard/mouse
![Page 8: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/8.jpg)
Architecture RecapArchitecture Recap
AOSPSystem startupMemory layout
![Page 9: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/9.jpg)
1. AOSP1. AOSP
![Page 10: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/10.jpg)
2. System startup2. System startup
![Page 11: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/11.jpg)
3. Memory layout3. Memory layout
![Page 12: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/12.jpg)
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: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/13.jpg)
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: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/14.jpg)
Kernel driver interfacesKernel driver interfaces
Memory-mapped registersDMAMTD mapsION
![Page 15: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/15.jpg)
Kernel user-space interfacesKernel user-space interfaces
brkmmap/munmap
![Page 16: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/16.jpg)
Low-memory conditionsLow-memory conditions
OOM killeroom_adjAndroid low-mem driveroom_adj set during initModifications to oom_adj at runtime by framework
![Page 17: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/17.jpg)
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: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/18.jpg)
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: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/19.jpg)
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: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/20.jpg)
ToolsTools
KernelNativeDalvik/ARTFramework
![Page 21: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/21.jpg)
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: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/22.jpg)
8.2 Native8.2 Native
dumpstatelibrankprocrankprocmemshowmaptombstonesdebuggerdcore files
![Page 23: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/23.jpg)
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: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/24.jpg)
8.4. Framework8.4. Framework
dumpsys meminfodumpsys procinfo
![Page 25: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/25.jpg)
Misc.Misc.
DDOS on memoryKitKat efforts for low-memSecurity aspectsHAL use of mmapSwap space?
zMAPION
CMA
![Page 26: MMeemmoorryy MMaannaaggeemmeenntt iinn AAnnddrrooiidd · AAggeennddaa 1. Architecture Recap 2. Kernel's overall role 3. Kernel driver interfaces 4. Kernel user-space interfaces 5](https://reader034.vdocuments.pub/reader034/viewer/2022042320/5f0a93717e708231d42c4f70/html5/thumbnails/26.jpg)
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