SlideShare uma empresa Scribd logo
1 de 35
Baixar para ler offline
Tuning Android for low RAM 
when 1 GiB is too much 
Chris Simmonds 
Tuning Android for low RAM 1 Copyright © 2011-2014, 2net Ltd
License 
These slides are available under a Creative Commons Attribution-ShareAlike 3.0 
license. You can read the full text of the license here 
http://creativecommons.org/licenses/by-sa/3.0/legalcode 
You are free to 
• copy, distribute, display, and perform the work 
• make derivative works 
• make commercial use of the work 
Under the following conditions 
• Attribution: you must give the original author credit 
• Share Alike: if you alter, transform, or build upon this work, you may distribute 
the resulting work only under a license identical to this one (i.e. include this 
page exactly as it is) 
• For any reuse or distribution, you must make clear to others the license terms of 
this work 
Tuning Android for low RAM 2 Copyright © 2011-2014, 2net Ltd
About Chris Simmonds 
• Consultant and trainer 
• Working with embedded Linux since 1999 
• Android since 2009 
• Speaker at many conferences and 
workshops 
"Looking after the Inner Penguin" blog at http://2net.co.uk/ 
https://uk.linkedin.com/in/chrisdsimmonds/ 
https://google.com/+chrissimmonds 
Tuning Android for low RAM 3 Copyright © 2011-2014, 2net Ltd
Overview 
• Project Svelte 
• How much RAM do you need? 
• Tuning Android 
• Global tuning knob: "ro.config.low_ram" 
• The Dalvik JIT cache 
• The OnTrimMemory application callback 
• Tuning the kernel 
• Kernel Samepage Merging (KSM) 
• Swap to compressed RAM 
Tuning Android for low RAM 4 Copyright © 2011-2014, 2net Ltd
The problem 
• Since Gingerbread minimum RAM has gone beyond 
512 MiB 
• Especially since "project Butter" (Jelly Bean 4.1) 
which improved graphics performance by adding 
more buffers (among other changes) 
• But there is still a desire to run Android on such 
"low-end" hardware 
• Cheap smartphones 
• Wearables: e.g. the current generation of 
smartwatches 
• "Embedded Android" 
Tuning Android for low RAM 5 Copyright © 2011-2014, 2net Ltd
Project Svelte 
• Kit Kat 4.4 introduced "project Svelte": Android on 
devices with 512 MiB RAM 
• Project Svelte consists of 
• Various memory-saving changes to Android 
framework 
• Tuning knobs for Android 
• Validated techniques for tuning Linux 
• Improved memory diagnostics 
• See source.android.com/devices/low-ram.html 
• Note: In many cases there is a trade-off between 
reducing memory use and increasing CPU load (and 
therefore increasing power requirements) 
Tuning Android for low RAM 6 Copyright © 2011-2014, 2net Ltd
How much RAM am I using? 
• Tricky question! 
• Some (most) is used by processes: apps and system 
daemons 
• But note that processes share a lot of read-only data 
• Some is cached 
• But caches can be dropped, so cached memory is 
usually regarded as "free" 
• Some is allocated by the kernel and not owned by 
any process 
• Some is used for the code and data segments of the 
kernel and kernel modules 
Tuning Android for low RAM 7 Copyright © 2011-2014, 2net Ltd
Memory metrics for processes 
• For each Linux process we can measure 
• Vss = virtual set size: pages mapped by this process 
• Rss = resident set size: pages mapped to real 
memory 
• Uss = unique set size: pages of memory not shared 
with any other process 
• Pss = proportional set size: pages shared with other 
processes, divided by the number of processes 
sharing 
• Perhaps a diagram would help... 
Tuning Android for low RAM 8 Copyright © 2011-2014, 2net Ltd
Memory metrics 
P1 P2 
Not 
committed 
Shared, 
committed 
Not shared, 
C 
B 
A committed 
Vss = A + B + C 
Rss = A + B 
Uss = A 
Pss = A + B/n where n is the number of processes sharing 
Tuning Android for low RAM 9 Copyright © 2011-2014, 2net Ltd
How to calculate Pss 
P1 
C 
B 
A 
P2 P3 
3 
2 
3 
2 
3 
2 
3 
2 
2 
3 3 
Pss(1) = 2 + 3/3 + 2/2 = 4 
Pss(2) = 2 + 3/3 + 2/2 = 4 
Pss(3) = 2 + 3/3 = 3 
Sum(Pss) = 11 = total of pages in use 
Tuning Android for low RAM 10 Copyright © 2011-2014, 2net Ltd
Tools: procrank 
Part of the Android tool set for a long time: ranks 
processes by Pss (default), type procrank -h for more 
options 
Example (edited): 
# procrank 
PID Vss Rss Pss Uss cmdline 
3351 1058776K 163952K 141197K 139596K com.google.earth 
2616 943156K 116020K 93360K 91724K com.android.vending 
539 990756K 112504K 91393K 89808K com.android.systemui 
4657 995760K 105964K 77829K 70776K com.rovio.angrybirds 
... 
119 31904K 7676K 6038K 5900K /system/bin/surfaceflinger 
122 27468K 3788K 3045K 2964K /system/bin/mediaserver 
... 
120 865084K 24308K 2263K 860K zygote 
... 
------ ------ ------ 
717098K 669272K TOTAL 
RAM: 1124832K total, 105528K free, 3808K buffers, 136624K cached, 
656K shmem, 23656K slab 
Tuning Android for low RAM 11 Copyright © 2011-2014, 2net Ltd
Tools: procmem 
Another tried and tested tool: shows Vss, Rss, etc for 
each mapping of a single process 
Example (edited): 
# procmem 119 
Vss Rss Pss Uss ShCl ShDi PrCl PrDi Name 
------- ------- ------- ------- ------- ------- ------- ------- 
4K 0K 0K 0K 0K 0K 0K 0K 
1012K 4K 4K 4K 0K 0K 4K 0K [stack:944] 
512K 512K 512K 512K 0K 0K 36K 476K /dev/mali0 
512K 512K 512K 512K 0K 0K 0K 512K /dev/mali0 
516K 12K 12K 12K 0K 0K 12K 0K [anon:libc_malloc] 
512K 512K 512K 512K 0K 0K 224K 288K /dev/mali0 
512K 512K 512K 512K 0K 0K 32K 480K /dev/mali0 
516K 12K 12K 12K 0K 0K 12K 0K [anon:libc_malloc] 
... 
2680K 2668K 2668K 2668K 0K 0K 2668K 0K [heap] 
132K 20K 20K 20K 0K 0K 20K 0K [stack] 
0K 0K 0K 0K 0K 0K 0K 0K [vectors] 
------- ------- ------- ------- ------- ------- ------- ------- 
31904K 7676K 6039K 5900K 1760K 16K 4144K 1760K TOTAL 
Tuning Android for low RAM 12 Copyright © 2011-2014, 2net Ltd
The Android application life cycle 
• Activity Manager grades applications by how many 
components (activities and services) are being used 
• Sets a per-process measure called oom_adj 
• oom_adj values are from -16 (important process) to 
15 (unimportant process) 
• As memory pressure increases, the kernel low 
memory killer starts killing processes starting with the 
highest oom_adj 
Tuning Android for low RAM 13 Copyright © 2011-2014, 2net Ltd
Values for oom_adj 
From frameworks/base/services/java/com/android/ 
server/am/ProcessList.java 
State oom_adj Type of process 
System -16 daemons and system services 
Persistent -12 persistent apps, e.g. telephony 
Foreground 0 contains the foreground activity 
Visible 1 contains activities that are visible 
Perceptible 2 e.g. background music playback 
Service 5 contains an application service 
Home 6 contains the home application 
Previous 7 the previous foreground application 
B Services 8 "old and decrepit services" 
Cached 9..15 all activities and services de-stroyed 
Tuning Android for low RAM 14 Copyright © 2011-2014, 2net Ltd
lowmemory killer thresholds 
• Thresholds calculated according to screen size and 
total memory 
• Example (from Nexus 10) 
oom_adj Threshold (KiB) 
-16 49152 
-12 49152 
0 49152 
1 61440 
2 73728 
3 86016 
4 98304 
5 98304 
6 98304 
7 98304 
8 98304 
9 98304 
15 122880 
Tuning Android for low RAM 15 Copyright © 2011-2014, 2net Ltd
Tools: meminfo 
dumpsys meminfo takes the oom_adj value into 
account: 
# dumpsys meminfo 
Applications Memory Usage (kB): 
Uptime: 5156998 Realtime: 70066043 
Total PSS by process: 
141263 kB: com.google.earth (pid 3351 / activities) 
93354 kB: com.android.vending (pid 2616 / activities) 
92554 kB: com.android.systemui (pid 539) 
... 
Total PSS by OOM adjustment: 
19794 kB: Native 
6031 kB: surfaceflinger (pid 119) 
... 
36427 kB: System 
36427 kB: system (pid 444) 
... 
101001 kB: Persistent 
92554 kB: com.android.systemui (pid 539) 
... 
362721 kB: Cached 
141263 kB: com.google.earth (pid 3351 / activities) 
... 
Total RAM: 1124832 kB 
Free RAM: 633617 kB (362721 cached pss + 138452 cached + 132444 free) 
Used RAM: 352407 kB (323895 used pss + 4304 buffers + 656 shmem + 23552 slab) 
Lost RAM: 138808 kB 
Tuning: 192 (large 512), oom 122880 kB, restore limit 40960 kB (high-end-gfx) 
Tuning Android for low RAM 16 Copyright © 2011-2014, 2net Ltd
Tools: meminfo 
• In Kit Kat, dumpsys meminfo has been augmented to 
make the use of memory more clear 
• Processes with oom_adj >= 9 (CACHED_APP_MIN_ADJ) 
can be killed without the user noticing 
• So Free RAM includes apps that can be discarded 
("cached pss") and system buffers ("cached") 
Tuning Android for low RAM 17 Copyright © 2011-2014, 2net Ltd
Tools: procstats 
• procstats adds history to the measurement by 
integrating Pss over time 
• Use to identify persistent memory hogs 
• Typically shows up apps with long-running 
background services 
• procstats has a nice graphical interface, and can be 
run from the command line 
Tuning Android for low RAM 18 Copyright © 2011-2014, 2net Ltd
procstats 
Settings -> Developer options -> Process Stats 
• Bar is a summary of memory 
pressure: green=good, 
yellow=OK, red=bad 
• For each app, shows 
• % of time it was running 
• a blue bar which is (average 
Pss * runtime) 
Tuning Android for low RAM 19 Copyright © 2011-2014, 2net Ltd
procstats 
Zoom in on second app: 
• Contains a service 
FlightUpdateService 
• Has been running 100% of 
the time 
• Is taking 33 MiB 
Tuning Android for low RAM 20 Copyright © 2011-2014, 2net Ltd
procstats command-line 
• The raw data is available through system service 
procstats 
• Dump the data using 
# dumpsys procstats 
AGGREGATED OVER LAST 24 HOURS: 
* system / 1000: 
TOTAL: 100% (22MB-31MB-35MB/19MB-29MB-33MB over 89) 
Persistent: 100% (22MB-31MB-35MB/19MB-29MB-33MB over 89) 
* com.android.systemui / u0a6: 
TOTAL: 100% (36MB-59MB-99MB/34MB-57MB-97MB over 89) 
Persistent: 100% (36MB-59MB-99MB/34MB-57MB-97MB over 89) 
Imp Fg: 0.00% 
The memory numbers are 
minPss-avgPss-maxPss / minUss-avgUss-maxUss 
Tuning Android for low RAM 21 Copyright © 2011-2014, 2net Ltd
Tuning Android for low RAM 
What are the options? 
• Tune Activity manager 
• Tune Dalvik 
• Tune Apps 
Tuning Android for low RAM 22 Copyright © 2011-2014, 2net Ltd
Tuning Android for low RAM 
• Kit Kat has a global tuning parameter for low RAM 
ro.config.low_ram 
• If set to true: 
• Optimise allocations in Dalvik heap 
• Saves memory by reducing use of the GPU 
• New API ActivityManager.isLowRamDevice() returns 
true which apps can use as a hint that they should 
reduce memory usage: some Google apps are 
reportedly coded to make this check 
• Reduces the Dalvik total PSS by 10 - 15% on devices 
with large bitmaps (such as Nexus 7 or 10) 
Tuning Android for low RAM 23 Copyright © 2011-2014, 2net Ltd
Tuning Dalvik 
• The Dalvik JIT cache defaults to 1.5 MiB per app (on 
ARMv7a): a typical app uses 100 KiB to 200 KiB 
• You can reduce it, but if set too low will send the JIT 
into a thrashing mode 
• For really low-memory devices it is better to disable 
JIT completely by setting cache size to zero 
BoardConfig.mk: 
PRODUCT_PROPERTY_OVERRIDES += dalvik.vm.jit.codecachesize=0 
• Saving: 3 MiB to 6 MiB 
Tuning Android for low RAM 24 Copyright © 2011-2014, 2net Ltd
Tuning applications 
• Generally, dereference objects that are not being 
used so they can be garbage-collected 
• Especially, implement void onTrimMemory (int level) 
and free memory as indicated 
• level is one of TRIM_MEMORY_COMPLETE, 
TRIM_MEMORY_MODERATE, TRIM_MEMORY_BACKGROUND, 
TRIM_MEMORY_UI_HIDDEN, 
TRIM_MEMORY_RUNNING_CRITICAL, 
TRIM_MEMORY_RUNNING_LOW, or 
TRIM_MEMORY_RUNNING_MODERATE 
Tuning Android for low RAM 25 Copyright © 2011-2014, 2net Ltd
Wallpaper 
• Ensure the default wallpaper setup on launcher is not 
live wallpaper 
• Do not pre-install any live wallpapers 
Tuning Android for low RAM 26 Copyright © 2011-2014, 2net Ltd
Tuning Linux for low RAM 
What are the options? 
• extra_free_kbytes 
• KSM 
• Swap to compressed RAM 
Tuning Android for low RAM 27 Copyright © 2011-2014, 2net Ltd
Linux memory reclaim 
• Background reclaim is done by the kswap daemon 
• Started when free memory drops below a threshold: 
2MB on a 2GB device and 636KB on a 512MB 
• Aims to keep some memory free by flushing dirty 
pages to disk (or invoking the low memory killer) 
• Direct reclaim happens when a process tries to 
allocate memory and there are no free pages 
• blocks the calling thread while pages are freed 
• Direct reclaim is bad because it can freeze the UI 
thread, leading to a poor UX 
Tuning Android for low RAM 28 Copyright © 2011-2014, 2net Ltd
extra_free_kbytes 
• Default kswapd threshold is rather low for Android 
devices 
• /proc/sys/vm/extra_free_kbytes is a tuneable added by 
Google to Linux 3.4 to modify the kswapd threshold 
• If set to 0 (default), Activity Manager will adjust it to 3 
x screen buffer 
• Can be configured in platform config.xml 
frameworks/base/core/res/res/values/config.xml 
• config_extraFreeKbytesAbsolute overrides the default 
chosen by Activity Manager: -1 keeps the default 
• config_extraFreeKbytesAdjust added (subtracted if 
negative) from the value chosen by Activity Manager 
Tuning Android for low RAM 29 Copyright © 2011-2014, 2net Ltd
Kernel Samepage Merging (KSM) 
• KSM is a kernel thread (ksmd) that runs in the 
background and compares pages in memory that 
have been marked MADV_MERGEABLE by 
user-space 
• If two pages are found to be the same, it merges 
them back to a single copy-on-write page 
• Balancing reduced memory usage vs more 
processing (greater power demand) 
• Benefit depends on workload 
Tuning Android for low RAM 30 Copyright © 2011-2014, 2net Ltd
KSM controls 
• Build kernel with CONFIG_KSM=y (Linux 2.6.32 or later) 
• Controlled by these files in /sys/kernel/mm/ksm 
File default Description 
run 0 Start ksmd thread if non-zero 
sleep_millisecs 500 ms between scans 
pages_to_scan 100 pages per scan 
• Typically you add lines to your init.[name].rc to set up 
KSM 
Tuning Android for low RAM 31 Copyright © 2011-2014, 2net Ltd
Does KSM work? 
• Also in /sys/kernel/mm/ksm 
File Description 
full_scans # times all mergeable pages have been scanned 
pages_shared # shared pages are being used 
pages_sharing # more sites are sharing them i.e. how much 
saved 
pages_unshared # pages unique but repeatedly checked for merg-ing 
pages_volatile # pages changing too fast to be merged 
• And, at the end of dumpsys meminfo: 
# dumpsys meminfo 
... 
KSM: 33992 kB saved from shared 4216 kB 
234796 kB unshared; 532028 kB volatile 
• Typical saving: about 10% of used RAM 
Tuning Android for low RAM 32 Copyright © 2011-2014, 2net Ltd
Compressed swap area 
• Use a compressed RAM swap area, zram, for swap 
• Unused dirty pages can be swapped out and 
compressed 
• Compression ratios in the 30-50% range are usually 
observed 
• Once again, you are balancing reduced memory 
usage vs more processing (greater power demand) 
Tuning Android for low RAM 33 Copyright © 2011-2014, 2net Ltd
Configuring zram 
• Add to kernel config 
CONFIG_SWAP 
CONFIG_CGROUP_MEM_RES_CTLR 
CONFIG_CGROUP_MEM_RES_CTLR_SWAP 
CONFIG_ZRAM 
• Add to /fstab.[product name]: 
/dev/block/zram0 none swap defaults 
zramsize=<size in bytes>,swapprio=<swap partition priority> 
• Add to /init.[product name].rc: 
swapon_all /fstab.[product name] 
Tuning Android for low RAM 34 Copyright © 2011-2014, 2net Ltd
• Questions? 
Slides on Slide Share: http://www.slideshare.net/ 
chrissimmonds/android-lowmemoryabs2014 
Tuning Android for low RAM 35 Copyright © 2011-2014, 2net Ltd

