SlideShare uma empresa Scribd logo
1 de 134
Idan Felix
10/08/2016
http://bit.ly/2aMrRzg
Wifi password : dotheimpossible
CPU & Battery
Most eclectic we had so far
5
First,
Idan Felix
I’m 33 years old
Varonis
Android Academy TLV
Idan Felix
I’m 33 years old
Varonis
Android Academy TLV
Yonatan Levin
Google Developer Expert &
Android @ Gett
Idan Felix
Senior Android & Redhead
Varonis
Jonathan Yarkoni
Android Developer &
Advocate
Ironsource
Android Academy Staff
Britt Barak
Android Lead
Real
Muiriel Felix
Android Design
I am selling out
LogisticsLogistics
Yossi Segev
Android
@
Crave
https://www.facebook.com/groups/android.academy.ils/
What’s next?
14/9 - Britt
- Threading
30 / 10 / 2016
New course coming
30 / 10 / 2016
New course coming
30 / 10 / 2016
New course coming
30 / 10 / 2016
New course coming
● +: Networking Debt.
● A: CPU
○ Theory
○ Practice
● B: Battery
○ Theory
○ Practice
10/08/2016
CPU & Battery
- Assess the problem and
establish acceptable behavior.
- Measure perf b4 modification.
- Identify bottleneck.
- Remove bottleneck.
- Measure perf after modification.
- If better, adopt.
If worse, put it back.
Methods of Systematic performance improvement
https://en.wikipedia.org/wiki/Performance_tuning
Bonus Part: Networking
+
“A man in debt is so far a slave.”
Ralph Waldo Emerson
Last month, Yonatan talked about
- Networks basics
- Retrofit 2.0
- Caching, Prefetching
- Bundling-up with Android (~ Sync Adapters),
Or at least playing nice (~ Job Scheduler)
- Doze (Will be revised
today)Really, check it out. https://docs.google.com/presentation/d/1maecLjfOOy38_VJWc2YKzoXGSR2ulF3KLG6wmoJ3OwE/edit?usp=sharing
Serialization
How objects move across domains
*
Serialization/Deserialization
Serialization Deserialization
JSON Class Representation
{
"starwars": {
"number_of_episodes": 1,
"realDarthVaderName": "Anakin Skywalker",
"nextEpisodeRelease": "01-12-2016 01:00:00+3:00GMT"
}
}
Sounds like a good idea, right?
Serialization/Deserialization
Advantage - It’s human readable.
And it’s its weakest point too.
GZipability
Memory overhead
GZip
When communicating with servers, it makes sense to compress the
data, and it’s quite easy to do so.
GZip is a GNU-Licensed compression algorithm, which is a merge of
2 compression algorithms: Lampel-Ziv-77 and Huffman-Codes,
That works in 32kb chunks.
GZip - LZ77 Concept - Back References
‫קול‬‫גלגל‬‫המתגלגל‬‫מלמטה‬
‫למעלה‬
‫מרכבות‬‫סתומות‬‫הולכות‬
‫ומתגלגלות‬
Credit for the example: Orevi Joe
2,28,4
7,27,210,2 49,4 41,2
5,2
How to mess up compressibility
When you’re sending arrays or lists of large objects, the JSON format
OOB has a lot of redundancy, and if the object is large, then some of
the references fail.
How to mess up compressibility - Solution
Consider crafting a DTO that would be optimized for smaller footprint
and compression.
Have numbers and similar strings close to each other.
Consider refactoring a list-of-object to an object-of-lists.
JSON Class Representation
[
{
"id": 314,
"shoe_size": 42,
"show_image": "-- 32kB Base64 representation of a PNG --",
},
{
"id": 42,
"shoe_size": 42,
"show_image": "-- 32kB Base64 representation of another PNG --",
},
{
"id": 456,
"shoe_size": 42,
"show_image": "-- 32kB Base64 representation of a different PNG --",
},
}
JSON Class Representation
{
"id": [314,42,456],
"shoe_sizes": [42,42,42],
"show_images": [
"-- 32kB Base64 representation of a PNG --",
"-- 32kB Base64 representation of another PNG --",
"-- 32kB Base64 representation of a different PNG --"
]
}
Json vs. Optimized-JSon
Human
Readability
Performance
JSon
Optimized
JSon
Flat
Buffers
- Faster
- Lighter
FlatBuffers
Colt McAnlis: https://youtu.be/IwxIIUypnTE
How it works?
Process
1.Create schema
2.Compile schema with flatc compiler
3.Import generated files into your project
4.Read from byte[]:
java.nio.ByteBuffer buf = builder.dataBuffer();
// Deserialize the data from the buffer.
Any Questions?
Any Questions?
● +: Networking Debt.
● A: CPU
○ Theory
○ Practice
● B: Battery
○ Theory
○ Practice
10/08/2016
CPU & Battery
Part A: CPU
A
“Tears come from the heart and not from the brain. ”
Leonardo Di Vinci
Understanding CPU
- How does my phone know Java?
- Big-O Notation and an intro to Data Structures
- Auto-Boxing
- Optimized Data Structures (Sparse Arrays and Maps)
- Things we can’t control
But before we begin,
something totally unrelated..
What is Eyjafjörður?
I wonder…
How does my phone know Java?
Well, IT DOESN’T
Build → Execute
On your PC, Gradle does a really complicated
Build process, that takes the Java Code and
Turns it into an APK.
We’ll go into what it does,
So we can appreciate the beauty.
Build → Execute
Simplified Walkthrough the build:
A. Manifest Merging
B. AAPT goes over the RES file and produces R.java and resources
C. Javac takes the code and produces a JAR file
D. ProGuard optimizes the JAR(s)
E.Dx takes the JAR(s) and produces a DEX file
F. AAPT takes everything (+ the Manifest), signs it and packs it to an APKRead more: https://developer.android.com/studio/build/index.html
The APK is unzipped on the device.
The customized Java Runtime loads and executes your code.
Build → Execute
If KitKat (4.4) and below
DALVIK
If Lollipop and above
ART
Remember this classic diagram?
Build → Execute
Build → Execute: Dalvik
Dalvik is a VM that..:
- uses dex (and odex) files to run programs.
Thanks, DEX, for using a short to index the method table. #65K
- Instead of being Stack based (Like the JVM), it is Register based.
- It’s main idea:
Being optimized for low memory consumption.
Build → Execute: Dalvik
And then there was Froyo (2.2)
Google added an amazing ability,
called JIT compiling (by trace).
JIT - Compile parts (~by trace) of the “ByteCode” to Machine-Code.
Performance was better - but the optimization options were limited.
Runtime-Perf: What to measure?
These are some of the KPIs for the Runtime:
- Performance
- App Startup Time
- Jank
- Battery Footprint
- Memory Footprint
Build → Execute: Art
The Art (~Android Runtime) uses the same dx format,
But instead of JIT-Compiling, uses AOT-Compiling.
When installing an app, the phone compiles the entire dex to
machine-code, and binds (statically link) whatever it needs
to the framework.
Build → Execute: Art
In N, ART does both JIT and AOT.
It profiles your code, and decides which
methods it should compile.
Since compilation is not a trivial task, it does it
only when the device is 100% charged and
connected to the charger.
Read: In Nougat, connecting your phone to a charger makes it work faster.
So, Is your phone a computer or not?
Why Yes:
It has a CPU, Memory, GPU, an OS,
It runs Java
Why No:
Not the same VM,
Not the same amounts of CPU, Memory,
Not the same usage patterns and expectations.
If it was a computer...
Big-O and Data Structures
This is what we were taught in Intro2CS, DataStructures, and etc.:
- Considering very large inputs (limn->∞) and ignore constants
- The Big O Notation: “1.02n” >> “n3 + 5n2” === “30n3” > “1000n2”
- Counting operations
- Accessing a memory cell by address is O(1)
Loops on some input are O(n),
- Trade-off Everything
Big-O and Data Structures
Big-O and Data Structures
Some rules still apply in mobile:
- Use the right algorithm,
- Use the right data-structure,
- Be Smart
Don’t forget that this is a mobile device.
Storing a list of numbers
You need to store n integers.
If you know what n is, use an array.
If you don’t - What do you do?
(Support insert-last
and get operation)
Possible Solution:
Create an array-like interface:
int get(int index)
void insertLast(int value)
Guess or get an initial size, and if needed,
Create a new array with more room,
copy the items, and use the new array.
Discuss.
Possible Solution, Now with Generics:
Create an array-like interface:
T get(int index)
void insert(T value, int index)
This is also called an ArrayList<T>.
Are there any
implications? Discuss.
How Generics Work
In Java,
The compiler translates your <T>s to Objects, and adds casting as
necessary.
So, you can’t use int (or boolean, or double, or long, …), so instead,
you use Integer (or Boolean, Double, or Long), which do the same
thing, more or less.
Now, consider these codes
int i = 5;
Integer i = 5;
int j = i + 2;
Integer j = i + 2;
What do these compile to?
Now, consider these codes
int i = 5;
Integer i = 5;
int j = i + 2;
Integer j = i + 2;
public void noBoxing();
Code:
0: iconst_5
1: istore_1
2: iload_1
3: iconst_2
4: iadd
5: istore_2
public void withBoxing();
Code:
0: iconst_5
1: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
4: astore_1
5: aload_1
6: invokevirtual #3 // Method java/lang/Integer.intValue:()I
9: iconst_2
10: iadd
11: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
14: astore_2
15: return
Now, consider this code
ArrayList<Integer> list =
new ArrayList<Integer>();
list.add(1);
list.add(2);
Same issue.
Now, consider this code
ArrayList<Integer> list = new ArrayList<Integer>();
For (int i = 0; i++; i < list.size()){
int value = list.get(i);
}
Same issue.
AutoBoxing
To handle previous code samples,
The runtime automagically takes your int, puts it inside a box, called
Integer and stores it in the heap.
This is AutoBoxing.
Colt McAnlis: https://youtu.be/dgzOwuoXVJU
AVOID BOXING
BEWARE of AUTO-BOXING
Colt McAnlis: https://youtu.be/dgzOwuoXVJU
Any Questions?
Storing a list of objects, by Key
You need to store n objects, by key.
If you know what n is, use an array.
If you don’t - What do you do?
(Support an insert and get operations)
Storing a list of objects, by Key
Consider this API:
void insert(key, value)
int get(key)
What’s in a key?
Consider this task:
You have a list of devices that you want to retrieve by their IMEI.
Here’s a sample IMEI: 53865487920334156 (17 digits)
It doesn’t make sense to have an array with 1017 cells…
Same for Creditcards, ID numbers, GUIDs, and mostly anything that has an id and there’s a lot of it in the world, and you
will not need them all
Here’s what we’ll do
- Squash the key to something we can handle (~HashCode),
Store according to the key
- Handle Conflicts somehow (Open-hash / Closed-hash)
- Handle resizing somehow
- If we add Generics, This will be called
java.util.HashMap<TKey, TValue>
Is it the right algorithm?
- Inserting: Amortized O(1), but up to O(n)
- Searching: Amortized O(1), but up to O(n)
- Memory Consumption - Not so good.
What’s wrong with it?
- If you’re using Integers or Longs (IMEIs), for keys or for values,
you might hit AutoBoxing.
- The “Entry” class (internal implementation detail) is another layer
of abstraction that gets allocated that we don’t need.
- Compacting and Expending are expensive
HashMap solution 1: ArrayMaps
ArrayMaps (from android.util) use a different algorithm:
It uses 2 arrays - one for the values and the keys, and one for the
hashes, which is kept sorted.
Colt McAnlis: https://youtu.be/ORgucLTtTDI
HashMap solution 1: ArrayMaps
ArrayMaps (from android.util) use a different algorithm:
It uses 2 arrays - one for the values and the keys, and one for the
hashes, which is kept sorted.
Insertion - O(n)
Searching - O(log n)
Colt McAnlis: https://youtu.be/ORgucLTtTDI
HashMap solution 1: ArrayMaps
ArrayMaps (from android.util) use a different algorithm:
It uses 2 arrays - one for the values and the keys, and one for the
hashes, which is kept sorted.
Insertion - O(n)
Searching - O(log n)
Memory consumption - much better, but autoboxing is still an issue
Colt McAnlis: https://youtu.be/ORgucLTtTDI
HashMap Solution #1: ArrayMaps
This data-structure is useful on mobile for 2 use-cases:
HashMap solution 2: SparseArrays and co.
Sparse Arrays are optimized for no-autoboxing, and use similar
algorithms for storage as ArrayMaps.
Use SparseArrays when the keys or the values are primitives (partial):
Colt McAnlis: https://youtu.be/I16lz26WyzQ
Key Type Value Type SparseArray
Integer Object SparseArray
Integer Boolean SparseBooleanArray
Long Object LongSparseArray
Important for job interviews
SparseArrays and Array-Maps:
GET - Requires binary search - O(log n)
INS - Require binary search and moving - O(n)
(and they also don’t retain order, ‫נבלות‬)
Another “what does this compiles to”
Consider the following code:
// from Iterators.foo();
ArrayList<Foo> foos = new ArrayList<Foo>();
for (Foo foo : foos){
// … Do something with foo
}
public void foo();
Code:
0: new #2 // class java/util/ArrayList
3: dup
4: invokespecial #3 // Method java/util/ArrayList."<init>":()V
7: astore_1
8: aload_1
9: invokevirtual #4 // Method java/util/ArrayList.iterator:()Ljava/util/Iterator;
12: astore_2
13: aload_2
14: invokeinterface #5, 1 // InterfaceMethod java/util/Iterator.hasNext:()Z
19: ifeq 35
22: aload_2
23: invokeinterface #6, 1 // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
28: checkcast #7 // class Foo
31: astore_3
32: goto 13
35: return
What does it compile to? (In Bytecode)
Now, Let’s compare
public void baz(){
ArrayList<Foo> foos = new ArrayList<Foo>();
int size = foos.size();
for (int i = 0; i < size; i++){
Foo foo = foos.get(i);
// Do something with foo...
}
}
Now, Let’s compare
public void baz();
Code:
0: new #2 // class java/util/ArrayList
3: dup
4: invokespecial #3 // Method java/util/ArrayList."<init>":()V
7: astore_1
8: aload_1
9: invokevirtual #8 // Method java/util/ArrayList.size:()I
12: istore_2
13: iconst_0
14: istore_3
15: iload_3
16: iload_2
17: if_icmpge 36
20: aload_1
21: iload_3
22: invokevirtual #9 // Method java/util/ArrayList.get:(I)Ljava/lang/Object;
25: checkcast #7 // class Foo
28: astore 4
30: iinc 3, 1
33: goto 15
36: return
Hmm. Android vs. Java?
- Java: use for(int i : list) because that’s how we do
it in java.
- Android: do what Java says, especially if you’re using
an iterator. https://developer.android.com/training/articles/perf-
tips.html#Loops
- Colt McAnlis:
Use a CS101 for loop, avoid the iterator if possible.
See for yourself: https://youtu.be/MZOf3pOAM6A
One thing we can’t control
Android can change the speed of the CPU to conserve battery.
Slower CPU ⇒ Battery lasts longer
This might cause 2 symptoms:
- Dropped frames (it takes 20ms for the CPU to speed-up again)
- Normal things take longer
ThisColt McAnlis: https://youtu.be/c8u3fEM3JG0
Any Questions?
Any Questions?
● +: Networking Debt.
● A: CPU
○ Theory
○ Practice
● B: Battery
○ Theory
○ Practice
10/08/2016
CPU & Battery
A recent study showed that 100% of your users don’t use
your app when their battery is out of juice.
The Battery
B
Understanding Battery
- Battery theory
- As A Developer, How to reduce battery usage
- As ANDROID, How to reduce battery usage
Measurement Units
Ampere (A) is a unit of electric current.
It describes how much power
something needs at an instant.
Milli-Ampere (mA) is the unit that
we actually use, because it’s a mobile
device, and things don’t use a lot of power.
Example: My GPS needs 140mA, and my 4G chip needs 180mA.
https://en.wikipedia.org/wiki/Ampere
Measurement Units
However, it doesn’t take time into considerations -
That’s why we use mAh.
Example:
Nexus 5X battery is 2,700 mAh
https://en.wikipedia.org/wiki/Ampere-hour
Battery Theory
My GPS needs 140mA, and my 4G chip needs 180mA.
Which is better for getting a location:
using the GPS or the 4G chip?
Battery Theory
My GPS needs 140mA, and my 4G chip needs 180mA.
Getting a GPS lock takes about 25 seconds, so:
25s x 140mA ~= 1mAh
Battery Theory
My GPS needs 140mA, and my 4G chip needs 180mA.
Getting network location lock takes about 2 seconds, so:
2s x 180mA ~= 0.1mAh
Jeffrey Sharkey: https://youtu.be/OUemfrKe65c (From Google I/O 2009)
So, What’s killing your battery?
The
Screen
The
Modem
The
GPS
Foreground work Background work
About the screen
So what can you do?
Taste of what’s possible: https://developer.android.com/reference/android/app/job/JobInfo.Builder.html
Reduce
Keep
background
activity to
minimum
Defer
Wait with the
activity until the
device is
charging
Coalesce
Try to piggy-back,
batch, queue or group
background work with
other background
tasks that has to be
done
The battery and the Radio
True or false:
3G and WIFI need (more or less)
the same amount of power ~180 mAh
The battery and the Radio
However, downloading 100mb
via 3G radio takes 1’56’’
and via WIFI 0’10’’
180mAh x 10sec = 0.5 mAh
180mAh x 116sec = 5.8 mAh
The battery and the Radio
Also:
- Radio wake-up costs a lot of power
- Radio has to stay awake for responses (and responses of responses)
- Radio goes to sleep after about ~20sec of activity.
Dive Deeper: Ran Nachmani @ Big Android BBQ 2015: https://youtu.be/LO_Swql0zVg
The battery and the Radio
So what can you do?
I/O 2016: https://youtu.be/VC2Hlb22mZM
Reduce Defer Coalesce
- If it’s not important - Don’t
- Reduce payload (GZIP, FlatBuffers)
- Use the JobScheduler to wait for
- Charger
- WIFI
- Piggy-back with
SyncAdapters
- Prefetch and Batch
Change usage pattern according to network state!
Tool: DDMS / ADM
The Android Device Monitor can really help in understanding what the
network is doing.
Any Questions?
GPS in action
Your device can find its locations using 3 devices:
- The GPS, which uses few satellites for exact location.
- The 3G network that uses cell-tower triangulation.
- Some WIFI networks can also hint of where you are.
Each of these is a location provider.
Most use the Fused Location Provider from Google Play services.
It merges all location providers for bests results and optimal
footprint.
protected void createLocationRequest() {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
GPS in action
https://developer.android.com/training/location/change-location-settings.html
GPS in action
True or false:
You might be updated on location
More frequent than you ask.
The battery and the GPS
We already talked about using the GPS vs the 3G data.
What else can we do?
More: Colt McAnlis: https://youtu.be/81W61JA6YHw
Reduce Defer Coalesce
- Ask for less accurate location
- Ask for less updates
- Limit number of callbacks
- Ask for less updates - Piggy-back other GPS
requests
Reduce the location battery footprint
of your app.
Set the priority for the accuracy you need.
PRIORITY_LOW_POWER (~10km "city" accuracy)
PRIORITY_BALANCED_POWER_ACCURACY (~100m "block" accuracy)
PRIORITY_HIGH_ACCURACY (accurate as possible at the expense of
battery life)
The battery and the GPS
https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest
Reduce
- Ask for less accurate location
- Ask for less updates
- Limit number of callbacks
The battery and the GPS
Reduce the location battery footprint
of your app.
Set the interval as high as you can.
mLocationRequest.setInterval(10000);
https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest
DeferReduce
- Ask for less accurate location
- Ask for less updates
- Limit number of callbacks
The battery and the GPS
Reduce the location battery footprint
of your app.
Also limit the number of callbacks.
mLocationRequest.setFastestInterval(5000);
https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest
Reduce
- Ask for less accurate location
- Ask for less updates
- Limit number of callbacks
The battery and the GPS
Reduce the location battery footprint
of your app.
Use PRIORITY_NO_POWER to passively listen for location updates
from other clients.
https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest
- Piggy-back other GPS requests
Coalesce
Any Questions?
So, What’s killing your battery?
The
Screen
The
Modem
The
GPS
Foreground work Background work
About the screen
THIS WAS A LIE
Wasting screen / cpu energy
Sometimes, you want to keep the device awake to do some work.
Perhaps you want the screen to stay on while watching a video, or wake-up from sleep to calculate
something periodically.
This can be done with WakeLocks:
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
Training: https://developer.android.com/training/scheduling/wakelock.html
Colt McAnlis: https://youtu.be/reMau7d0yeg
Wasting screen / cpu energy
However, it’s really important to handle WakeLocks with care.
- Beware of dependencies
(User data, server faults, etc.)
- Always use a timeout
Training: https://developer.android.com/training/scheduling/wakelock.html
Colt McAnlis: https://youtu.be/reMau7d0yeg
Short story
Friday, ~8:00AM
My phone has ~22%
Mr. Levin: “Put it on the floor in your bag, don’t touch it,
and let it go into DOZE.”
OK.
What does Android do
to reserve battery?
Doze
Introduced with Marshmallow (6.0, API 23),
https://developer.android.com/training/monitoring-device-state/doze-standby.html
Doze
When the device is in this state:
- Network access is suspended
- The device ignores wake locks
- Alarms (a la AlarmManager) don’t get execute (unless While-Idle or setAlarmClock)
- No SyncAdapters or JobSchedulers
- System also does not do Wifi Scans
https://developer.android.com/training/monitoring-device-state/doze-standby.html
So we listened to you for 2 hours for nothing?
Don’t be mad Don’t do
anything
If you have a problem with Doze, It means that you have a problem
with a simple fact:
Your users have life
They might forget their tablet at home when they leave for a 3 days
trip to Amsterdam, and would love for it to have battery when they
come back from their trip.
#TrueStory #FML
Doze
Joanna Smith, Big Android BBQ 2015: https://youtu.be/Rwshwq_vI1s
Doze
Besides that,
Don’t do anything.
If you’re using all the stuff we taught, you should be good
- SyncAdapters, JobSchedulers will work as expected.
- Alarms are to be watched, and other network access too.
Joanna Smith, Big Android BBQ 2015: https://youtu.be/Rwshwq_vI1s
Doze
These are the states that the phone can be in:
Joanna Smith, Big Android BBQ 2015: https://youtu.be/Rwshwq_vI1s
Active Inactive
Idle
Pending
Idle Idle
Maintenance
This transition takes ~2h
Remember my story? About a 45m lecture?
Doze
In Android Nougat,
The terms for entering Doze were loosened.
This means that even while the device is in the user’s
pocket it can go into Doze. Still same principle apply:
Don’t do anything about it.
Any Questions?
Felix stop!!! :)
To Amsterdam...You leave us...
Missing from this lecture
Missing from this lecture
https://youtu.be/dZL2LDAg5cs
Any Questions?
Idan Felix
10/08/2016
CPU & Battery
Most eclectic we had so far
5
Idan Felix
10/08/2016
Everything is Trade-off
Be as smart as you can be.
5
Idan Felix
10/08/2016
Drive Home Safely.
5

