Tuning Android for low RAM

Similar documents
Efficient Memory Management on Mobile Devices

Virtual Memory Management in Linux (Part II)

Read-only rootfs. Theory and practice. Chris Simmonds. Embedded Linux Conference Europe Read-only rootfs 1 Copyright , 2net Ltd

PostgreSQL Performance Tuning. Ibrar Ahmed Senior Software Percona LLC PostgreSQL Consultant

ION - Large pages for devices

Controlling Memory Footprint at All Layers: Linux Kernel, Applications, Libraries, and Toolchain

Lecture 3 Android Internals

Memory management. Last modified: Adaptation of Silberschatz, Galvin, Gagne slides for the textbook Applied Operating Systems Concepts

Android Memory Management

Memory management: outline

Memory management: outline

Operating Systems. IV. Memory Management

June IBM Power Academy. IBM PowerVM memory virtualization. Luca Comparini STG Lab Services Europe IBM FR. June,13 th Dubai

A pragmatic guide to boot-time optimization

CS370 Operating Systems

Chapter 8: Memory-Management Strategies

Caching and Buffering in HDF5

Swapping. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CHAPTER 8 - MEMORY MANAGEMENT STRATEGIES

Swapping. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS510 Operating System Foundations. Jonathan Walpole

Chapter 8: Main Memory

Chapter 8: Main Memory. Operating System Concepts 9 th Edition

The Beast We Call A3. CS 161: Lecture 10 3/7/17

Chapter 8 Memory Management

CS 5523 Operating Systems: Memory Management (SGG-8)

Chapter 8: Main Memory

Android: Under the Hood. GDG-SG DevFest 5th Nov 2016 Jason Zaman

Chapter 8: Memory- Management Strategies. Operating System Concepts 9 th Edition

Understanding Storage I/O Behaviors of Mobile Applications. Louisiana State University Department of Computer Science and Engineering

OS-caused Long JVM Pauses - Deep Dive and Solutions

Virtual Memory Management

Process. Memory Management

Another difference is that the kernel includes only the suspend to memory mechanism, and not the suspend to hard disk, which is used on PCs.

Process. One or more threads of execution Resources required for execution. Memory (RAM) Others

Basic Memory Management. Basic Memory Management. Address Binding. Running a user program. Operating Systems 10/14/2018 CSC 256/456 1

GCMA Overview. LG Electronics CTO Software Platform Laboratory Minchan Kim

Letting Go. John Stultz August 30th

GCMA: Guaranteed Contiguous Memory Allocator. SeongJae Park

PROCESS VIRTUAL MEMORY. CS124 Operating Systems Winter , Lecture 18

CS399 New Beginnings. Jonathan Walpole

Memory management. Requirements. Relocation: program loading. Terms. Relocation. Protection. Sharing. Logical organization. Physical organization

Process. One or more threads of execution Resources required for execution

Topics. File Buffer Cache for Performance. What to Cache? COS 318: Operating Systems. File Performance and Reliability

OS and Hardware Tuning

Chapter 8: Memory- Management Strategies. Operating System Concepts 9 th Edition

Chapter 8: Memory- Management Strategies

Disk scheduling Disk reliability Tertiary storage Swap space management Linux swap space management

OS and HW Tuning Considerations!

CHAPTER 8: MEMORY MANAGEMENT. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

Virtual Memory Paging

Chapter 7: Main Memory. Operating System Concepts Essentials 8 th Edition

TA7750 Understanding Virtualization Memory Management Concepts. Kit Colbert, Principal Engineer, VMware, Inc. Fei Guo, Sr. MTS, VMware, Inc.

Windows 10: Memory Compression (from insider hub, 20 August 2015)

CS370 Operating Systems

CS 261 Fall Mike Lam, Professor. Virtual Memory

Chapter 10: Case Studies. So what happens in a real operating system?

Performance Sentry VM Provider Objects April 11, 2012

Recall: Address Space Map. 13: Memory Management. Let s be reasonable. Processes Address Space. Send it to disk. Freeing up System Memory

memory management Vaibhav Bajpai

Design Issues 1 / 36. Local versus Global Allocation. Choosing

Chapter 8 Main Memory

Memory Management. Fundamentally two related, but distinct, issues. Management of logical address space resource

Android Internals. Lecture 3. Operating Systems Practical. 19 October 2016

Chapter 4: Memory Management. Part 1: Mechanisms for Managing Memory

The Virtual Memory Abstraction. Memory Management. Address spaces: Physical and Virtual. Address Translation

ECE 598 Advanced Operating Systems Lecture 10

Memory Footprint of Locality Information On Many-Core Platforms Brice Goglin Inria Bordeaux Sud-Ouest France 2018/05/25

Everything You Ever Wanted To Know About Resource Scheduling... Almost

What are Transactions? Transaction Management: Introduction (Chap. 16) Major Example: the web app. Concurrent Execution. Web app in execution (CS636)