Mais conteúdo relacionado

Mais procurados

Windows power management basic knowledge
Windows power management basic knowledgeWindows power management basic knowledge
Windows power management basic knowledge
Yonghong(Dave) Feng
 

Mais procurados (20)

Linux Internals - Part I
Linux Internals - Part ILinux Internals - Part I
Linux Internals - Part I
 
LAS16-105: Walkthrough of the EAS kernel adaptation to the Android Common Kernel
LAS16-105: Walkthrough of the EAS kernel adaptation to the Android Common KernelLAS16-105: Walkthrough of the EAS kernel adaptation to the Android Common Kernel
LAS16-105: Walkthrough of the EAS kernel adaptation to the Android Common Kernel
 
Linux Preempt-RT Internals
Linux Preempt-RT InternalsLinux Preempt-RT Internals
Linux Preempt-RT Internals
 
Kernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime Ripard
Kernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime RipardKernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime Ripard
Kernel Recipes 2017 - An introduction to the Linux DRM subsystem - Maxime Ripard
 
Android Treble: Blessing or Trouble?
Android Treble: Blessing or Trouble?Android Treble: Blessing or Trouble?
Android Treble: Blessing or Trouble?
 
Scheduling in Android
Scheduling in AndroidScheduling in Android
Scheduling in Android
 