Mais conteúdo relacionado

Semelhante a Performance #5 cpu and battery

NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...
NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...
NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...Hafez Kamal
 
Prometheus as exposition format for eBPF programs running on Kubernetes
Prometheus as exposition format for eBPF programs running on KubernetesPrometheus as exposition format for eBPF programs running on Kubernetes
Prometheus as exposition format for eBPF programs running on KubernetesLeonardo Di Donato
 
Lab Handson: Power your Creations with Intel Edison!
Lab Handson: Power your Creations with Intel Edison!Lab Handson: Power your Creations with Intel Edison!
Lab Handson: Power your Creations with Intel Edison!Codemotion
 
carrow - Go bindings to Apache Arrow via C++-API
carrow - Go bindings to Apache Arrow via C++-APIcarrow - Go bindings to Apache Arrow via C++-API
carrow - Go bindings to Apache Arrow via C++-APIYoni Davidson
 
Designing A Project Using Java Programming
Designing A Project Using Java ProgrammingDesigning A Project Using Java Programming
Designing A Project Using Java ProgrammingKaty Allen
 
Questions On The Code And Core Module
Questions On The Code And Core ModuleQuestions On The Code And Core Module
Questions On The Code And Core ModuleKatie Gulley
 
1 Project 2 Introduction - the SeaPort Project seri.docx
1  Project 2 Introduction - the SeaPort Project seri.docx1  Project 2 Introduction - the SeaPort Project seri.docx
1 Project 2 Introduction - the SeaPort Project seri.docxhoney725342
 
