Androids' memory management differ from the other operating systems. In this PowerPoint presentation we tried to figure out how it works and how it differs from other operating systems.
3. INTRODUCTION
Android is a Linux based OS with 2.6.x kernel which has
been stripped down to handle most tasks pretty well.
It uses native open source C libraries that have powered
Linux machines for years.
The Android SDK provides the tools and API’s necessary to begin developing applications
on the Android platform using the Java programming language.
All the basic OS operations like I/O, memory management, and so on, are
handled by the native Linux kernel.
4. Overview of Android Memory Management
The Android Runtime (ART) and Dalvik virtual machine
use paging and memory-mapping (mmapping) to manage memory.
Android does not support swapping
Flash memory can only be written to a limited number of times
before it becomes unreliable
It uses flash memory for persistent storage like the hard drives in
a desktop, so there is not as much space available
The bandwidth to flash memory is lower
5. WHAT IS MEMORY MAPPING?
Memory mapping is
used to map image and
data files into a
processes address
space.
In memory mapping, the
contents of a file are linked
directly into the virtual
address space of a process.
6. DALVIK VIRTUAL MACHINE
It executes applications written for android
Dalvik is the process virtual machine in Google’s android
operating system
Multiple Instances of the VM running. Like a specialized
java VM
9. So, why
should
we care
about
the
memory?
It executes applications written for android
Heap size is limited and device dependent
Multiple Instances of the VM running. Like a specialized
java VM
Multiple Instances of the VM running. Like a specialized
java VM and Battery life.
10. VIRTUAL MEMORY
In computing, virtual memory (also virtual storage) is a
memory management technique that provides an "idealized
abstraction of the storage resources that are actually
available on a given machine" which "creates the illusion to
users of a very large (main) memory."
The computer's OS using a combination of hardware and software, maps
memory addresses used by a program, called virtual addresses, into
physical addresses in computer memory. Main storage, as seen by a
process or task, appears as a contiguous address space or collection of
contiguous segments. The operating system manages virtual address
spaces and the assignment of real memory to virtual memory. Address
translation hardware in the CPU, often referred to as a memory
management unit or MMU, automatically translates virtual addresses to
physical addresses.
11. VIRTUAL MEMORY-KEY BENEFITS
More security and Isolation
Avoid Process to handle a shared space.
More memory available
Easier programming/hidden fragmentation
13. GARBAGE COLLECTION
Garbage collection has two goals: find data objects in a program that cannot
be accessed in the future; and reclaim the resources used by those objects.
So, garbage collection can be triggered
when an allocation fails
when the size of the heap hits some soft limit
when a GC was explicitly requested.
OutOfMemoryError is about to be triggered
14. GARBAGE COLLECTION
It is the
mechanism for
reclaiming
unused memory
environment.
It is a
predefined
event.
The key point is: Android
memory heap has
different buckets of
allocations that it tracks
based on the expected life
and size of an object being
allocated.
For example-> recently
allocated objects belong in
the young generation
,when an object stays
active long enough it can
be promoted to the older
generation –followed by a
permanent generation.
15. • The actual GC is done using a Mark-Sweep Algorithm
16. SHARING MEMORY
In order to fit everything it needs in RAM, Android tries to share RAM pages across
processes. It can do so in the following ways:
2. Most static data is mmapped into a process.
1. Each app process is forked from an existing process called
Zygote. Zygote is Android’s core process that starts up at
init .It is the initial cell formed when a new organism is
produced.
3. In many places, Android shares the same dynamic RAM
across processes using explicitly allocated shared memory
regions.
17. ALLOCATING AND RECLAIMING APP MEMORY
Here are some facts about how Android
allocates then reclaims memory from
your app:
• The Dalvik heap for each process is
constrained to a single virtual memory
range. This defines the logical heap
size, which can grow as it needs to (but
only up to a limit that the system
defines for each app).
18. ALLOCATING AND RECLAIMING APP MEMORY
The Dalvik heap does not compact the logical size of the heap, meaning that Android does not
defragment the heap to close up space. Android can only shrink the logical heap size when there
is unused space at the end of the heap.
• But this doesn't mean the physical memory used by the heap can't shrink. After garbage
collection, Dalvik walks the heap and finds unused pages, then returns those pages to the kernel
using ‘madvise()’.
So,the point is paired allocations and deallocations of large chunks should result in reclaiming
all (or nearly all) the physical memory used. However, reclaiming memory from small allocations
can be much less efficient because the page used for a small allocation may still be shared with
something else that has not yet been freed.
19. • To maintain a functional multi-tasking environment, Android sets a hard limit on the heap size for
each app. The exact heap size limit varies between devices based on how much RAM the device has
available overall.
• If your app has reached the heap capacity and tries to allocate more memory, it will receive an
OutOfMemoryError.
• You might want to query the system to determine exactly how much heap space you have available
on the current device—for example, to determine how much data is safe to keep in a cache. You can
query the system for this figure by calling getMemoryClass(). This returns an integer indicating the
number of megabytes available for your app's heap.
Restricting App Memory
20. When the user switches between apps, Android keeps processes that are not
hosting a foreground ("user visible") app component in a least-recently used
(LRU) cache.
For example, when the user first launches an app, a process is created for it,
but when the user leaves the app, that process does not quit. The system keeps
the process cached, so if the user later returns to the app, the process is reused
for faster app switching.
Switching Apps
21. If your app has a cached process and it retains memory that it currently does
not need, then your app even while the user is not using it is affecting the
system's overall performance.
So, as the system runs low on memory, it may kill processes in the LRU cache
beginning with the process least recently used, but also giving some
consideration toward which processes are most memory intensive.
Switching Apps
22. Process Management
A process is a program in execution. A
process needs certain resources, including
CPU time, memory, files, and I/O devices, to
accomplish its task.
23. Process
When an application component starts and the
application doesn’t have any other components running,
the Android system starts a new Linux process for the
application with a single thread of execution.
By default all components of the same application run in
the same process and thread(main() thread).
24. Foreground
Process
An acivitiy which is
interacting with user. When
an activity calls methods like
onstart(), onresume().
When a service is executing
any of the methods like
onCreate(), onStart(),
onDestroy().
Background
Process
When an acivity running
method like onStop(). i.e
currently user is not
interacting with that activity.
System maintains LRU list of
background process.
Whenever a process decided
to kill, it stores the state of
activity. So whenever next
user wants that activity, we
can restore the previous
state of activity.
Visible
Process
An acivitiy which is not
interacting with user. But it is
still visible to user, when an
acivity running method like
onPause().
When a service is interacting
with visible acitvity.
Service
Process
When a service is started
using startService().
Eg: playing music in
background, downloading file
from internet.
Empty
Process
When a process does not
have any active component.
Caching of empty process
inside memory, reduces
relaunching of applicaiton
once again if user wants that
applicaiton in future.
Process Life Cycle in Android
25. COMPARISON OF MM BETWEEN ANDROID AND AN OTHER OS (WINDOWS)
Android Windows
To stress the memory management of the Android, an app
creates multiple foreground activities to allocate memory
resources.
Windows supports virtual memory and can run more
programs than there is RAM to hold.
High memory and CPU usage. Low memory and CPU usage.
Android is an open source platform, meaning that the
operating system is available for modification by
manufacturers to suit their respective needs and phones.
Microsoft Windows is closed-sourced, meaning that it is
owned and managed by Microsoft and developers do not
have direct access to the operating system programming
code.
The Android system does not allow the machine to be
overcommitted and will terminate idle apps to free memory.
In Windows, the main memory is saturated, the virtual
memory system starts to thrash and performance is greatly
degraded. The programs will run, but very slowly.
The library is divided in to two components: Android
Runtime and Android Library. Android Runtime is consisted
of a Java Core Library and Dalvik Virtual Machine.
The core operating system (OS) services consist of the kernel
and other features. Core OS services enable low-level tasks,
such as process, thread, and memory management. Basic
device drivers are also part of the Core OS services.
Supported CPU architectures: ARM, x86, MIPS and the 64-bit
variants of all three.
Supported CPU architecture: ARM
26. SUMMARY
Android is based on a Linux kernel that uses APIs to give
access to system calls.
Android uses its own virtual machine called DalvikVM to
ensure that multiple processes can run at the same time.
The memory management in Android is based on the
priority of the processes that are currently running.
Processes are killed following this priority hierarchy to
maintain overall memory management