Embedded Linux BSP Training (Intro)
Embedded Linux BSP Training (Intro)Embedded Linux BSP Training (Intro)
Embedded Linux BSP Training (Intro)
 
Embedded Android : System Development - Part II (HAL)
Embedded Android : System Development - Part II (HAL)Embedded Android : System Development - Part II (HAL)
Embedded Android : System Development - Part II (HAL)
 
Embedded Android : System Development - Part I
Embedded Android : System Development - Part IEmbedded Android : System Development - Part I
Embedded Android : System Development - Part I
 
Qemu Introduction
Qemu IntroductionQemu Introduction
Qemu Introduction
 
Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device drivers
 
Embedded Android : System Development - Part III
Embedded Android : System Development - Part IIIEmbedded Android : System Development - Part III
Embedded Android : System Development - Part III
 
Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)Embedded Android : System Development - Part II (Linux device drivers)
Embedded Android : System Development - Part II (Linux device drivers)
 
introduction to linux kernel tcp/ip ptocotol stack
introduction to linux kernel tcp/ip ptocotol stack introduction to linux kernel tcp/ip ptocotol stack
introduction to linux kernel tcp/ip ptocotol stack
 
Android crash debugging
Android crash debuggingAndroid crash debugging
Android crash debugging
 
Windows power management basic knowledge
Windows power management basic knowledgeWindows power management basic knowledge
Windows power management basic knowledge
 
