Please disable adblock to view this page.

← Go home

Unix and Android Memory Management

unix-svr4-memory-management-formats

October 29, 2016
Published By : Pratik Kataria
Categorised in:

SVR4 and Solaris use two separate schemes:

  • paging system
  • kernel memory allocator

Paging system

  • provides a virtual memory capability that allocates page frames in main memory to processes
  • allocates page frames to disk block buffers

Kernel Memory Allocator

  • allocates memory for the kernel

UNIX SVR4 Memory Management Formats

unix-svr4-memory-management-formats

Page Replacement

The page frame data table is used for page replacement
Pointers are used to create lists within the table

  • all available frames are linked together in a list of free frames available for bringing in pages
  • when the number of available frames drops below a certain threshold, the kernel will steal a number of frames to compensate

two-handed-clock-page-replacement

Lazy Buddy

Technique adopted for SVR4
UNIX often exhibits steady-state behavior in kernel memory demand i.e. the amount of demand for blocks of a particular size varies slowly in time
Defers coalescing until it seems likely that it is needed, and then coalesces as many blocks as possible

ANDROID MEMORY MANAGEMENT

Fastest growing mobile OS
Over 300,000 Android activations a day
Android overtook iOS as the dominant OS in US during 2H 2010
First phone launched HTC G1 in 2008
Currently an OS of choice for Motorola, HTC, Samsung, Sony Ericsson, among others
Software updates every few months

Android – what is it?

software stack for mobile devices
-incl. OS, middleware and key applications
Open source – source code is open and contributions are welcome
application framework enabling reuse/replacement of apps
Dalvik virtual machine optimized for mobile
integrated browser based on webkit
Optimized graphics – 2D graphics library, 3D based on OPEN GL ES
SQLite for data storage
Media support (MPEG4, H.264, MP3, AAC, JPG, PNG,…)
Support for radio interfaces, Bluetooth, WiFi, Camera, GPS, accelerometer
Software Development Kit (SDK)
Preinstalled applications from Google (GMS)
-Gmail, Maps, Search, Voice Search, Youtube

Software Stack

Linux kernel
Libraries
Android run time

  • core libraries
  • Dalvik virtual machine

application layer
application protocol

Android S/W Stack – Application

Android provides a set of core applications:

  • Email Client
  • SMS Program
  • Calendar
  • Maps
  • Browser
  • Contacts

All applications are written using the Java language.

Android S/W Stack – App Framework

Enabling and simplifying the reuse of components

  • Developers have full access to the same framework APIs used by the core applications.
  • Users are allowed to replace components.

app-framework

Android S/W Stack – Libraries

Including a set of C/C++ libraries used by components of the Android system
Exposed to developers through the Android application framework

Android S/W Stack – Runtime

Core Libraries

  • Providing most of the functionality available in the core libraries of the Java language
  • APIs
    • Data Structures
    • Utilities
    • File Access
    • Network Access
    • Graphics
  • Dalvik Virtual Machine
    • Providing environment on which every Android application runs
      • Each Android application runs in its own process, with its own instance of the Dalvik VM.
      • Dalvik has been written such that a device can run multiple VMs efficiently.
    • Register-based virtual machine
    • Executing the Dalvik Executable (.dex) format
      • .dex format is optimized for minimal memory footprint.
      • Compilation
    • Relying on the Linux Kernel for:
      • Threading
      • Low-level memory management

Android S/W Stack – Linux Kernel

Relying on Linux Kernel 2.6 for core system services

  • Memory and Process Management
  • Network Stack
  • Driver Model
  • Security

Providing an abstraction layer between the H/W and the rest of the S/W stack

Boot Process in Android

boot-process-android

Steps:
Power on and system start-up
Bootloader
The Linux Kernel
The init process
Zygote and Dalvik
The System server

1. Power on and system start-up

After power-on, Boot-ROM code starts executing from a pre-defined location
Boot-ROM is a hard-wired code
It loads Bootloader into RAM

2. Bootloader

Executes in two stages:
Detect external RAM and call second stage
Set up network, memory etc. to run the kernel
Can be found at:
<AndroidSource>\bootable\bootloader\legacy\usbloader

Two files in bootloader:
init.s – Initializes stacks, zeros the BSS segments,
call_main() in main.c
main.c – Initializes hardware (clocks, board,
keypad, console), creates Linux tags.

3. The Linux Kernel

Bootloader loads Linux Kernel from Boot media and place in the RAM
Linux Kernel Initializes interrupt controllers, set up memory protections, caches and scheduling.
4. The init process

Kernel launches init process from the location:
<android source>/system/core/init
Init.rc is a script that describes the system services, file system and other parameters that need to be set up
Can be found at:
<android source>/system/core/rootdir/init.rc

5. Zygote and Dalvik

Dalvik: Virtual Machine (VM) used in Java
Different instances for separate apps
Not possible in Android, as it requires more memory and time (Android requires quick launch of apps)
In android, Zygote is used, which enables shared code across Dalvik VM, lower memory footprint and minimal startup time.
The Zygote is launched by the init process and will basically just start executing and and initialize the Dalvik VM.

6. The System Server

First java component to run in the system
Source code can be found at:
frameworks/base/services/java/com/android/server/SystemServer.java

Core services:

1.     Starting Power Manager
2.     Creating Activity Manager
3.     Starting Telephony Registry
4.     Starting Package Manager
5.     Set Activity Manager Service as System Process
6.     Starting Context Manager
7.     Starting System Context Providers
8.     Starting Battery Service
9.     Starting Alarm Manager
10.   Starting Sensor Service
11.   Starting Window Manager
12.   Starting Bluetooth Service
13.   Starting Mount Service

Other services:

1. Starting Status Bar Service
2.     Starting Hardware Service
3.     Starting NetStat Service
4.     Starting Connectivity Service
5.     Starting Notification Manager
6.     Starting DeviceStorageMonitor Service
7.     Starting Location Manager
8.     Starting Search Service
9.     Starting Clipboard Service
10.   Starting Checkin Service
11.   Starting Wallpaper Service
12.   Starting Audio Service
13.   Starting HeadsetObserver
14.   Starting AdbSettingsObserver