Unit III Notes Mobile Application Development
Unit III Notes Mobile Application Development
UNIT III
MEMORY MANAGEMENT-DESIGN PATTERNS FOR LIMITED MEMORY –
WORKFLOW FOR APPLICATION DEVELOPMENT – JAVA API – DYNAMIC
LINKING – PLUGINS AND RULE OF THUMB FOR USING DLL –
CONCURRENCY AND RESOURCE MANAGEMENT
A managed memory environment, like the ART or Dalvik virtual machine, keeps track
of each memory allocation.
Once it determines that a piece of memory is no longer being used by the program, it
frees it back to the heap, without any intervention from the programmer. The
mechanism for reclaiming unused memory within a managed memory
environment is known as garbage collection.
Garbage collection has two goals:
1. Find data objects in a program that cannot be accessed in the future;
2. Reclaim the resources used by those objects.
Android’s memory heap is a generational one, meaning that there are
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 an older
generation, followed by a permanent generation.
Each heap generation has its own dedicated upper limit on the amount of
memory that objects there can occupy. Any time a generation starts to fill up, the system
executes a
garbage collection event in an attempt to free up memory. The duration of the garbage
collection depends on which generation of objects it’s collecting and how many active
objects are in each generation.
The system has a running set of criteria for determining when to perform
garbage collection. When the criteria are satisfied, the system stops executing
the process and begins garbage collection.
Active Processes
Active (foreground) processes are those hosting applications with
components currently interacting with the user.
These are the processes Android is trying to keep responsive by reclaiming
resources.
There are generally very few of these processes, and they will be killed only as a last resort.
Activities in an ―active‖ state; that is, they are in the foreground
and responding to user events. You will explore Activity states in greater detail later in
this chapter.
Activities, Services, or Broadcast Receivers that are currently executing an
onReceive event handler.
Services that are executing an onStart, onCreate, or onDestroy event handler.
Visible Processes
Visible, but inactive processes are those hosting ―visible ‖ Activities . As
the name suggests, visible Activities are visible, but they aren’t in the
foreground or responding to user events.
Started Service Processes
Processes hosting Services that have been started. Services support ongoing
processing that should continue without a visible interface.
Because Services don‘t interact directly with the user, they receive a slightly
lower priority than visible Activities.
DDMS
Android Studio includes a debugging tool called the Dalvik Debug Monitor Service
(DDMS). DDMS provides services like screen capture on the device, threading, heap
information on the device, logcat, processes, incoming calls, SMS checking, location,
data spoofing, and many other things related to testing your Android application.
DDMS connects the IDE to the applications running on the device. On Android, every
application runs in its own process, each of which hosts its own virtual machine (VM).
And each process listens for a debugger on a different port.
When it starts, DDMS connects to ADB (Android Debug Bridge, which is a command-
line utility included with Google‘s Android SDK.).
An Android Debugger is used for debugging the Android app and starts a device
monitoring service between the two. This will notify DDMS when a device is connected
or disconnected.
When a device is connected, a VM monitoring service is created between ADB and
DDMS, which will notify DDMS when a VM on the device is started or terminated.
bytes) – functions calling themselves recursively can end up using a lot of stack, if the
depth of the recursion is not considered beforehand.
Data Packing
Data packing is probably the most obvious way to reduce memory
consumption. There are several sides to data packing.
Use compression with care. In addition to considering the data layout in memory, there are several
compression techniques for decreasing the size of a file.
Table compression, also referred to as nibble coding or Huffman
coding, is about encoding each element of data in a variable
number of bits so that the more common elements require fewer
bits.
We have provided the complete phases of a mobile app development process that
would guide you throughout your development journey.
1. Researching
2. Wire framing
3. Evaluating Technical Feasibility
4. Prototyping
5. Designing
6. Developing
7. Testing
8. Deploying the app
1. Researching
You might already have plenty of ideas for your mobile app; it is still good to dig deeper
into demographics, behavior patterns, and demand of your targeted audiences. The other
important thing that covers up under this phase of the mobile app development process is
not to overlook your competitors. By researching thoroughly, get yourself answers to these
following questions:
o Who is your targeted audience?
o What will be a suitable platform to launch your app?
o What are your competitors offering to customers?
These are just a few questions from the long list that you have to keep in mind. While
researching, think from your customers‘ perspective to find out what additional
features should be there in your app to make it stand out in the crowd. Sparing enough
time for researching and brainstorming will build a strong foundation for your mobile app
development.
2. Wire framing
Wire framing gives a clear understanding of your app’s features and
functionalities, and hence, is a crucial phase. Draw detailed sketches
of the product you want to build to reveal the usability problems beforehand. Wire
framing help narrow down the ideas and organize all the app design components correctly.
Try to identify how your planned features will blend into a fully-functional mobile
application. Also, make a storyboard or roadmap to demonstrate how a user will use and
explore your app. Your prime focus should be on delivering an excellent customer
experience by simplifying the roadmap.
In this phase of mobile app development process, you have to check whether the
backend system would be capable of supporting the app’s functionality
or not. To figure out the technical feasibility of your app‘s idea, access the public data
by sourcing public application programming interfaces (APIs).
We have to understand that an app with different formats (wearables, smartphones, tablets,
etc.) and platforms (Android, iOS, or any other) will not have the same needs. By the end of
this stage, you would have various ideas for your app‘s functionality.
4. Prototyping
5. Designing
6. Developing
Generally, this phase of the android and iOS app development process starts at the very
initial stage.
Right after you finalize an app idea, the developers need to develop a prototype to
authenticate the features and functionalities.
The development phase is further divided into various sections, where the
team or a developer writes pieces of code, which then tested by another team. After marking
the first section as bug-free, the development team moves further.
In the case of complex projects with frequently changing user requirements, it is good to opt
for an agile methodology. This type of methodology leads to progressive development and
brings flexibility in the planning process.
7. Testing
Testing early and frequently gives developers the advantage of fixing a bug right
when it occurs.
It also controls the final cost of the development as it will require both money and
efforts to fix a bug, which occurred at the first stage, after you reach on fifth or more.
While testing your app, consider compatibility, security, usability,
performance, UI checks, and other factors in mind. Check whether your
application serves its purpose or not.
In this stage, your app is ready to launch. To do so, select a day and release your
mobile application on the desired platforms.
Deploying the app is not the final step technically as you receive feedback from your
audience and thus, have to make the changes accordingly.
The other two mobi answer the queries of the users using your mobile application. It would
be no wrong to say that mobile app development is a long-term commitment rather than just
a short-term project.
JAVA API
What is Java?
Java is an object-oriented programming language that runs on almost
all electronic devices. Java is platform-independent because of Java
virtual machines (JVMs).
It follows the principle of "write once, run everywhere.‖ When a JVM is
installed on the host operating system, it automatically adapts to the environment and
executes the program‘s functionalities.
To install Java on a computer, the developer must download the JDK and set up the
Java Runtime Environment (JRE).
As previously noted, a Java download consists of two files:
JDK
JRE
The JDK file is key to developing APIs in Java and consists of:
The compiler
The JVM
The Java API
WHAT IS JAVA API AND THE NEED FOR JAVA APIS?
Java application programming interfaces (APIs) are predefined software tools that easily enable interactivity
between multiple applications
Compiler
A Java compiler is a predefined program that converts the high-level, user-written code
language to low-level, computer-understandable, byte-code language during the
compile time.
JVM
A JVM processes the byte-code from the compiler and provides an output in a
user-readable format.
Java APIS
Java APIs are integrated pieces of software that come with JDKs. APIs in Java provides
the interface between two different applications and establish communication.
Three types of developers use Java APIs based on their job or project:
1. Internal developers
2. Partner developers
3. Open developers
Internal Developers
Internal developers use internal APIs for a specific organization. Internal APIs are accessible only
by developers within one organization.
Applications that use internal APIs include:
B2B
B2C
A2A
B2E
Examples include Gmail, Google Cloud VM, and Instagram.
Partner Developers
Organizations that establish communications develop and use partner APIs. These types of APIs
are available to partner developers via API keys.
Applications that use partner APIs include:
B2B
B2C
Examples include Finextra and Microsoft (MS Open API Initiative),
Open Developers
Some leading companies provide access to their APIs to developers in the open-source
format. These businesses provide access to APIs via a key so that the company can ensure
that the API is not used illegally.
The application type that uses internal APIs is:
B2C
Examples include Twitter and
Telnyx. THE NEED FOR
JAVA APIS
Java developers use APIs to:
Introducing APIs to the public leads many companies to release private data to generate new ideas,
fix existing bugs, and receive new ways to improve operations.
Online banking has changed the industry forever, and APIs offer customers the ability to manage
their finances digitally with complete simplicity.
TYPES OF JAVA APIS
There are four types of APIs in Java:
Public
Private
Partner
Composite
Public
Public (or open) APIs are Java APIs that come with the JDK. They do not have strict restrictions
about how developers use them.
Private
Private (or internal) APIs are developed by a specific organization and are accessible to only
employees who work for that organization.
Partner
Partner APIs are considered to be third-party APIs and are developed by organizations for strategic
business operations.
Composite
Downloaded by RISHIKESH KA CSE ([email protected])
lOMoARcPSD|37512785
Composite APIs are micro services, and developers build them by combining several service
APIs. THE ADVANTAGES OF APIS
Some of the main advantages of using Java APIs include:
APIs in Java enable a wide range of SQL support services in user applications through a component-based
interface
Scope
Java APIs easily make websites, applications, and information available to a wide range of users
and audiences.
Customization
Java APIs enable developers and businesses to build applications that personalize the user interface
and data.
Adaptability
Java APIs are highly flexible and adaptable because they can easily accept feature updates and
changes to frameworks and operating environments.
DYNAMIC LINKING
Application
APIs in Java provide effortless access to all of an application‘s major software components and
easily deliver services.
Efficiency
Java APIs are highly efficient because they enable rapid application deployment. Also, the data that
the application generates is always available online.
Automation
APIs allow computers to automatically upload, download, update and delete data automatically
without human interaction.
Integration
Java APIs can integrate into any application and website and provide a fluid user experience
with dynamic data delivery.
DYNAMIC LINKING
Several applications can use the library in such a fashion that only one copy
of the library is needed, thus saving memory.
Application-specific tailoring can be handled in a convenient fashion, provided that
supporting facilities exist.
Smaller compilations and deliveries are enabled
Composition of systems becomes more flexible, because only a
subset of all possible software can be included in a device when
creating a device for a certain segment.
It is easier to focus testing to some interface and features that can be
accessed using that interface.
Library structure eases scoping of system components and enables
the creation of an explicit unit for management.
Work allocation can be done in terms of dynamic libraries, if the implementation is
carried out using a technique that does not support convenient mechanisms for
modularity.
STATIC VERSUS DYNAMIC DLLS
While dynamically linked libraries are all dynamic in their nature, there are two different
implementation schemes.
One is static linking, which most commonly means that the library is
instantiated at the starting time of a program, and the loaded library
resides in the memory as long as the program that loaded the library into its memory
space is being executed.
In contrast to static DLLs, dynamic DLLs, which are often also referred to
as plug-in, especially if they introduce some special extension, can
be loaded and unloaded whenever needed, and the facilities can thus be
altered during an execution.
The benefit of the approach is that one can introduce new features using such libraries.
For instance, in the mobile setting one can consider that sending a message is an
operation that is similar to different message types (e.g. SMS, MMS, email), but
different implementations are needed for communicating with the network in the correct
fashion.
One applicable solution for the implementation of plugins is the abstract factory design
pattern introduced by Gamma et al. (1995).
Which prefixes Abs and Conc refer to abstract and concrete elements
of the design. In some cases, the programmer is responsible for all the
operations, in which case all the plugins are just plain dynamically linked libraries
from which the programmer selects one.
In other cases, sophisticated support for plugins is implemented, where the
infrastructure handles plugin selection based on some heuristics, for instance.
The idea of plugins can be applied in a recursive fashion. This means
that a plugin used for specializing some functions of the system can use other
plugins in its implementation to allow improved flexibility.
IMPLEMENTATION-LEVEL CONCERNS
To begin with, a framework is commonly used for loading and unloading plugins. This
implies a mechanism for extending (or degenerating) an application on the fly when some
new services are needed.
Secondly, in order to enable loading, facilities must be offered for searching all the available
implementations of a certain interface. This selection process is commonly referred to as
resolution. In many implementations, a default implementation is provided if no other libraries
are present, especially when a system plugin that can be overridden is used.
Finally, a policy for registering components and removing them is needed in order to enable
dynamic introduction of features. This can be based on the use of registry files, or simply on
copying certain files to certain locations, for instance.
MANAGING MEMORY CONSUMPTION RELATED TO DYNAMICALLY
LINKED LIBRARIES
Memory consumption forms a major concern in the design of software for mobile devices. At
the same time, a dynamically linked library is often the smallest unit of software that can be
realistically managed when developing software for mobile devices.
MEMORY LIMIT
Setting explicit limits regarding memory usage for all parts of the system is one way to
manifest the importance of controlling memory usage. Therefore, make all dynamically
linked libraries (and other development-time entities) as well as their developers responsible
Downloaded by RISHIKESH KA CSE ([email protected])
lOMoARcPSD|37512785
input is in a form that can be directly processed. Then, performing the tests may require that
binary components are delivered.
• Organizational unit can be authorized to compose a single library that is responsible for a
certain set of functions. Requesting a library then results in a separate deliverable that can
be directly integrated into the final system.
temporarily block each other, so that only one thread at a time performs some
operations on the variable.
In general, such operations commonly address memory that is shared by a number of
threads, and blocking is needed to ensure that only one thread at a
time enters the critical region.
For more complex cases, semaphores are a common technique for ensuring that only
one thread at a time enters the critical region
2. Message Passing
Message passing is another commonly used mechanism for implementing
cooperation between threads.
In this type of an approach, the idea is that threads can send messages to each other, and that
kernel will deliver the messages.
The architecture of such a system resembles message-dispatching architecture,
where the kernel acts as the bus, and individual processes are the stations attached
to it.
COMMON PROBLEMS
Concurrent programs can fail in three different ways.
1. One category of errors in mutual exclusion can lead to locking, where all
threads wait for each other to release resources that they can reserve.
2. Secondly, starvation occurs when threads reserve resources such that some
particular thread can never reserve all the resources it needs at the same time.
3. Thirdly, errors with regard to atomic operations, i.e., operations that are
to be executed in full without any intervention from other threads, can lead to a
failure.
MIDP JAVA AND CONCURRENCY
While Java again in principle hides the implementation of the threading model from the user, its
details can become visible to the programmer in some cases.
1. Threading in Virtual Machine
In the implementation of the Java virtual machine, one important topic is how to
implement threads. There are two alternative implementations, one where threads of the
underlying operating system are used for implementing Java threads, and the other
where the used virtual machine is run in one operating system thread, and it is
responsible for scheduling Java threads for execution.
The latter types of threads are often referred to as green threads; one cannot
see them from green grass.
The scheme can be considered as a sophisticated form of event-based
programming, because in essence, the virtual machine simply acts as an event handler
and schedules the different threads into execution in accordance to incoming events.
2. Using Threads in Mobile Java
Using threads in Java is simple. There is a type thread that can be
instantiated as a Runnable, which then creates a new thread.
The new thread starts its execution from method run after the creator calls the start
method of the thread.
Mutual exclusion is implemented either in the fashion the methods are called or
using the synchronized keyword, which enables monitor-like implementations.
3. As an example, consider the following case. There is a shared variable that is shared by two
classes. The variable is hosted by IncrementerThread, but the other thread (MyThread) is
allowed to access the value directly. The hosting thread (instance of IncrementerThread)
will only increment the shared integer value.
The definition of the thread is the following:
public class IncrementerThread extends Thread
{
public int i;
public IncrementerThread()
{
i = 0;
Thread t = new
Thread(this); t.start();
}
public void run()
{
for(;;) i++;
}}
OVERVIEW
In many ways, each resource included in a mobile device can be considered as a
potential variation and management point.
The parts that are associated with the file system and disks in general should form a
subsystem.
We are essentially creating a manager for all aspects of the file system inside a
specialized entity. This gives a clear strategy for connecting software to the underlying
hardware.
First, a device driver addresses the hardware, and on top of the driver, a
separate resource manager takes care of higher-level concerns.
Subsystems can communicate with each other by for instance sending messages to each
other.
Process boundaries can be used for separating the different resources at the level of an
implementation. Unfortunately this essentially makes the option of handling errors that
occur in the manager of a certain resource more difficult.
Another problem in isolating resource managers is memory protection: in
many cases resource managers can use the same data but memory
protection may require the use of copies. A practical guideline for designing
such isolation is that it should be possible to reconstruct all events for debugging
purposes.
There are two fundamentally different solutions for embedding resources in the system.
1. The first solution is to put all resources under one control. This can be
implemented using a monolithic kernel or a virtual machine through which the access
to the resources of the device is provided.
Downloaded by RISHIKESH KA CSE ([email protected])
lOMoARcPSD|37512785
Positive and negative aspects of this approach are the following. Addressing
different parts of the kernel using procedural interfaces can be
implemented in a performance-effective fashion as no context switching
is required but all the resources can be accessed directly.
The operating system can serve the requests of programs faster, in
particular when an operation that requires coordination in several resources is needed. On
the downside, without careful management, it is possible to create a tangled code base in the
kernel, where the different parts of the system are very tightly coupled.
SEPARATING RESOURCE MANAGERS
Parallel partitioning of resource-management-related facilities of a
mobile device leads to a design where individual resources are
managed by separate software modules.
These modules can then communicate with each other using messages,
leading to an architecture commonly referred to as message
passing architecture
One common implementation for this type of architecture is that all the modules run in a
process of their own. Inside a module, a thread is used to listen to messages.
Whenever a message is received, the thread executes a message-handling procedure, which
fundamentally is about event processing. It needs several context switching to avoid this we
introduce shared memory.
COMMON CONCERNS
There are several common concerns when considering resource use of a mobile device.
Many of them are derivatives of scarce hardware resources, but some of them can be traced to
the requirements of organizations developing mobile systems. For instance, one can consider
the following concerns:
Extension and adaptation is needed for being able to reuse the same code base
in different devices and contexts whose hardware and software characteristics and
available resources may differ. For instance, some devices can use hardwaresupported graphics
acceleration, whereas others use the main processor for this task.
of mobile devices as application platforms, optimization for a single purpose is harder than in a
purely embedded setting on the other hand.
• Energy management is an issue that arises when mobile devices are used more like
computers and less like single-purpose devices; being active consumes more memory.
• Internal resource management is needed for ensuring that the right resources are
available at the right time. Furthermore, it may be a necessity to introduce features for
handling hardware-specific properties. The issue is emphasized by the fact that in many cases,
resource management is always running in the background