Linux device drivers
Linux device drivers Linux device drivers
Linux device drivers
 
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKBLinux Kernel Booting Process (1) - For NLKB
Linux Kernel Booting Process (1) - For NLKB
 
Android Internals
Android InternalsAndroid Internals
Android Internals
 
BusyBox for Embedded Linux
BusyBox for Embedded LinuxBusyBox for Embedded Linux
BusyBox for Embedded Linux
 

Destaque

Tuning android for low ram devices
Tuning android for low ram devicesTuning android for low ram devices
Tuning android for low ram devices
Droidcon Berlin
 
Александр Терещук - Memory Analyzer Tool and memory optimization tips in Android
Александр Терещук - Memory Analyzer Tool and memory optimization tips in AndroidАлександр Терещук - Memory Analyzer Tool and memory optimization tips in Android
Александр Терещук - Memory Analyzer Tool and memory optimization tips in Android
UA Mobile
 
Process control daemon
Process control daemonProcess control daemon
Process control daemon
haish
 
Android Performance Best Practices
Android Performance Best Practices Android Performance Best Practices
Android Performance Best Practices
Amgad Muhammad
 
Crash_Report_Mechanism_In_Tizen
Crash_Report_Mechanism_In_TizenCrash_Report_Mechanism_In_Tizen
Crash_Report_Mechanism_In_Tizen
Lex Yu
 

Destaque (20)

Tuning android for low ram devices
Tuning android for low ram devicesTuning android for low ram devices
Tuning android for low ram devices
 
Александр Терещук - Memory Analyzer Tool and memory optimization tips in Android
Александр Терещук - Memory Analyzer Tool and memory optimization tips in AndroidАлександр Терещук - Memory Analyzer Tool and memory optimization tips in Android
Александр Терещук - Memory Analyzer Tool and memory optimization tips in Android
 
Linux memory consumption
Linux memory consumptionLinux memory consumption
Linux memory consumption
 
Android Memory , Where is all My RAM
Android Memory , Where is all My RAM Android Memory , Where is all My RAM
Android Memory , Where is all My RAM
 
Memory in Android
Memory in AndroidMemory in Android
Memory in Android
 
Android memory fundamentals
Android memory fundamentalsAndroid memory fundamentals
Android memory fundamentals
 
Poster_Jan
Poster_JanPoster_Jan
Poster_Jan
 
Process control daemon
Process control daemonProcess control daemon
Process control daemon
 
Linux memory-management-kamal
Linux memory-management-kamalLinux memory-management-kamal
Linux memory-management-kamal
 
Memory management in Andoid
Memory management in AndoidMemory management in Andoid
Memory management in Andoid
 
Android Performance Best Practices
Android Performance Best Practices Android Performance Best Practices
Android Performance Best Practices
 
Linux Memory Management
Linux Memory ManagementLinux Memory Management
Linux Memory Management
 
Software update for IoT Embedded World 2017
Software update for IoT Embedded World 2017Software update for IoT Embedded World 2017
Software update for IoT Embedded World 2017
 
Jollen's Presentation: Introducing Android low-level
Jollen's Presentation: Introducing Android low-levelJollen's Presentation: Introducing Android low-level
Jollen's Presentation: Introducing Android low-level
 
LCA13: Memory Hotplug on Android
LCA13: Memory Hotplug on AndroidLCA13: Memory Hotplug on Android
LCA13: Memory Hotplug on Android
 
Stan winston & animatronics
Stan winston & animatronicsStan winston & animatronics
Stan winston & animatronics
 
Debugging Native heap OOM - JavaOne 2013
Debugging Native heap OOM - JavaOne 2013Debugging Native heap OOM - JavaOne 2013
Debugging Native heap OOM - JavaOne 2013
 
Android Rooting and Flashing
Android Rooting and FlashingAndroid Rooting and Flashing
Android Rooting and Flashing
 
Android Development Tools
Android Development ToolsAndroid Development Tools
Android Development Tools
 
Crash_Report_Mechanism_In_Tizen
Crash_Report_Mechanism_In_TizenCrash_Report_Mechanism_In_Tizen
Crash_Report_Mechanism_In_Tizen
 