JavaScript Miller Columns
JavaScript Miller ColumnsJavaScript Miller Columns
JavaScript Miller ColumnsJonathan Fine
 
Full-stack Web Development with MongoDB, Node.js and AWS
Full-stack Web Development with MongoDB, Node.js and AWSFull-stack Web Development with MongoDB, Node.js and AWS
Full-stack Web Development with MongoDB, Node.js and AWSMongoDB
 
Test-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptxTest-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptxVictor Rentea
 
Who pulls the strings?
Who pulls the strings?Who pulls the strings?
Who pulls the strings?Ronny
 
DevSecCon SG 2018 Fabian Presentation Slides
DevSecCon SG 2018 Fabian Presentation SlidesDevSecCon SG 2018 Fabian Presentation Slides
DevSecCon SG 2018 Fabian Presentation SlidesFab L
 
JS Fest 2019. Ryan Dahl. Deno, a new way to JavaScript
JS Fest 2019. Ryan Dahl. Deno, a new way to JavaScriptJS Fest 2019. Ryan Dahl. Deno, a new way to JavaScript
JS Fest 2019. Ryan Dahl. Deno, a new way to JavaScriptJSFestUA
 
Eric Lafortune - The Jack and Jill build system
Eric Lafortune - The Jack and Jill build systemEric Lafortune - The Jack and Jill build system
Eric Lafortune - The Jack and Jill build systemGuardSquare
 