CSE 120. Translation Lookaside Buffer (TLB) Implemented in Hardware. July 18, Day 5 Memory. Instructor: Neil Rhodes. Software TLB Management

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

SRM-Buffer: An OS Buffer Management SRM-Buffer: An OS Buffer Management Technique toprevent Last Level Cache from Thrashing in Multicores

CSE 4/521 Introduction to Operating Systems. Lecture 12 Main Memory I (Background, Swapping) Summer 2018

Transaction Management: Introduction (Chap. 16)

Process. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

CS 390 Chapter 8 Homework Solutions

Lecture 16. Today: Start looking into memory hierarchy Cache$! Yay!

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Chapter 8: Virtual Memory. Operating System Concepts

davidklee.net gplus.to/kleegeek linked.com/a/davidaklee

CFLRU:A A Replacement Algorithm for Flash Memory

Chapter 8. Virtual Memory

CS307: Operating Systems

Operating System Concepts

Virtual Memory. Kevin Webb Swarthmore College March 8, 2018

CSCE Operating Systems Non-contiguous Memory Allocation. Qiang Zeng, Ph.D. Fall 2018

OPERATING SYSTEM. Chapter 9: Virtual Memory

Process. One or more threads of execution Resources required for execution. Memory (RAM) Others

Basic Memory Management

SHARED ADDRESS TRANSLATION REVISITED

CSE 451: Operating Systems. Section 10 Project 3 wrap-up, final exam review

KVM PERFORMANCE OPTIMIZATIONS INTERNALS. Rik van Riel Sr Software Engineer, Red Hat Inc. Thu May

Lecture 14: Cache & Virtual Memory

Page Frame Reclaiming

Performance of Various Levels of Storage. Movement between levels of storage hierarchy can be explicit or implicit

Memory Management. Goals of Memory Management. Mechanism. Policies

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Memory Management. COMP755 Advanced Operating Systems

Classifying Information Stored in Memory! Memory Management in a Uniprogrammed System! Segments of a Process! Processing a User Program!

Transcription:

Tuning Android for low RAM when 1 GiB is too much Tuning Android for low RAM 1 Copyright 2011-2014, 2net Limited

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 The orginals are at http://2net.co.uk/slides/android-lowmemory-abs-2014.pdf Tuning Android for low RAM 2 Copyright 2011-2014, 2net Limited

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 Limited

Overview Project Svelte How much RAM do you need? Tuning Android Reducing memory pressure from the Dalvik heap Optimising the JIT cache Tuning the kernel Kernel Samepage Merging (KSM) Compressed swap area Tuning Android for low RAM 4 Copyright 2011-2014, 2net Limited

The problem Android devices need memory for Operating system Background services Video buffers Applications Since Gingerbread minimum RAM has gone beyond 512 MiB Especially since Jelly Bean 4.1 "project Butter" which improved graphics performance by adding more buffers. But display buffers are getting larger... Tuning Android for low RAM 5 Copyright 2011-2014, 2net Limited

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 tradeoff between reducing memory use and increasing CPU load (and therefore increasing power requirements) Tuning Android for low RAM 6 Copyright 2011-2014, 2net Limited

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 Limited

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 Limited

Memory metrics P1 P2 C Not committed B Shared, committed A Not shared, 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 Limited

How to calculate Pss P1 P2 P3 C 3 3 2 2 3 B 3 3 3 A 2 2 2 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 Limited

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 Limited

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 Limited

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 Limited

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 destroyed Tuning Android for low RAM 14 Copyright 2011-2014, 2net Limited

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 Limited

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 Limited

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 Limited

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 Limited

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 Limited

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 Limited

procstats command-line The raw data is avaiable 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 Limited

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 Limited

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 Limited

Dalvik JIT code cache 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 Limited

Wallpaper Ensure the default wallpaper setup on launcher is not live wallpaper Do not pre-install any live wallpapers Tuning Android for low RAM 25 Copyright 2011-2014, 2net Limited

Tuning Linux for low RAM What are the options? KSM Swap to compressed RAM Tune ION carveout Tuning Android for low RAM 26 Copyright 2011-2014, 2net Limited

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 27 Copyright 2011-2014, 2net Limited

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 28 Copyright 2011-2014, 2net Limited

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 29 Copyright 2011-2014, 2net Limited

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 30 Copyright 2011-2014, 2net Limited

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 merging 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 31 Copyright 2011-2014, 2net Limited

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 32 Copyright 2011-2014, 2net Limited

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 33 Copyright 2011-2014, 2net Limited

Contiguous memory buffers Some simple peripherals require contiguous memory Typically, a region on memory is reserved using CMA... and allocated using an ION carveout heap... so it makes sense to review and minimise the use of such heaps Tuning Android for low RAM 34 Copyright 2011-2014, 2net Limited

Questions? Slides on Slide Share: http://www.slideshare.net/ chrissimmonds/android-lowmemoryabs2014 Tuning Android for low RAM 35 Copyright 2011-2014, 2net Limited