Semelhante a Tuning Android for low RAM

ABS 2014 - Android Kit Kat Internals
ABS 2014 - Android Kit Kat InternalsABS 2014 - Android Kit Kat Internals
ABS 2014 - Android Kit Kat Internals
Benjamin Zores
 

Semelhante a Tuning Android for low RAM (20)

Reducing the boot time of Linux devices
Reducing the boot time of Linux devicesReducing the boot time of Linux devices
Reducing the boot time of Linux devices
 
z/VM Performance Analysis
z/VM Performance Analysisz/VM Performance Analysis
z/VM Performance Analysis
 
Android beyond the smartphone
Android beyond the smartphoneAndroid beyond the smartphone
Android beyond the smartphone
 
Explore Android Internals
Explore Android InternalsExplore Android Internals
Explore Android Internals
 
Android As a Server- Building Android for the Cloud (AnDevCon SF 2013)
Android As a Server- Building Android for the Cloud (AnDevCon SF 2013)Android As a Server- Building Android for the Cloud (AnDevCon SF 2013)
Android As a Server- Building Android for the Cloud (AnDevCon SF 2013)
 
Building Android for the Cloud: Android as a Server (Mobile World Congress 2014)
Building Android for the Cloud: Android as a Server (Mobile World Congress 2014)Building Android for the Cloud: Android as a Server (Mobile World Congress 2014)
Building Android for the Cloud: Android as a Server (Mobile World Congress 2014)
 
Abs2014 kitkatinternals-212-phpapp01
Abs2014 kitkatinternals-212-phpapp01Abs2014 kitkatinternals-212-phpapp01
Abs2014 kitkatinternals-212-phpapp01
 
CollabSphere 2020 - INF105 - HCL Notes 11.0.1 FP1 - Performance Boost Re-Relo...
CollabSphere 2020 - INF105 - HCL Notes 11.0.1 FP1 - Performance Boost Re-Relo...CollabSphere 2020 - INF105 - HCL Notes 11.0.1 FP1 - Performance Boost Re-Relo...
CollabSphere 2020 - INF105 - HCL Notes 11.0.1 FP1 - Performance Boost Re-Relo...
 
CollabSphere 2020 Live - HCL Notes 11.0.1 FP1 - Performance Boost Re-Reloaded
CollabSphere 2020 Live - HCL Notes 11.0.1 FP1 - Performance Boost Re-ReloadedCollabSphere 2020 Live - HCL Notes 11.0.1 FP1 - Performance Boost Re-Reloaded
CollabSphere 2020 Live - HCL Notes 11.0.1 FP1 - Performance Boost Re-Reloaded
 
Reducing boot time in embedded Linux
Reducing boot time in embedded LinuxReducing boot time in embedded Linux
Reducing boot time in embedded Linux
 
RNUG 2020: HCL Notes 11.0.1 FP2 - Performance Boost Re-Reloaded
RNUG 2020: HCL Notes 11.0.1 FP2 - Performance Boost Re-ReloadedRNUG 2020: HCL Notes 11.0.1 FP2 - Performance Boost Re-Reloaded
RNUG 2020: HCL Notes 11.0.1 FP2 - Performance Boost Re-Reloaded
 
RNUG - HCL Notes 11.0.1 FP2 — Performance Boost Re-Reloaded
RNUG - HCL Notes 11.0.1 FP2 — Performance Boost Re-ReloadedRNUG - HCL Notes 11.0.1 FP2 — Performance Boost Re-Reloaded
RNUG - HCL Notes 11.0.1 FP2 — Performance Boost Re-Reloaded
 
Analyzing OS X Systems Performance with the USE Method
Analyzing OS X Systems Performance with the USE MethodAnalyzing OS X Systems Performance with the USE Method
Analyzing OS X Systems Performance with the USE Method
 
Introduction to Embedded Systems
Introduction to Embedded SystemsIntroduction to Embedded Systems
Introduction to Embedded Systems
 
XPDDS18: Design and Implementation of Automotive: Virtualization Based on Xen...
XPDDS18: Design and Implementation of Automotive: Virtualization Based on Xen...XPDDS18: Design and Implementation of Automotive: Virtualization Based on Xen...
XPDDS18: Design and Implementation of Automotive: Virtualization Based on Xen...
 
Final presentasi gnome asia
Final presentasi gnome asiaFinal presentasi gnome asia
Final presentasi gnome asia
 
ABS 2014 - Android Kit Kat Internals
ABS 2014 - Android Kit Kat InternalsABS 2014 - Android Kit Kat Internals
ABS 2014 - Android Kit Kat Internals
 
Building the Case for System z Linux
Building the Case for System z LinuxBuilding the Case for System z Linux
Building the Case for System z Linux
 
AdminCamp 2018 - IBM Notes V10 Performance Boost
AdminCamp 2018 - IBM Notes V10 Performance BoostAdminCamp 2018 - IBM Notes V10 Performance Boost
AdminCamp 2018 - IBM Notes V10 Performance Boost
 
Visão geral do hardware do servidor System z e Linux on z - Concurso Mainframe
Visão geral do hardware do servidor System z e Linux on z - Concurso MainframeVisão geral do hardware do servidor System z e Linux on z - Concurso Mainframe
Visão geral do hardware do servidor System z e Linux on z - Concurso Mainframe
 

Mais de Chris Simmonds

Linux field-update-2015
Linux field-update-2015Linux field-update-2015
Linux field-update-2015
Chris Simmonds
 

Mais de Chris Simmonds (16)

Debugging embedded devices using GDB
Debugging embedded devices using GDBDebugging embedded devices using GDB
Debugging embedded devices using GDB
 
Debian or Yocto Project? Which is the best for your Embedded Linux project?
Debian or Yocto Project? Which is the best for your Embedded Linux project?Debian or Yocto Project? Which is the best for your Embedded Linux project?
Debian or Yocto Project? Which is the best for your Embedded Linux project?
 
Embedded Linux Quick Start Guide v1.5
Embedded Linux Quick Start Guide v1.5Embedded Linux Quick Start Guide v1.5
Embedded Linux Quick Start Guide v1.5
 
Running Android on the Raspberry Pi: Android Pie meets Raspberry Pi
Running Android on the Raspberry Pi: Android Pie meets Raspberry PiRunning Android on the Raspberry Pi: Android Pie meets Raspberry Pi
Running Android on the Raspberry Pi: Android Pie meets Raspberry Pi
 