Aura LA GDG - July 17-2017
Aura LA GDG - July 17-2017Aura LA GDG - July 17-2017
Aura LA GDG - July 17-2017Kristan Uccello
 
Easy way to learn Core java full material
Easy way to learn Core java full materialEasy way to learn Core java full material
Easy way to learn Core java full materialSivannarayana Chimata
 

Semelhante a Performance #5 cpu and battery (20)

NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...
NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...
NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...
 
Prometheus as exposition format for eBPF programs running on Kubernetes
Prometheus as exposition format for eBPF programs running on KubernetesPrometheus as exposition format for eBPF programs running on Kubernetes
Prometheus as exposition format for eBPF programs running on Kubernetes
 
Lab Handson: Power your Creations with Intel Edison!
Lab Handson: Power your Creations with Intel Edison!Lab Handson: Power your Creations with Intel Edison!
Lab Handson: Power your Creations with Intel Edison!
 
Tdd is not about testing
Tdd is not about testingTdd is not about testing
Tdd is not about testing
 
carrow - Go bindings to Apache Arrow via C++-API
carrow - Go bindings to Apache Arrow via C++-APIcarrow - Go bindings to Apache Arrow via C++-API
carrow - Go bindings to Apache Arrow via C++-API
 
Designing A Project Using Java Programming
Designing A Project Using Java ProgrammingDesigning A Project Using Java Programming
Designing A Project Using Java Programming
 
Questions On The Code And Core Module
Questions On The Code And Core ModuleQuestions On The Code And Core Module
Questions On The Code And Core Module
 
1 Project 2 Introduction - the SeaPort Project seri.docx
1  Project 2 Introduction - the SeaPort Project seri.docx1  Project 2 Introduction - the SeaPort Project seri.docx
1 Project 2 Introduction - the SeaPort Project seri.docx
 
JavaScript Miller Columns
JavaScript Miller ColumnsJavaScript Miller Columns
JavaScript Miller Columns
 
Big data made easy with a Spark
Big data made easy with a SparkBig data made easy with a Spark
Big data made easy with a Spark
 
Full-stack Web Development with MongoDB, Node.js and AWS
Full-stack Web Development with MongoDB, Node.js and AWSFull-stack Web Development with MongoDB, Node.js and AWS
Full-stack Web Development with MongoDB, Node.js and AWS
 
Getting Native with NDK
Getting Native with NDKGetting Native with NDK
Getting Native with NDK
 
IntroML_2.
IntroML_2.IntroML_2.
IntroML_2.
 
Test-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptxTest-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptx
 
Who pulls the strings?
Who pulls the strings?Who pulls the strings?
Who pulls the strings?
 
DevSecCon SG 2018 Fabian Presentation Slides
DevSecCon SG 2018 Fabian Presentation SlidesDevSecCon SG 2018 Fabian Presentation Slides
DevSecCon SG 2018 Fabian Presentation Slides
 
JS Fest 2019. Ryan Dahl. Deno, a new way to JavaScript
JS Fest 2019. Ryan Dahl. Deno, a new way to JavaScriptJS Fest 2019. Ryan Dahl. Deno, a new way to JavaScript
JS Fest 2019. Ryan Dahl. Deno, a new way to JavaScript
 
Eric Lafortune - The Jack and Jill build system
Eric Lafortune - The Jack and Jill build systemEric Lafortune - The Jack and Jill build system
Eric Lafortune - The Jack and Jill build system
 
Aura LA GDG - July 17-2017
Aura LA GDG - July 17-2017Aura LA GDG - July 17-2017
Aura LA GDG - July 17-2017
 
Easy way to learn Core java full material
Easy way to learn Core java full materialEasy way to learn Core java full material
Easy way to learn Core java full material
 

Mais de Vitali Pekelis

Droidkaigi2019thagikura 190208135940
Droidkaigi2019thagikura 190208135940Droidkaigi2019thagikura 190208135940
Droidkaigi2019thagikura 190208135940Vitali Pekelis
 
Google i o &amp; android q changes 2019
Google i o &amp; android q changes 2019Google i o &amp; android q changes 2019
Google i o &amp; android q changes 2019Vitali Pekelis
 
Advanced #6 clean architecture
Advanced #6  clean architectureAdvanced #6  clean architecture
Advanced #6 clean architectureVitali Pekelis
 
Advanced #4 GPU & Animations
Advanced #4   GPU & AnimationsAdvanced #4   GPU & Animations
Advanced #4 GPU & AnimationsVitali Pekelis
 
Advanced #2 networking
Advanced #2   networkingAdvanced #2   networking
Advanced #2 networkingVitali Pekelis
 
Advanced #1 cpu, memory
Advanced #1   cpu, memoryAdvanced #1   cpu, memory
Advanced #1 cpu, memoryVitali Pekelis
 
All the support you need. Support libs in Android
All the support you need. Support libs in AndroidAll the support you need. Support libs in Android
All the support you need. Support libs in AndroidVitali Pekelis
 
How to build Sdk? Best practices
How to build Sdk? Best practicesHow to build Sdk? Best practices
How to build Sdk? Best practicesVitali Pekelis
 
Android design patterns
Android design patternsAndroid design patterns
Android design patternsVitali Pekelis
 
Advanced #3 threading
Advanced #3  threading Advanced #3  threading
Advanced #3 threading Vitali Pekelis
 
Mobile ui fruit or delicious sweets
Mobile ui  fruit or delicious sweetsMobile ui  fruit or delicious sweets
Mobile ui fruit or delicious sweetsVitali Pekelis
 
Lecture #4 c loaders and co.
Lecture #4 c   loaders and co.Lecture #4 c   loaders and co.
Lecture #4 c loaders and co.Vitali Pekelis
 
Session #4 b content providers
Session #4 b  content providersSession #4 b  content providers
Session #4 b content providersVitali Pekelis
 
Advanced #2 - ui perf
 Advanced #2 - ui perf Advanced #2 - ui perf
Advanced #2 - ui perfVitali Pekelis
 
Android design lecture #3
Android design   lecture #3Android design   lecture #3
Android design lecture #3Vitali Pekelis
 

Mais de Vitali Pekelis (20)

Droidkaigi2019thagikura 190208135940
Droidkaigi2019thagikura 190208135940Droidkaigi2019thagikura 190208135940
Droidkaigi2019thagikura 190208135940
 
Droidkaigi 2019
Droidkaigi 2019Droidkaigi 2019
Droidkaigi 2019
 
Google i o &amp; android q changes 2019
Google i o &amp; android q changes 2019Google i o &amp; android q changes 2019
Google i o &amp; android q changes 2019
 
Android Q 2019
Android Q 2019Android Q 2019
Android Q 2019
 
Advanced #6 clean architecture
Advanced #6  clean architectureAdvanced #6  clean architecture
Advanced #6 clean architecture
 
Advanced #4 GPU & Animations
Advanced #4   GPU & AnimationsAdvanced #4   GPU & Animations
Advanced #4 GPU & Animations
 
Advanced #2 networking
Advanced #2   networkingAdvanced #2   networking
Advanced #2 networking
 
Advanced #2 threading
Advanced #2   threadingAdvanced #2   threading
Advanced #2 threading
 
Advanced #1 cpu, memory
Advanced #1   cpu, memoryAdvanced #1   cpu, memory
Advanced #1 cpu, memory
 
All the support you need. Support libs in Android
All the support you need. Support libs in AndroidAll the support you need. Support libs in Android
All the support you need. Support libs in Android
 
How to build Sdk? Best practices
How to build Sdk? Best practicesHow to build Sdk? Best practices
How to build Sdk? Best practices
 
Di &amp; dagger
Di &amp; daggerDi &amp; dagger
Di &amp; dagger
 
Android design patterns
Android design patternsAndroid design patterns
Android design patterns
 
Advanced #3 threading
Advanced #3  threading Advanced #3  threading
Advanced #3 threading
 
Mobile ui fruit or delicious sweets
Mobile ui  fruit or delicious sweetsMobile ui  fruit or delicious sweets
Mobile ui fruit or delicious sweets
 
Lecture #4 c loaders and co.
Lecture #4 c   loaders and co.Lecture #4 c   loaders and co.
Lecture #4 c loaders and co.
 
Session #4 b content providers
Session #4 b  content providersSession #4 b  content providers
Session #4 b content providers
 
Advanced #2 - ui perf
 Advanced #2 - ui perf Advanced #2 - ui perf
Advanced #2 - ui perf
 
Android meetup
Android meetupAndroid meetup
Android meetup
 
Android design lecture #3
Android design   lecture #3Android design   lecture #3
Android design lecture #3
 