Android rpi-csimmonds-fosdem-2019
Android rpi-csimmonds-fosdem-2019Android rpi-csimmonds-fosdem-2019
Android rpi-csimmonds-fosdem-2019
 
Linux power management: are you doing it right?
Linux power management: are you doing it right?Linux power management: are you doing it right?
Linux power management: are you doing it right?
 
Embedded Android: Android beyond the smartphone
Embedded Android: Android beyond the smartphoneEmbedded Android: Android beyond the smartphone
Embedded Android: Android beyond the smartphone
 
Quick and Easy Device Drivers for Embedded Linux Using UIO
Quick and Easy Device Drivers for Embedded Linux Using UIOQuick and Easy Device Drivers for Embedded Linux Using UIO
Quick and Easy Device Drivers for Embedded Linux Using UIO
 
Software update for IoT: the current state of play
Software update for IoT: the current state of playSoftware update for IoT: the current state of play
Software update for IoT: the current state of play
 
Read-only rootfs: theory and practice
Read-only rootfs: theory and practiceRead-only rootfs: theory and practice
Read-only rootfs: theory and practice
 
10 ways hardware engineers can make software integration easier
10 ways hardware engineers can make software integration easier10 ways hardware engineers can make software integration easier
10 ways hardware engineers can make software integration easier
 
Userspace drivers-2016
Userspace drivers-2016Userspace drivers-2016
Userspace drivers-2016
 
The end of embedded Linux (as we know it)
The end of embedded Linux (as we know it)The end of embedded Linux (as we know it)
The end of embedded Linux (as we know it)
 
Linux field-update-2015
Linux field-update-2015Linux field-update-2015
Linux field-update-2015
 
The Android graphics path, in depth
The Android graphics path, in depthThe Android graphics path, in depth
The Android graphics path, in depth
 
A timeline for embedded Linux
A timeline for embedded LinuxA timeline for embedded Linux
A timeline for embedded Linux
 

Último

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Último (20)

Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 