Performance #5 cpu and battery

  • 1. Idan Felix 10/08/2016 http://bit.ly/2aMrRzg Wifi password : dotheimpossible CPU & Battery Most eclectic we had so far 5
  • 3. Idan Felix I’m 33 years old Varonis Android Academy TLV
  • 4. Idan Felix I’m 33 years old Varonis Android Academy TLV
  • 5. Yonatan Levin Google Developer Expert & Android @ Gett Idan Felix Senior Android & Redhead Varonis Jonathan Yarkoni Android Developer & Advocate Ironsource Android Academy Staff Britt Barak Android Lead Real Muiriel Felix Android Design
  • 10. What’s next? 14/9 - Britt - Threading
  • 11.
  • 12. 30 / 10 / 2016 New course coming 30 / 10 / 2016 New course coming
  • 13. 30 / 10 / 2016 New course coming 30 / 10 / 2016 New course coming ● +: Networking Debt. ● A: CPU ○ Theory ○ Practice ● B: Battery ○ Theory ○ Practice 10/08/2016 CPU & Battery
  • 14. - Assess the problem and establish acceptable behavior. - Measure perf b4 modification. - Identify bottleneck. - Remove bottleneck. - Measure perf after modification. - If better, adopt. If worse, put it back. Methods of Systematic performance improvement https://en.wikipedia.org/wiki/Performance_tuning
  • 15. Bonus Part: Networking + “A man in debt is so far a slave.” Ralph Waldo Emerson
  • 16. Last month, Yonatan talked about - Networks basics - Retrofit 2.0 - Caching, Prefetching - Bundling-up with Android (~ Sync Adapters), Or at least playing nice (~ Job Scheduler) - Doze (Will be revised today)Really, check it out. https://docs.google.com/presentation/d/1maecLjfOOy38_VJWc2YKzoXGSR2ulF3KLG6wmoJ3OwE/edit?usp=sharing
  • 17. Serialization How objects move across domains *
  • 19. JSON Class Representation { "starwars": { "number_of_episodes": 1, "realDarthVaderName": "Anakin Skywalker", "nextEpisodeRelease": "01-12-2016 01:00:00+3:00GMT" } }
  • 20. Sounds like a good idea, right?
  • 21. Serialization/Deserialization Advantage - It’s human readable. And it’s its weakest point too. GZipability Memory overhead
  • 22. GZip When communicating with servers, it makes sense to compress the data, and it’s quite easy to do so. GZip is a GNU-Licensed compression algorithm, which is a merge of 2 compression algorithms: Lampel-Ziv-77 and Huffman-Codes, That works in 32kb chunks.
  • 23. GZip - LZ77 Concept - Back References ‫קול‬‫גלגל‬‫המתגלגל‬‫מלמטה‬ ‫למעלה‬ ‫מרכבות‬‫סתומות‬‫הולכות‬ ‫ומתגלגלות‬ Credit for the example: Orevi Joe 2,28,4 7,27,210,2 49,4 41,2 5,2
  • 24. How to mess up compressibility When you’re sending arrays or lists of large objects, the JSON format OOB has a lot of redundancy, and if the object is large, then some of the references fail.
  • 25. How to mess up compressibility - Solution Consider crafting a DTO that would be optimized for smaller footprint and compression. Have numbers and similar strings close to each other. Consider refactoring a list-of-object to an object-of-lists.
  • 26. JSON Class Representation [ { "id": 314, "shoe_size": 42, "show_image": "-- 32kB Base64 representation of a PNG --", }, { "id": 42, "shoe_size": 42, "show_image": "-- 32kB Base64 representation of another PNG --", }, { "id": 456, "shoe_size": 42, "show_image": "-- 32kB Base64 representation of a different PNG --", }, }
  • 27. JSON Class Representation { "id": [314,42,456], "shoe_sizes": [42,42,42], "show_images": [ "-- 32kB Base64 representation of a PNG --", "-- 32kB Base64 representation of another PNG --", "-- 32kB Base64 representation of a different PNG --" ] }
  • 29. - Faster - Lighter FlatBuffers Colt McAnlis: https://youtu.be/IwxIIUypnTE
  • 31. Process 1.Create schema 2.Compile schema with flatc compiler 3.Import generated files into your project 4.Read from byte[]: java.nio.ByteBuffer buf = builder.dataBuffer(); // Deserialize the data from the buffer.
  • 33. Any Questions? ● +: Networking Debt. ● A: CPU ○ Theory ○ Practice ● B: Battery ○ Theory ○ Practice 10/08/2016 CPU & Battery
  • 34. Part A: CPU A “Tears come from the heart and not from the brain. ” Leonardo Di Vinci
  • 35. Understanding CPU - How does my phone know Java? - Big-O Notation and an intro to Data Structures - Auto-Boxing - Optimized Data Structures (Sparse Arrays and Maps) - Things we can’t control
  • 36. But before we begin, something totally unrelated.. What is Eyjafjörður?
  • 37. I wonder… How does my phone know Java? Well, IT DOESN’T
  • 38. Build → Execute On your PC, Gradle does a really complicated Build process, that takes the Java Code and Turns it into an APK. We’ll go into what it does, So we can appreciate the beauty.
  • 39. Build → Execute Simplified Walkthrough the build: A. Manifest Merging B. AAPT goes over the RES file and produces R.java and resources C. Javac takes the code and produces a JAR file D. ProGuard optimizes the JAR(s) E.Dx takes the JAR(s) and produces a DEX file F. AAPT takes everything (+ the Manifest), signs it and packs it to an APKRead more: https://developer.android.com/studio/build/index.html
  • 40. The APK is unzipped on the device. The customized Java Runtime loads and executes your code. Build → Execute If KitKat (4.4) and below DALVIK If Lollipop and above ART
  • 41. Remember this classic diagram? Build → Execute
  • 42. Build → Execute: Dalvik Dalvik is a VM that..: - uses dex (and odex) files to run programs. Thanks, DEX, for using a short to index the method table. #65K - Instead of being Stack based (Like the JVM), it is Register based. - It’s main idea: Being optimized for low memory consumption.
  • 43. Build → Execute: Dalvik And then there was Froyo (2.2) Google added an amazing ability, called JIT compiling (by trace). JIT - Compile parts (~by trace) of the “ByteCode” to Machine-Code. Performance was better - but the optimization options were limited.
  • 44. Runtime-Perf: What to measure? These are some of the KPIs for the Runtime: - Performance - App Startup Time - Jank - Battery Footprint - Memory Footprint
  • 45. Build → Execute: Art The Art (~Android Runtime) uses the same dx format, But instead of JIT-Compiling, uses AOT-Compiling. When installing an app, the phone compiles the entire dex to machine-code, and binds (statically link) whatever it needs to the framework.
  • 46.
  • 47.
  • 48. Build → Execute: Art In N, ART does both JIT and AOT. It profiles your code, and decides which methods it should compile. Since compilation is not a trivial task, it does it only when the device is 100% charged and connected to the charger. Read: In Nougat, connecting your phone to a charger makes it work faster.
  • 49. So, Is your phone a computer or not? Why Yes: It has a CPU, Memory, GPU, an OS, It runs Java Why No: Not the same VM, Not the same amounts of CPU, Memory, Not the same usage patterns and expectations.
  • 50. If it was a computer...
  • 51. Big-O and Data Structures This is what we were taught in Intro2CS, DataStructures, and etc.: - Considering very large inputs (limn->∞) and ignore constants - The Big O Notation: “1.02n” >> “n3 + 5n2” === “30n3” > “1000n2” - Counting operations - Accessing a memory cell by address is O(1) Loops on some input are O(n), - Trade-off Everything
  • 52. Big-O and Data Structures
  • 53. Big-O and Data Structures Some rules still apply in mobile: - Use the right algorithm, - Use the right data-structure, - Be Smart Don’t forget that this is a mobile device.
  • 54. Storing a list of numbers You need to store n integers. If you know what n is, use an array. If you don’t - What do you do? (Support insert-last and get operation)
  • 55. Possible Solution: Create an array-like interface: int get(int index) void insertLast(int value) Guess or get an initial size, and if needed, Create a new array with more room, copy the items, and use the new array. Discuss.
  • 56. Possible Solution, Now with Generics: Create an array-like interface: T get(int index) void insert(T value, int index) This is also called an ArrayList<T>. Are there any implications? Discuss.
  • 57. How Generics Work In Java, The compiler translates your <T>s to Objects, and adds casting as necessary. So, you can’t use int (or boolean, or double, or long, …), so instead, you use Integer (or Boolean, Double, or Long), which do the same thing, more or less.
  • 58. Now, consider these codes int i = 5; Integer i = 5; int j = i + 2; Integer j = i + 2; What do these compile to?
  • 59. Now, consider these codes int i = 5; Integer i = 5; int j = i + 2; Integer j = i + 2; public void noBoxing(); Code: 0: iconst_5 1: istore_1 2: iload_1 3: iconst_2 4: iadd 5: istore_2 public void withBoxing(); Code: 0: iconst_5 1: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 4: astore_1 5: aload_1 6: invokevirtual #3 // Method java/lang/Integer.intValue:()I 9: iconst_2 10: iadd 11: invokestatic #2 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 14: astore_2 15: return
  • 60. Now, consider this code ArrayList<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); Same issue.
  • 61. Now, consider this code ArrayList<Integer> list = new ArrayList<Integer>(); For (int i = 0; i++; i < list.size()){ int value = list.get(i); } Same issue.
  • 62. AutoBoxing To handle previous code samples, The runtime automagically takes your int, puts it inside a box, called Integer and stores it in the heap. This is AutoBoxing. Colt McAnlis: https://youtu.be/dgzOwuoXVJU
  • 63. AVOID BOXING BEWARE of AUTO-BOXING Colt McAnlis: https://youtu.be/dgzOwuoXVJU
  • 65. Storing a list of objects, by Key You need to store n objects, by key. If you know what n is, use an array. If you don’t - What do you do? (Support an insert and get operations)
  • 66. Storing a list of objects, by Key Consider this API: void insert(key, value) int get(key)
  • 67. What’s in a key? Consider this task: You have a list of devices that you want to retrieve by their IMEI. Here’s a sample IMEI: 53865487920334156 (17 digits) It doesn’t make sense to have an array with 1017 cells… Same for Creditcards, ID numbers, GUIDs, and mostly anything that has an id and there’s a lot of it in the world, and you will not need them all
  • 68. Here’s what we’ll do - Squash the key to something we can handle (~HashCode), Store according to the key - Handle Conflicts somehow (Open-hash / Closed-hash) - Handle resizing somehow - If we add Generics, This will be called java.util.HashMap<TKey, TValue>
  • 69. Is it the right algorithm? - Inserting: Amortized O(1), but up to O(n) - Searching: Amortized O(1), but up to O(n) - Memory Consumption - Not so good.
  • 70. What’s wrong with it? - If you’re using Integers or Longs (IMEIs), for keys or for values, you might hit AutoBoxing. - The “Entry” class (internal implementation detail) is another layer of abstraction that gets allocated that we don’t need. - Compacting and Expending are expensive
  • 71. HashMap solution 1: ArrayMaps ArrayMaps (from android.util) use a different algorithm: It uses 2 arrays - one for the values and the keys, and one for the hashes, which is kept sorted. Colt McAnlis: https://youtu.be/ORgucLTtTDI
  • 72. HashMap solution 1: ArrayMaps ArrayMaps (from android.util) use a different algorithm: It uses 2 arrays - one for the values and the keys, and one for the hashes, which is kept sorted. Insertion - O(n) Searching - O(log n) Colt McAnlis: https://youtu.be/ORgucLTtTDI
  • 73. HashMap solution 1: ArrayMaps ArrayMaps (from android.util) use a different algorithm: It uses 2 arrays - one for the values and the keys, and one for the hashes, which is kept sorted. Insertion - O(n) Searching - O(log n) Memory consumption - much better, but autoboxing is still an issue Colt McAnlis: https://youtu.be/ORgucLTtTDI
  • 74. HashMap Solution #1: ArrayMaps This data-structure is useful on mobile for 2 use-cases:
  • 75. HashMap solution 2: SparseArrays and co. Sparse Arrays are optimized for no-autoboxing, and use similar algorithms for storage as ArrayMaps. Use SparseArrays when the keys or the values are primitives (partial): Colt McAnlis: https://youtu.be/I16lz26WyzQ Key Type Value Type SparseArray Integer Object SparseArray Integer Boolean SparseBooleanArray Long Object LongSparseArray
  • 76. Important for job interviews SparseArrays and Array-Maps: GET - Requires binary search - O(log n) INS - Require binary search and moving - O(n) (and they also don’t retain order, ‫נבלות‬)
  • 77. Another “what does this compiles to” Consider the following code: // from Iterators.foo(); ArrayList<Foo> foos = new ArrayList<Foo>(); for (Foo foo : foos){ // … Do something with foo }
  • 78. public void foo(); Code: 0: new #2 // class java/util/ArrayList 3: dup 4: invokespecial #3 // Method java/util/ArrayList."<init>":()V 7: astore_1 8: aload_1 9: invokevirtual #4 // Method java/util/ArrayList.iterator:()Ljava/util/Iterator; 12: astore_2 13: aload_2 14: invokeinterface #5, 1 // InterfaceMethod java/util/Iterator.hasNext:()Z 19: ifeq 35 22: aload_2 23: invokeinterface #6, 1 // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object; 28: checkcast #7 // class Foo 31: astore_3 32: goto 13 35: return What does it compile to? (In Bytecode)
  • 79. Now, Let’s compare public void baz(){ ArrayList<Foo> foos = new ArrayList<Foo>(); int size = foos.size(); for (int i = 0; i < size; i++){ Foo foo = foos.get(i); // Do something with foo... } }
  • 80. Now, Let’s compare public void baz(); Code: 0: new #2 // class java/util/ArrayList 3: dup 4: invokespecial #3 // Method java/util/ArrayList."<init>":()V 7: astore_1 8: aload_1 9: invokevirtual #8 // Method java/util/ArrayList.size:()I 12: istore_2 13: iconst_0 14: istore_3 15: iload_3 16: iload_2 17: if_icmpge 36 20: aload_1 21: iload_3 22: invokevirtual #9 // Method java/util/ArrayList.get:(I)Ljava/lang/Object; 25: checkcast #7 // class Foo 28: astore 4 30: iinc 3, 1 33: goto 15 36: return
  • 81. Hmm. Android vs. Java? - Java: use for(int i : list) because that’s how we do it in java. - Android: do what Java says, especially if you’re using an iterator. https://developer.android.com/training/articles/perf- tips.html#Loops - Colt McAnlis: Use a CS101 for loop, avoid the iterator if possible. See for yourself: https://youtu.be/MZOf3pOAM6A
  • 82. One thing we can’t control Android can change the speed of the CPU to conserve battery. Slower CPU ⇒ Battery lasts longer This might cause 2 symptoms: - Dropped frames (it takes 20ms for the CPU to speed-up again) - Normal things take longer ThisColt McAnlis: https://youtu.be/c8u3fEM3JG0
  • 84. Any Questions? ● +: Networking Debt. ● A: CPU ○ Theory ○ Practice ● B: Battery ○ Theory ○ Practice 10/08/2016 CPU & Battery
  • 85. A recent study showed that 100% of your users don’t use your app when their battery is out of juice. The Battery B
  • 86. Understanding Battery - Battery theory - As A Developer, How to reduce battery usage - As ANDROID, How to reduce battery usage
  • 87. Measurement Units Ampere (A) is a unit of electric current. It describes how much power something needs at an instant. Milli-Ampere (mA) is the unit that we actually use, because it’s a mobile device, and things don’t use a lot of power. Example: My GPS needs 140mA, and my 4G chip needs 180mA. https://en.wikipedia.org/wiki/Ampere
  • 88. Measurement Units However, it doesn’t take time into considerations - That’s why we use mAh. Example: Nexus 5X battery is 2,700 mAh https://en.wikipedia.org/wiki/Ampere-hour
  • 89. Battery Theory My GPS needs 140mA, and my 4G chip needs 180mA. Which is better for getting a location: using the GPS or the 4G chip?
  • 90. Battery Theory My GPS needs 140mA, and my 4G chip needs 180mA. Getting a GPS lock takes about 25 seconds, so: 25s x 140mA ~= 1mAh
  • 91. Battery Theory My GPS needs 140mA, and my 4G chip needs 180mA. Getting network location lock takes about 2 seconds, so: 2s x 180mA ~= 0.1mAh Jeffrey Sharkey: https://youtu.be/OUemfrKe65c (From Google I/O 2009)
  • 92. So, What’s killing your battery? The Screen The Modem The GPS Foreground work Background work
  • 94. So what can you do? Taste of what’s possible: https://developer.android.com/reference/android/app/job/JobInfo.Builder.html Reduce Keep background activity to minimum Defer Wait with the activity until the device is charging Coalesce Try to piggy-back, batch, queue or group background work with other background tasks that has to be done
  • 95. The battery and the Radio True or false: 3G and WIFI need (more or less) the same amount of power ~180 mAh
  • 96. The battery and the Radio However, downloading 100mb via 3G radio takes 1’56’’ and via WIFI 0’10’’ 180mAh x 10sec = 0.5 mAh 180mAh x 116sec = 5.8 mAh
  • 97. The battery and the Radio Also: - Radio wake-up costs a lot of power - Radio has to stay awake for responses (and responses of responses) - Radio goes to sleep after about ~20sec of activity. Dive Deeper: Ran Nachmani @ Big Android BBQ 2015: https://youtu.be/LO_Swql0zVg
  • 98. The battery and the Radio So what can you do? I/O 2016: https://youtu.be/VC2Hlb22mZM Reduce Defer Coalesce - If it’s not important - Don’t - Reduce payload (GZIP, FlatBuffers) - Use the JobScheduler to wait for - Charger - WIFI - Piggy-back with SyncAdapters - Prefetch and Batch Change usage pattern according to network state!
  • 99. Tool: DDMS / ADM The Android Device Monitor can really help in understanding what the network is doing.
  • 101. GPS in action Your device can find its locations using 3 devices: - The GPS, which uses few satellites for exact location. - The 3G network that uses cell-tower triangulation. - Some WIFI networks can also hint of where you are. Each of these is a location provider.
  • 102. Most use the Fused Location Provider from Google Play services. It merges all location providers for bests results and optimal footprint. protected void createLocationRequest() { LocationRequest mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(10000); mLocationRequest.setFastestInterval(5000); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); } GPS in action https://developer.android.com/training/location/change-location-settings.html
  • 103. GPS in action True or false: You might be updated on location More frequent than you ask.
  • 104. The battery and the GPS We already talked about using the GPS vs the 3G data. What else can we do? More: Colt McAnlis: https://youtu.be/81W61JA6YHw Reduce Defer Coalesce - Ask for less accurate location - Ask for less updates - Limit number of callbacks - Ask for less updates - Piggy-back other GPS requests
  • 105. Reduce the location battery footprint of your app. Set the priority for the accuracy you need. PRIORITY_LOW_POWER (~10km "city" accuracy) PRIORITY_BALANCED_POWER_ACCURACY (~100m "block" accuracy) PRIORITY_HIGH_ACCURACY (accurate as possible at the expense of battery life) The battery and the GPS https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest Reduce - Ask for less accurate location - Ask for less updates - Limit number of callbacks
  • 106. The battery and the GPS Reduce the location battery footprint of your app. Set the interval as high as you can. mLocationRequest.setInterval(10000); https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest DeferReduce - Ask for less accurate location - Ask for less updates - Limit number of callbacks
  • 107. The battery and the GPS Reduce the location battery footprint of your app. Also limit the number of callbacks. mLocationRequest.setFastestInterval(5000); https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest Reduce - Ask for less accurate location - Ask for less updates - Limit number of callbacks
  • 108. The battery and the GPS Reduce the location battery footprint of your app. Use PRIORITY_NO_POWER to passively listen for location updates from other clients. https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest - Piggy-back other GPS requests Coalesce
  • 110. So, What’s killing your battery? The Screen The Modem The GPS Foreground work Background work
  • 111. About the screen THIS WAS A LIE
  • 112. Wasting screen / cpu energy Sometimes, you want to keep the device awake to do some work. Perhaps you want the screen to stay on while watching a video, or wake-up from sleep to calculate something periodically. This can be done with WakeLocks: PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag"); wakeLock.acquire(); Training: https://developer.android.com/training/scheduling/wakelock.html Colt McAnlis: https://youtu.be/reMau7d0yeg
  • 113. Wasting screen / cpu energy However, it’s really important to handle WakeLocks with care. - Beware of dependencies (User data, server faults, etc.) - Always use a timeout Training: https://developer.android.com/training/scheduling/wakelock.html Colt McAnlis: https://youtu.be/reMau7d0yeg
  • 114.
  • 115. Short story Friday, ~8:00AM My phone has ~22% Mr. Levin: “Put it on the floor in your bag, don’t touch it, and let it go into DOZE.”
  • 116. OK. What does Android do to reserve battery?
  • 117. Doze Introduced with Marshmallow (6.0, API 23), https://developer.android.com/training/monitoring-device-state/doze-standby.html
  • 118. Doze When the device is in this state: - Network access is suspended - The device ignores wake locks - Alarms (a la AlarmManager) don’t get execute (unless While-Idle or setAlarmClock) - No SyncAdapters or JobSchedulers - System also does not do Wifi Scans https://developer.android.com/training/monitoring-device-state/doze-standby.html
  • 119. So we listened to you for 2 hours for nothing?
  • 120. Don’t be mad Don’t do anything
  • 121. If you have a problem with Doze, It means that you have a problem with a simple fact: Your users have life They might forget their tablet at home when they leave for a 3 days trip to Amsterdam, and would love for it to have battery when they come back from their trip. #TrueStory #FML Doze Joanna Smith, Big Android BBQ 2015: https://youtu.be/Rwshwq_vI1s
  • 122. Doze Besides that, Don’t do anything. If you’re using all the stuff we taught, you should be good - SyncAdapters, JobSchedulers will work as expected. - Alarms are to be watched, and other network access too. Joanna Smith, Big Android BBQ 2015: https://youtu.be/Rwshwq_vI1s
  • 123. Doze These are the states that the phone can be in: Joanna Smith, Big Android BBQ 2015: https://youtu.be/Rwshwq_vI1s Active Inactive Idle Pending Idle Idle Maintenance This transition takes ~2h Remember my story? About a 45m lecture?
  • 124. Doze In Android Nougat, The terms for entering Doze were loosened. This means that even while the device is in the user’s pocket it can go into Doze. Still same principle apply: Don’t do anything about it.
  • 128.
  • 129. Missing from this lecture
  • 130. Missing from this lecture https://youtu.be/dZL2LDAg5cs
  • 132. Idan Felix 10/08/2016 CPU & Battery Most eclectic we had so far 5
  • 133. Idan Felix 10/08/2016 Everything is Trade-off Be as smart as you can be. 5

Notas do Editor

  1. Sparse Arrays Autoboxing CPU Frequency Scaling
  2. Battery Drain WakeLocks