Tuning Android for low RAM

  • 1. Tuning Android for low RAM when 1 GiB is too much Chris Simmonds Tuning Android for low RAM 1 Copyright © 2011-2014, 2net Ltd
  • 2. License These slides are available under a Creative Commons Attribution-ShareAlike 3.0 license. You can read the full text of the license here http://creativecommons.org/licenses/by-sa/3.0/legalcode You are free to • copy, distribute, display, and perform the work • make derivative works • make commercial use of the work Under the following conditions • Attribution: you must give the original author credit • Share Alike: if you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one (i.e. include this page exactly as it is) • For any reuse or distribution, you must make clear to others the license terms of this work Tuning Android for low RAM 2 Copyright © 2011-2014, 2net Ltd
  • 3. About Chris Simmonds • Consultant and trainer • Working with embedded Linux since 1999 • Android since 2009 • Speaker at many conferences and workshops "Looking after the Inner Penguin" blog at http://2net.co.uk/ https://uk.linkedin.com/in/chrisdsimmonds/ https://google.com/+chrissimmonds Tuning Android for low RAM 3 Copyright © 2011-2014, 2net Ltd
  • 4. Overview • Project Svelte • How much RAM do you need? • Tuning Android • Global tuning knob: "ro.config.low_ram" • The Dalvik JIT cache • The OnTrimMemory application callback • Tuning the kernel • Kernel Samepage Merging (KSM) • Swap to compressed RAM Tuning Android for low RAM 4 Copyright © 2011-2014, 2net Ltd
  • 5. The problem • Since Gingerbread minimum RAM has gone beyond 512 MiB • Especially since "project Butter" (Jelly Bean 4.1) which improved graphics performance by adding more buffers (among other changes) • But there is still a desire to run Android on such "low-end" hardware • Cheap smartphones • Wearables: e.g. the current generation of smartwatches • "Embedded Android" Tuning Android for low RAM 5 Copyright © 2011-2014, 2net Ltd
  • 6. Project Svelte • Kit Kat 4.4 introduced "project Svelte": Android on devices with 512 MiB RAM • Project Svelte consists of • Various memory-saving changes to Android framework • Tuning knobs for Android • Validated techniques for tuning Linux • Improved memory diagnostics • See source.android.com/devices/low-ram.html • Note: In many cases there is a trade-off between reducing memory use and increasing CPU load (and therefore increasing power requirements) Tuning Android for low RAM 6 Copyright © 2011-2014, 2net Ltd
  • 7. How much RAM am I using? • Tricky question! • Some (most) is used by processes: apps and system daemons • But note that processes share a lot of read-only data • Some is cached • But caches can be dropped, so cached memory is usually regarded as "free" • Some is allocated by the kernel and not owned by any process • Some is used for the code and data segments of the kernel and kernel modules Tuning Android for low RAM 7 Copyright © 2011-2014, 2net Ltd
  • 8. Memory metrics for processes • For each Linux process we can measure • Vss = virtual set size: pages mapped by this process • Rss = resident set size: pages mapped to real memory • Uss = unique set size: pages of memory not shared with any other process • Pss = proportional set size: pages shared with other processes, divided by the number of processes sharing • Perhaps a diagram would help... Tuning Android for low RAM 8 Copyright © 2011-2014, 2net Ltd
  • 9. Memory metrics P1 P2 Not committed Shared, committed Not shared, C B A committed Vss = A + B + C Rss = A + B Uss = A Pss = A + B/n where n is the number of processes sharing Tuning Android for low RAM 9 Copyright © 2011-2014, 2net Ltd
  • 10. How to calculate Pss P1 C B A P2 P3 3 2 3 2 3 2 3 2 2 3 3 Pss(1) = 2 + 3/3 + 2/2 = 4 Pss(2) = 2 + 3/3 + 2/2 = 4 Pss(3) = 2 + 3/3 = 3 Sum(Pss) = 11 = total of pages in use Tuning Android for low RAM 10 Copyright © 2011-2014, 2net Ltd
  • 11. Tools: procrank Part of the Android tool set for a long time: ranks processes by Pss (default), type procrank -h for more options Example (edited): # procrank PID Vss Rss Pss Uss cmdline 3351 1058776K 163952K 141197K 139596K com.google.earth 2616 943156K 116020K 93360K 91724K com.android.vending 539 990756K 112504K 91393K 89808K com.android.systemui 4657 995760K 105964K 77829K 70776K com.rovio.angrybirds ... 119 31904K 7676K 6038K 5900K /system/bin/surfaceflinger 122 27468K 3788K 3045K 2964K /system/bin/mediaserver ... 120 865084K 24308K 2263K 860K zygote ... ------ ------ ------ 717098K 669272K TOTAL RAM: 1124832K total, 105528K free, 3808K buffers, 136624K cached, 656K shmem, 23656K slab Tuning Android for low RAM 11 Copyright © 2011-2014, 2net Ltd
  • 12. Tools: procmem Another tried and tested tool: shows Vss, Rss, etc for each mapping of a single process Example (edited): # procmem 119 Vss Rss Pss Uss ShCl ShDi PrCl PrDi Name ------- ------- ------- ------- ------- ------- ------- ------- 4K 0K 0K 0K 0K 0K 0K 0K 1012K 4K 4K 4K 0K 0K 4K 0K [stack:944] 512K 512K 512K 512K 0K 0K 36K 476K /dev/mali0 512K 512K 512K 512K 0K 0K 0K 512K /dev/mali0 516K 12K 12K 12K 0K 0K 12K 0K [anon:libc_malloc] 512K 512K 512K 512K 0K 0K 224K 288K /dev/mali0 512K 512K 512K 512K 0K 0K 32K 480K /dev/mali0 516K 12K 12K 12K 0K 0K 12K 0K [anon:libc_malloc] ... 2680K 2668K 2668K 2668K 0K 0K 2668K 0K [heap] 132K 20K 20K 20K 0K 0K 20K 0K [stack] 0K 0K 0K 0K 0K 0K 0K 0K [vectors] ------- ------- ------- ------- ------- ------- ------- ------- 31904K 7676K 6039K 5900K 1760K 16K 4144K 1760K TOTAL Tuning Android for low RAM 12 Copyright © 2011-2014, 2net Ltd
  • 13. The Android application life cycle • Activity Manager grades applications by how many components (activities and services) are being used • Sets a per-process measure called oom_adj • oom_adj values are from -16 (important process) to 15 (unimportant process) • As memory pressure increases, the kernel low memory killer starts killing processes starting with the highest oom_adj Tuning Android for low RAM 13 Copyright © 2011-2014, 2net Ltd
  • 14. Values for oom_adj From frameworks/base/services/java/com/android/ server/am/ProcessList.java State oom_adj Type of process System -16 daemons and system services Persistent -12 persistent apps, e.g. telephony Foreground 0 contains the foreground activity Visible 1 contains activities that are visible Perceptible 2 e.g. background music playback Service 5 contains an application service Home 6 contains the home application Previous 7 the previous foreground application B Services 8 "old and decrepit services" Cached 9..15 all activities and services de-stroyed Tuning Android for low RAM 14 Copyright © 2011-2014, 2net Ltd
  • 15. lowmemory killer thresholds • Thresholds calculated according to screen size and total memory • Example (from Nexus 10) oom_adj Threshold (KiB) -16 49152 -12 49152 0 49152 1 61440 2 73728 3 86016 4 98304 5 98304 6 98304 7 98304 8 98304 9 98304 15 122880 Tuning Android for low RAM 15 Copyright © 2011-2014, 2net Ltd
  • 16. Tools: meminfo dumpsys meminfo takes the oom_adj value into account: # dumpsys meminfo Applications Memory Usage (kB): Uptime: 5156998 Realtime: 70066043 Total PSS by process: 141263 kB: com.google.earth (pid 3351 / activities) 93354 kB: com.android.vending (pid 2616 / activities) 92554 kB: com.android.systemui (pid 539) ... Total PSS by OOM adjustment: 19794 kB: Native 6031 kB: surfaceflinger (pid 119) ... 36427 kB: System 36427 kB: system (pid 444) ... 101001 kB: Persistent 92554 kB: com.android.systemui (pid 539) ... 362721 kB: Cached 141263 kB: com.google.earth (pid 3351 / activities) ... Total RAM: 1124832 kB Free RAM: 633617 kB (362721 cached pss + 138452 cached + 132444 free) Used RAM: 352407 kB (323895 used pss + 4304 buffers + 656 shmem + 23552 slab) Lost RAM: 138808 kB Tuning: 192 (large 512), oom 122880 kB, restore limit 40960 kB (high-end-gfx) Tuning Android for low RAM 16 Copyright © 2011-2014, 2net Ltd
  • 17. Tools: meminfo • In Kit Kat, dumpsys meminfo has been augmented to make the use of memory more clear • Processes with oom_adj >= 9 (CACHED_APP_MIN_ADJ) can be killed without the user noticing • So Free RAM includes apps that can be discarded ("cached pss") and system buffers ("cached") Tuning Android for low RAM 17 Copyright © 2011-2014, 2net Ltd
  • 18. Tools: procstats • procstats adds history to the measurement by integrating Pss over time • Use to identify persistent memory hogs • Typically shows up apps with long-running background services • procstats has a nice graphical interface, and can be run from the command line Tuning Android for low RAM 18 Copyright © 2011-2014, 2net Ltd
  • 19. procstats Settings -> Developer options -> Process Stats • Bar is a summary of memory pressure: green=good, yellow=OK, red=bad • For each app, shows • % of time it was running • a blue bar which is (average Pss * runtime) Tuning Android for low RAM 19 Copyright © 2011-2014, 2net Ltd
  • 20. procstats Zoom in on second app: • Contains a service FlightUpdateService • Has been running 100% of the time • Is taking 33 MiB Tuning Android for low RAM 20 Copyright © 2011-2014, 2net Ltd
  • 21. procstats command-line • The raw data is available through system service procstats • Dump the data using # dumpsys procstats AGGREGATED OVER LAST 24 HOURS: * system / 1000: TOTAL: 100% (22MB-31MB-35MB/19MB-29MB-33MB over 89) Persistent: 100% (22MB-31MB-35MB/19MB-29MB-33MB over 89) * com.android.systemui / u0a6: TOTAL: 100% (36MB-59MB-99MB/34MB-57MB-97MB over 89) Persistent: 100% (36MB-59MB-99MB/34MB-57MB-97MB over 89) Imp Fg: 0.00% The memory numbers are minPss-avgPss-maxPss / minUss-avgUss-maxUss Tuning Android for low RAM 21 Copyright © 2011-2014, 2net Ltd
  • 22. Tuning Android for low RAM What are the options? • Tune Activity manager • Tune Dalvik • Tune Apps Tuning Android for low RAM 22 Copyright © 2011-2014, 2net Ltd
  • 23. Tuning Android for low RAM • Kit Kat has a global tuning parameter for low RAM ro.config.low_ram • If set to true: • Optimise allocations in Dalvik heap • Saves memory by reducing use of the GPU • New API ActivityManager.isLowRamDevice() returns true which apps can use as a hint that they should reduce memory usage: some Google apps are reportedly coded to make this check • Reduces the Dalvik total PSS by 10 - 15% on devices with large bitmaps (such as Nexus 7 or 10) Tuning Android for low RAM 23 Copyright © 2011-2014, 2net Ltd
  • 24. Tuning Dalvik • The Dalvik JIT cache defaults to 1.5 MiB per app (on ARMv7a): a typical app uses 100 KiB to 200 KiB • You can reduce it, but if set too low will send the JIT into a thrashing mode • For really low-memory devices it is better to disable JIT completely by setting cache size to zero BoardConfig.mk: PRODUCT_PROPERTY_OVERRIDES += dalvik.vm.jit.codecachesize=0 • Saving: 3 MiB to 6 MiB Tuning Android for low RAM 24 Copyright © 2011-2014, 2net Ltd
  • 25. Tuning applications • Generally, dereference objects that are not being used so they can be garbage-collected • Especially, implement void onTrimMemory (int level) and free memory as indicated • level is one of TRIM_MEMORY_COMPLETE, TRIM_MEMORY_MODERATE, TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_UI_HIDDEN, TRIM_MEMORY_RUNNING_CRITICAL, TRIM_MEMORY_RUNNING_LOW, or TRIM_MEMORY_RUNNING_MODERATE Tuning Android for low RAM 25 Copyright © 2011-2014, 2net Ltd
  • 26. Wallpaper • Ensure the default wallpaper setup on launcher is not live wallpaper • Do not pre-install any live wallpapers Tuning Android for low RAM 26 Copyright © 2011-2014, 2net Ltd
  • 27. Tuning Linux for low RAM What are the options? • extra_free_kbytes • KSM • Swap to compressed RAM Tuning Android for low RAM 27 Copyright © 2011-2014, 2net Ltd
  • 28. Linux memory reclaim • Background reclaim is done by the kswap daemon • Started when free memory drops below a threshold: 2MB on a 2GB device and 636KB on a 512MB • Aims to keep some memory free by flushing dirty pages to disk (or invoking the low memory killer) • Direct reclaim happens when a process tries to allocate memory and there are no free pages • blocks the calling thread while pages are freed • Direct reclaim is bad because it can freeze the UI thread, leading to a poor UX Tuning Android for low RAM 28 Copyright © 2011-2014, 2net Ltd
  • 29. extra_free_kbytes • Default kswapd threshold is rather low for Android devices • /proc/sys/vm/extra_free_kbytes is a tuneable added by Google to Linux 3.4 to modify the kswapd threshold • If set to 0 (default), Activity Manager will adjust it to 3 x screen buffer • Can be configured in platform config.xml frameworks/base/core/res/res/values/config.xml • config_extraFreeKbytesAbsolute overrides the default chosen by Activity Manager: -1 keeps the default • config_extraFreeKbytesAdjust added (subtracted if negative) from the value chosen by Activity Manager Tuning Android for low RAM 29 Copyright © 2011-2014, 2net Ltd
  • 30. Kernel Samepage Merging (KSM) • KSM is a kernel thread (ksmd) that runs in the background and compares pages in memory that have been marked MADV_MERGEABLE by user-space • If two pages are found to be the same, it merges them back to a single copy-on-write page • Balancing reduced memory usage vs more processing (greater power demand) • Benefit depends on workload Tuning Android for low RAM 30 Copyright © 2011-2014, 2net Ltd
  • 31. KSM controls • Build kernel with CONFIG_KSM=y (Linux 2.6.32 or later) • Controlled by these files in /sys/kernel/mm/ksm File default Description run 0 Start ksmd thread if non-zero sleep_millisecs 500 ms between scans pages_to_scan 100 pages per scan • Typically you add lines to your init.[name].rc to set up KSM Tuning Android for low RAM 31 Copyright © 2011-2014, 2net Ltd
  • 32. Does KSM work? • Also in /sys/kernel/mm/ksm File Description full_scans # times all mergeable pages have been scanned pages_shared # shared pages are being used pages_sharing # more sites are sharing them i.e. how much saved pages_unshared # pages unique but repeatedly checked for merg-ing pages_volatile # pages changing too fast to be merged • And, at the end of dumpsys meminfo: # dumpsys meminfo ... KSM: 33992 kB saved from shared 4216 kB 234796 kB unshared; 532028 kB volatile • Typical saving: about 10% of used RAM Tuning Android for low RAM 32 Copyright © 2011-2014, 2net Ltd
  • 33. Compressed swap area • Use a compressed RAM swap area, zram, for swap • Unused dirty pages can be swapped out and compressed • Compression ratios in the 30-50% range are usually observed • Once again, you are balancing reduced memory usage vs more processing (greater power demand) Tuning Android for low RAM 33 Copyright © 2011-2014, 2net Ltd
  • 34. Configuring zram • Add to kernel config CONFIG_SWAP CONFIG_CGROUP_MEM_RES_CTLR CONFIG_CGROUP_MEM_RES_CTLR_SWAP CONFIG_ZRAM • Add to /fstab.[product name]: /dev/block/zram0 none swap defaults zramsize=<size in bytes>,swapprio=<swap partition priority> • Add to /init.[product name].rc: swapon_all /fstab.[product name] Tuning Android for low RAM 34 Copyright © 2011-2014, 2net Ltd
  • 35. • Questions? Slides on Slide Share: http://www.slideshare.net/ chrissimmonds/android-lowmemoryabs2014 Tuning Android for low RAM 35 Copyright © 2011-2014, 2net Ltd