SlideShare une entreprise Scribd logo
1  sur  135
Idan Felix
@idanfelix
30/05/2016
https://goo.gl/xekmHy
GPU Performance
2
First,
Idan Felix
3
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
Logistics
https://www.facebook.com/groups/android.academy.ils/
What’s next?
13/6 - Britt
- View, Animations
4/7 - Yonatan
- Networking, JSON, Batching, Location
10/8 - Felix
- Battery & CPU
14/9 - Britt
- Threading
30 / 10 / 2016
New course coming
Register to Meetup,
Join our facebook
Learn Android
Be Awesome
#‫רקורסיה‬
What a hell did you do @ San Francisco?
Google IO 2016
+
Firebase 2.0
Analytics
Cloud Messaging
Analytics
Analytics
Analytics
Seamless Update
No need to do anything.
Prompt only when download & Install is ready.
Just reboot device.
Multi Window Mode and Picture in Picture
Multi Window Mode and Picture in Picture
Notifications
Quick Settings
Doze On the Go
Project Svelte: Background Optimizations
DEPRECATED:
CONNECTIVITY_ACTION
ACTION_NEW_PICTURE
ACTION_NEW_VIDEO
Direct boot
Till user open the device
Only apps that was configured allow to run
Different storage
Java 8
android {
...
defaultConfig {
...
jackOptions {
enabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
Fragments
BugFixes
commitNow()
Android Studio 2.2
Constrain Layout
APK Analyzer
Android Wear 2.0
Android Wear 2.0
Instant Apps
“With Instant Apps, tapping a link can take you with Deep
Links into an Android app in just a few seconds without
having to install the app,”
Michael Siliski
#PerfMatters
Layers of our talk(s)
- Understanding The Theory
- What Can Go Wrong?
- Weapons for the hunt
Warning
Sorry,
(Almost) No code today
But a lot of links!
Theory
A
“Understanding is the first step to acceptance,
And only with acceptance can there be recovery.”
J. K. Rowling, Harry Potter and the Goblet of Fire
- 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
- Figure Out Where You Need to Be
- Determine Where You Are Now
- Decide Whether You Can Achieve
Your Objectives
- Develop a Plan for Achieving Your
Objectives, and Execute
- Conduct an Economic Analysis
http://www.perfeng.com/papers/step5.pdf
A Word on Premature Optimizations
Meet Prof. Donald Kunth
Professor at Stanford, Wrote “The art of computer programming”
Few quotes:
“Beware of bugs in the above code;
I have only proved it correct, not
tried it.”
https://en.wikipedia.org/wiki/Donald_Knuth
Meet Prof. Donald Kunth
https://en.wikipedia.org/wiki/Donald_Knuth
Professor at Stanford, Wrote “The art of computer programming”
Few quotes:
The psychological profiling [of a
programmer] is mostly the ability to
shift levels of abstraction,
from low level to high level.
Meet Prof. Donald Kunth
https://en.wikipedia.org/wiki/Donald_Knuth
Professor at Stanford, Wrote “The art of computer programming”
Few quotes:
Programmers waste enormous amounts of time thinking about, or
worrying about, the speed of noncritical parts of their programs, and
these attempts at efficiency actually have a strong negative impact
when debugging and maintenance are considered.
We should forget about small efficiencies, say about 97% of the time:
premature optimization is the root of all evil. Yet we should
not pass up our opportunities in that critical 3%.
However...
Knowing these kind of things
- Helps you avoid mistakes and bugs
- Makes you a better developer
- Teaches you the internal workings of the system
- Cost-Effectiveness-wise it’s just being smart:
“...Yet we should not pass up our opportunities in that critical 3%”Colt McAnlis: https://medium.com/google-developers/the-truth-about-preventative-optimizations-ccebadfd3eb5
Target Audience
This lecture is for you if…:
- You have a custom view in your app
- You have a lot of images in your app
- You have an app, or developing an app,
and want to do a better job
- You want to become a better developer
Memory
Battery
Radio
GPU
APK
Size
CPU
Layout
The Magic
<!-- Layout for weather forecast list item for today -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:background="@drawable/today_touch_selector">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="7"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="60dp"
android:orientation="vertical">
<TextView
android:id="@+id/list_item_date_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceLarge"
android:fontFamily="sans-serif-condensed"
android:textColor="@color/white" />
SRC: https://github.com/udacity/Sunshine-Version-2/blob/sunshine_master/app/src/main/res/layout/list_item_forecast_today.xml
Magic
The Magic
Measure Layout
Inflate
Draw
Step 0: Inflate the View Tree
- During onCreate() we call setContentView()
- The XML is parsed
and a tree of objects is created
- The tree is traversed during next
steps
Now we have
a Data Structure to use
Things get interesting!
Step 1: Measure
- Starts with the root
- Recursively ask the views (+Children) to measure themselves.
- This is done by calling onMeasure(int, int)
- It’s a negotiation, so onMeasure may be called multiple times.
- When it’s done, All the views in the tree know their size.REF: http://developer.android.com/reference/android/view/View.html#onMeasure(int, int)
Step 2: Layout
- Starts with the root
- Recursively position each child
- Done by onLayout(boolean, int, int, int, int)
- Stores the position, and set position for all children
- When it’s done, All the views in the tree knows their positionREF: http://developer.android.com/reference/android/view/View.html#onLayout(boolean, int, int, int, int)
- Now that a view knows its position,
The onDraw(Canvas) method is called
- That’s where a view draws itself
before asking its children to draw.
- The Canvas object generates (or Updates) a list of OpenGL-ES
commands (draw-list) to send to the GPU.
Step 3: Draw (AKA Update)
REF: http://developer.android.com/reference/android/view/View.html#onDraw(android.graphics.Canvas)
Guide: http://developer.android.com/training/custom-views/custom-drawing.html
Canvas Methods (and responsibility)
drawARGB
drawArc
drawBitmap
drawColor
drawLine
drawPicture
drawText
clipPath
clipRect
quickReject
rotate
scale
skew
translate
Ref:
https://developer.android.com/reference/android/graphics/Canva
The Magic
Measure Layout
Inflate
Draw
Plus ça change, plus c'est la même chose
When things change (text, color, size, padding, margin, etc.),
A view notifies the system, by calling
Invalidate - which will call the onDraw again, or
requestLayout - which will call the entire process again.
Image Credit:
It doesn’t end there
Step 4: Execute
The GPU executes the command list,
That was generated in onDraw()
And was cached
But what if this takes too long?
Solution: Use a double buffer
http://openglbook.com/chapter-1-getting-started.html
Solution: Use a double buffer
http://openglbook.com/chapter-1-getting-started.html
Step 5: VSync
Old CRT screens had to be “synced”,
so that the monitor starts a frame
at the correct time.
Similarly, Android holds the copy from a back buffer
if it is currently drawing to the screen.
https://en.wikipedia.org/wiki/Analog_television#Vertical_synchronization
Britt’s Lecture (13/6)
The Magic
Animate Measure Layout
TodayInflate
Draw
Execute Sync
Let’s talk about timing
But How Does It Work?
Smooth
Motion
60
No
Difference
60+
Flip Book
12
Movies
Frames Per Second
Fluid Motion
24
+effects
We Have A Winner!
Smooth
Motion
60
No
Difference
60+
Flip Book
12
Movies
Frames Per Second
Fluid Motion
24
+effects
Smooth
Motion
60
Colt McAnlis: https://youtu.be/CaMTIgxCSqU
Things to remember
- Britt will give a similar explanation on her lecture on 13/6
- Know thee process,
Appreciate, and
Know that the entire process should take less than 16ms
- Things on the GPU:
- Should get there as soon as possible (early)
Any Questions?
Part 2:
What can possibly go wrong
Few things in this process can go wrong
- Allocations in onDraw()
- A reminder from last week
- Avoiding Redundant Work
- Overdraw
- ClipRect
- QuickReject
- Invalidations
Allocating objects (new XXX()) might cause a GC (blocking)
So you might drop a frame.
Allocations in onDraw()
But it goes much deeper. See Ian Ni-Lewis: https://youtu.be/HAK5acHQ53E
Solution and Avoidance
Solution: DON’T ALLOCATE OBJECTS IN onDraw() METHOD.
Avoidance: USE LINT (built into the Studio)
https://developer.android.com/studio/write/lint.html || Colt McAnlis: https://youtu.be/Z_huaXCsYyw
STUDIO
But wait -
How can I know if frames gets dropped?
The all-mighty LogCat
I/Choreographer(1378): Skipped 55 frames! The application may be doing too much work
on its main thread.
But there’s a much cooler tool
GPU Profiling
GPU Profiling
Displays a graph for each visible app, showing
how much time each frame took:
The taller the bar, the longer it took
to render
The green line marks the 16 millisecond target.
Every time a frame crosses it,
your app is missing a frame.
https://developer.android.com/studio/profile/dev-options-rendering.html
16ms line
Dropped
Frames :(
14
https://developer.android.com/studio/profile/dev-options-rendering.html || Colt McAnlis https://youtu.be/VzYkVL1n4M8
OnDraw work
Copy the list
Execute
glSwapBuffers
Any Questions?
16ms line
Dropped
Frames :(
14
accented
if >16ms
Shiney
colors!
https://youtu.be/erGJw8W
DV74
This is where you tell them the biggest lie of the GPU profiler,
Before moving to the next part
BTW - Now for reals,
Any questions?
B
The number of files that a given pixel is
drawn in a frame.
Overdraw
Colt McAnlis: https://youtu.be/T52v50r-JfE
What is Overdraw
When a pixel is drawn multiple times -
That’s overdraw.
it might waste time and energy.
When the GPU executes the display list,
It can count how many times each pixel is drawn.
Not like this
Detecting Overdraw
Detecting Overdraw
https://developer.android.com/studio/profile/dev-options-
There’s even a Code-Lab online:
https://io2015codelabs.appspot.com/codelabs/android-performance-debug-gpu-overdraw
Roman Nurik: http://www.curious-creature.com/docs/android-
Fixing Overdraw
There are 2 common reasons for overdraw:
- Redundant backgrounds / Redundant transparency
- Wasteful onDraw
- Things that aren’t visible at all gets drawn (not using quickReject)
- Things that will be overdrawn gets drawn (not using clipRect)
Colt McAnlis: https://youtu.be/vkTn3Ule4Ps
QuickReject
A method to tell if something can be not drawn at all.
Call quickReject to see if you can skip drawing of things that will be
off screen.
REF: https://developer.android.com/reference/android/graphics/Canvas.html
ClipRect
ClipRect is a way to avoid OverDraw,
By keeping your GPU from drawing pixels that you know that will be
obscured by other stuff,
you refrain from overdraw.
Step 1:
quickReject
Step 2:
clipRect
ClipRect vs. QuickReject
Method return type: Detects... Helps to...
QuickReject boolean Fully invisible stuff Avoid redundant
calls to drawXXX(),
Keeping the
drawlist short.
ClipRect void Fully and Partially
invisible stuff
Avoid drawing
pixels,
but still executing
the draw-list!
boolean
But is usually used
as void
Any Questions?
Invalidations
C
Image Credit:
Plus ça change, plus c'est la même chose
When things change (text, color, size, padding, margin, etc.),
A view notifies the system, by calling
Invalidate - which will call the onDraw again, or
requestLayout - which will call the entire process again.
Simply
Avoid calling
invalidate
unless you have to,
And watch this: https://youtu.be/we6poP0kw6E
The horrible things that we do with
bitmaps are incredible,
but there are ways to fix it!
Bitmap Abuse
D
Bitmaps flow
Nothing here is accurate, but as an overview, it’s OK.
Image stored in the
APK, or in media
pack
Image downloaded
from internet, kept in
device storage
Image loaded into
memory (to the Heap)
Image loaded into GPU
Content is drawn on
screen
Bitmaps flow
Nothing here is accurate, but as an overview, it’s OK.
Image stored in the
APK, or in media
pack
Image downloaded
from internet, kept in
device storage
Image loaded into
memory (to the Heap)
Image loaded into GPU
Content is drawn on
screen
Uses PNG or JPEG
formats, files are
stored compressed
Optimized by AAPT
Uses one of 4 formats:
ALPHA_8 1 byte per pixel, only
alpha
ARGB_4444 deprecated
ARGB_8888 4 bytes per pixel,
+alpha
RGB_565 2 bytes per pixel, no
alpha
Bitmaps flow - Downloading
Image stored in the
APK, or in media
pack
Image loaded into
memory (to the Heap)
Image loaded into GPU
Content is drawn on
screen
Image downloaded
from internet, kept in
device storage
Which image format is downloaded?
Which Quality?
Which Size?
Which network? WIFI? Metered?
For how long are these images kept?
Any size limits?
Image downloaded
from internet, kept in
device storage
Bitmaps flow - In Package
Image loaded into
memory (to the Heap)
Image loaded into GPU
Content is drawn on
screen
Which image format is stored?
Which Quality?
Which Size(s)?
Which network? WIFI? Metered? → APK SIZE
For how long are these images kept? → Forever
Any size limits? → 100mb, but users will hate you!
Image stored in the
APK, or in media
pack
Image stored in the
APK, or in media
pack
Image downloaded
from internet, kept in
device storage
Bitmaps flow - In Memory
Image loaded into GPU
Content is drawn on
screen
What Pixel-Format is used?
Does the image need to re-scale?
How to scale the image efficiently?
Does the image has enough room?
Image loaded into
memory (to the Heap)
Image stored in the
APK, or in media
pack
Image downloaded
from internet, kept in
device storage
Bitmaps flow - In GPU
Content is drawn on
screen
Image loaded into
memory (to the Heap)
How much time does it take to copy the image?
Do you causing rendering to another buffer?
Image loaded into GPU
Why is it important?
java.lang.OutofMemoryError: bitmap size exceeds VM budget.
Nexus 5x takes pictures at 3840x2160 resolution, at 24-bit.
That’s 33,177,600 bytes of data.
Well, good-luck!
https://developer.android.com/training/displaying-bitmaps/index.html
Lucky us,
We can optimize almost everything,
And there’s A LOT of information out there
Bitmaps flow - Optimize download
Image stored in the
APK, or in media
pack
Image loaded into
memory (to the Heap)
Image loaded into GPU
Content is drawn on
screen
- Work with your server folks to provide you with
smaller files and better formats
- Use Cache (LRU Cache is great!)
- Use an image handling library, like Glide or Picasso
Image downloaded
from internet, kept in
device storage
Image downloaded
from internet, kept in
device storage
Bitmaps flow - Optimize APK size
Image loaded into
memory (to the Heap)
Image loaded into GPU
Content is drawn on
screen
- Choose the right format
- Choose a good-enough quality
- Add PNG/JPEG compression tools to your build
process
- Remove unused resources
Image stored in the
APK, or in media
pack
Image stored in the
APK, or in media
pack
Image downloaded
from internet, kept in
device storage
Bitmaps flow - Optimize loading & memory
Image loaded into GPU
Content is drawn on
screen
- Choose the right pixel format
- Resize
- Load on non-UI thread
- Reuse memory
- Use object pools if needed
Image loaded into
memory (to the Heap)
Most Important Tip:
Use a good image handling library
and let’s check out *some* of the other things that we can do
Trick #1: Load images efficiently
Step 1:
Don’t load the image at all,
Only decode its size.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;
https://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Trick #1: Load images efficiently
Step 2:
Don’t load the entire image,
Sub-sample
https://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Trick #2: Cache and off-load
Use LRU-Cache to load bitmaps into memory and re-use them.
This will help with list-views and similar, and help to limit the amount
of memory used.
Move things off the UI thread. Use AsyncTask, but with caution, and
handle concurrency, recycling, and lifecycle.
https://developer.android.com/training/displaying-bitmaps/process-bitmap.html
https://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
Trick #3: Re-use bitmaps, in pools
- Supported from 3.0 (API 11)
- if possible, use the same memory.
- Works only on mutable bitmaps (BitmapFactory.Options)
- Works only if image size is smaller than the buffer (since KitKat,
4.4)
Works only if image sizes are exactly the same (before)
- Not easy work
Colt McAnlis: https://youtu.be/_ioFW3cyRV0 , and https://developer.android.com/training/displaying-bitmaps/manage-memory.html
Trick #4: Be smarter than AAPT with PNGs
AAPT optimizes PNG files - but uses only these 3 optimizations:
- Is the image Grayscale
- Is the image Transparent
- Is the image cheap to index
All loss-less optimizations.
Apply a lossy optimization tool.
Trick #4: Be smarter than AAPT with PNGs
You will probably want some more info.
PNG:
How it works: https://medium.com/@duhroach/how-png-works-
f1174e3cc7b7
Reducing size: https://medium.com/@duhroach/reducing-png-file-size-
8473480d0476
AAPT: https://medium.com/@duhroach/smaller-pngs-and-android-s-aapt-tool-
4ce38a24019d
JPG:
How it works: https://medium.freecodecamp.com/how-jpg-works-
a4dbd2316f35
Reducing size: https://medium.com/@duhroach/reducing-jpg-file-size-
From everything we learned,
What you should remember
Summary
Σ
Theory
1.Systematic Performance Improvement
2.Across-the-board impact
3.Preventative / Premature optimizations
4.The Getting-XML-to-Screen scheme
Animate Measure Layout
Inflate
Draw
Execute Sync
Profiling tools
Bitmap Abuse
Use a good image handling library
Know the theory
Pick the right formats
Try to reduce image size
Try to reuse bitmaps if possible
Any Questions?
Thank you,
Drive home safely
Thank you,
Drive home safely

Contenu connexe

Similaire à Performence #2 gpu

Scaling Up Machine Learning: How to Benchmark GraphLab Create on Huge Datasets
Scaling Up Machine Learning: How to Benchmark GraphLab Create on Huge DatasetsScaling Up Machine Learning: How to Benchmark GraphLab Create on Huge Datasets
Scaling Up Machine Learning: How to Benchmark GraphLab Create on Huge Datasets
Turi, Inc.
 
Building an Open Source iOS app: lessons learned
Building an Open Source iOS app: lessons learnedBuilding an Open Source iOS app: lessons learned
Building an Open Source iOS app: lessons learned
Wojciech Koszek
 
01Introduction.pptx - C280, Computer Vision
01Introduction.pptx - C280, Computer Vision01Introduction.pptx - C280, Computer Vision
01Introduction.pptx - C280, Computer Vision
butest
 

Similaire à Performence #2 gpu (20)

Lecture #3 activities and intents
Lecture #3  activities and intentsLecture #3  activities and intents
Lecture #3 activities and intents
 
Advanced #2 - ui perf
 Advanced #2 - ui perf Advanced #2 - ui perf
Advanced #2 - ui perf
 
Scaling Up Machine Learning: How to Benchmark GraphLab Create on Huge Datasets
Scaling Up Machine Learning: How to Benchmark GraphLab Create on Huge DatasetsScaling Up Machine Learning: How to Benchmark GraphLab Create on Huge Datasets
Scaling Up Machine Learning: How to Benchmark GraphLab Create on Huge Datasets
 
Pain Driven Development by Alexandr Sugak
Pain Driven Development by Alexandr SugakPain Driven Development by Alexandr Sugak
Pain Driven Development by Alexandr Sugak
 
Android design lecture #1
Android design   lecture #1Android design   lecture #1
Android design lecture #1
 
Make Tools
Make ToolsMake Tools
Make Tools
 
iPhone App from concept to product
iPhone App from concept to productiPhone App from concept to product
iPhone App from concept to product
 
Internet of Things Expo Concept from Umich Grad Program
Internet of Things Expo Concept from Umich Grad ProgramInternet of Things Expo Concept from Umich Grad Program
Internet of Things Expo Concept from Umich Grad Program
 
Is Python still production ready ? Ludovic Gasc
Is Python still production ready ? Ludovic GascIs Python still production ready ? Ludovic Gasc
Is Python still production ready ? Ludovic Gasc
 
My summary for cs001x computer science for beginners
My summary for cs001x computer science for beginnersMy summary for cs001x computer science for beginners
My summary for cs001x computer science for beginners
 
Performance #5 cpu and battery
Performance #5  cpu and batteryPerformance #5  cpu and battery
Performance #5 cpu and battery
 
DevOps Practice in Nonprofit - Abdurrachman Mappuji
DevOps Practice in Nonprofit - Abdurrachman MappujiDevOps Practice in Nonprofit - Abdurrachman Mappuji
DevOps Practice in Nonprofit - Abdurrachman Mappuji
 
Mobile world
Mobile worldMobile world
Mobile world
 
8 Usability Lessons from the UPA Conference by Mark Alves
8 Usability Lessons from the UPA Conference by Mark Alves8 Usability Lessons from the UPA Conference by Mark Alves
8 Usability Lessons from the UPA Conference by Mark Alves
 
Operationalizing Clojure Confidently
Operationalizing Clojure ConfidentlyOperationalizing Clojure Confidently
Operationalizing Clojure Confidently
 
Common Missteps in Cross-Platform Development.pdf
Common Missteps in Cross-Platform Development.pdfCommon Missteps in Cross-Platform Development.pdf
Common Missteps in Cross-Platform Development.pdf
 
Building an Open Source iOS app: lessons learned
Building an Open Source iOS app: lessons learnedBuilding an Open Source iOS app: lessons learned
Building an Open Source iOS app: lessons learned
 
Software Engineering For Startups
Software Engineering For StartupsSoftware Engineering For Startups
Software Engineering For Startups
 
01Introduction.pptx - C280, Computer Vision
01Introduction.pptx - C280, Computer Vision01Introduction.pptx - C280, Computer Vision
01Introduction.pptx - C280, Computer Vision
 
GDSC IIITM - Discover Your Domain
GDSC IIITM  - Discover Your DomainGDSC IIITM  - Discover Your Domain
GDSC IIITM - Discover Your Domain
 

Plus de Vitali Pekelis

Plus 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
 
Android meetup
Android meetupAndroid meetup
Android meetup
 
Android design lecture #3
Android design   lecture #3Android design   lecture #3
Android design lecture #3
 
From newbie to ...
From newbie to ...From newbie to ...
From newbie to ...
 

Dernier

CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
anilsa9823
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Dernier (20)

Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 

Performence #2 gpu

  • 3. Idan Felix 3 I’m 33 years old Varonis Android Academy TLV
  • 4. 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
  • 7. What’s next? 13/6 - Britt - View, Animations 4/7 - Yonatan - Networking, JSON, Batching, Location 10/8 - Felix - Battery & CPU 14/9 - Britt - Threading
  • 8. 30 / 10 / 2016 New course coming
  • 9. Register to Meetup, Join our facebook Learn Android Be Awesome #‫רקורסיה‬
  • 10.
  • 11. What a hell did you do @ San Francisco? Google IO 2016 +
  • 12.
  • 13.
  • 14.
  • 15.
  • 17.
  • 23.
  • 24. Seamless Update No need to do anything. Prompt only when download & Install is ready. Just reboot device.
  • 25. Multi Window Mode and Picture in Picture
  • 26. Multi Window Mode and Picture in Picture
  • 30. Project Svelte: Background Optimizations DEPRECATED: CONNECTIVITY_ACTION ACTION_NEW_PICTURE ACTION_NEW_VIDEO
  • 31. Direct boot Till user open the device Only apps that was configured allow to run Different storage
  • 32. Java 8 android { ... defaultConfig { ... jackOptions { enabled true } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } }
  • 40. “With Instant Apps, tapping a link can take you with Deep Links into an Android app in just a few seconds without having to install the app,” Michael Siliski
  • 42. Layers of our talk(s) - Understanding The Theory - What Can Go Wrong? - Weapons for the hunt
  • 45. But a lot of links!
  • 46. Theory A “Understanding is the first step to acceptance, And only with acceptance can there be recovery.” J. K. Rowling, Harry Potter and the Goblet of Fire
  • 47. - 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 - Figure Out Where You Need to Be - Determine Where You Are Now - Decide Whether You Can Achieve Your Objectives - Develop a Plan for Achieving Your Objectives, and Execute - Conduct an Economic Analysis http://www.perfeng.com/papers/step5.pdf
  • 48. A Word on Premature Optimizations
  • 49. Meet Prof. Donald Kunth Professor at Stanford, Wrote “The art of computer programming” Few quotes: “Beware of bugs in the above code; I have only proved it correct, not tried it.” https://en.wikipedia.org/wiki/Donald_Knuth
  • 50. Meet Prof. Donald Kunth https://en.wikipedia.org/wiki/Donald_Knuth Professor at Stanford, Wrote “The art of computer programming” Few quotes: The psychological profiling [of a programmer] is mostly the ability to shift levels of abstraction, from low level to high level.
  • 51. Meet Prof. Donald Kunth https://en.wikipedia.org/wiki/Donald_Knuth Professor at Stanford, Wrote “The art of computer programming” Few quotes: Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.
  • 52. However... Knowing these kind of things - Helps you avoid mistakes and bugs - Makes you a better developer - Teaches you the internal workings of the system - Cost-Effectiveness-wise it’s just being smart: “...Yet we should not pass up our opportunities in that critical 3%”Colt McAnlis: https://medium.com/google-developers/the-truth-about-preventative-optimizations-ccebadfd3eb5
  • 53. Target Audience This lecture is for you if…: - You have a custom view in your app - You have a lot of images in your app - You have an app, or developing an app, and want to do a better job - You want to become a better developer
  • 55. The Magic <!-- Layout for weather forecast list item for today --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:minHeight="?android:attr/listPreferredItemHeight" android:orientation="horizontal" android:background="@drawable/today_touch_selector"> <LinearLayout android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="7" android:layout_marginTop="16dp" android:layout_marginBottom="16dp" android:layout_marginLeft="60dp" android:orientation="vertical"> <TextView android:id="@+id/list_item_date_textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:textAppearanceLarge" android:fontFamily="sans-serif-condensed" android:textColor="@color/white" /> SRC: https://github.com/udacity/Sunshine-Version-2/blob/sunshine_master/app/src/main/res/layout/list_item_forecast_today.xml Magic
  • 57. Step 0: Inflate the View Tree - During onCreate() we call setContentView() - The XML is parsed and a tree of objects is created - The tree is traversed during next steps
  • 58. Now we have a Data Structure to use Things get interesting!
  • 59. Step 1: Measure - Starts with the root - Recursively ask the views (+Children) to measure themselves. - This is done by calling onMeasure(int, int) - It’s a negotiation, so onMeasure may be called multiple times. - When it’s done, All the views in the tree know their size.REF: http://developer.android.com/reference/android/view/View.html#onMeasure(int, int)
  • 60. Step 2: Layout - Starts with the root - Recursively position each child - Done by onLayout(boolean, int, int, int, int) - Stores the position, and set position for all children - When it’s done, All the views in the tree knows their positionREF: http://developer.android.com/reference/android/view/View.html#onLayout(boolean, int, int, int, int)
  • 61. - Now that a view knows its position, The onDraw(Canvas) method is called - That’s where a view draws itself before asking its children to draw. - The Canvas object generates (or Updates) a list of OpenGL-ES commands (draw-list) to send to the GPU. Step 3: Draw (AKA Update) REF: http://developer.android.com/reference/android/view/View.html#onDraw(android.graphics.Canvas) Guide: http://developer.android.com/training/custom-views/custom-drawing.html
  • 62. Canvas Methods (and responsibility) drawARGB drawArc drawBitmap drawColor drawLine drawPicture drawText clipPath clipRect quickReject rotate scale skew translate Ref: https://developer.android.com/reference/android/graphics/Canva
  • 64. Plus ça change, plus c'est la même chose When things change (text, color, size, padding, margin, etc.), A view notifies the system, by calling Invalidate - which will call the onDraw again, or requestLayout - which will call the entire process again.
  • 67. Step 4: Execute The GPU executes the command list, That was generated in onDraw() And was cached But what if this takes too long?
  • 68.
  • 69. Solution: Use a double buffer http://openglbook.com/chapter-1-getting-started.html
  • 70. Solution: Use a double buffer http://openglbook.com/chapter-1-getting-started.html
  • 71. Step 5: VSync Old CRT screens had to be “synced”, so that the monitor starts a frame at the correct time. Similarly, Android holds the copy from a back buffer if it is currently drawing to the screen. https://en.wikipedia.org/wiki/Analog_television#Vertical_synchronization
  • 72. Britt’s Lecture (13/6) The Magic Animate Measure Layout TodayInflate Draw Execute Sync
  • 74. But How Does It Work? Smooth Motion 60 No Difference 60+ Flip Book 12 Movies Frames Per Second Fluid Motion 24 +effects
  • 75. We Have A Winner! Smooth Motion 60 No Difference 60+ Flip Book 12 Movies Frames Per Second Fluid Motion 24 +effects Smooth Motion 60 Colt McAnlis: https://youtu.be/CaMTIgxCSqU
  • 76. Things to remember - Britt will give a similar explanation on her lecture on 13/6 - Know thee process, Appreciate, and Know that the entire process should take less than 16ms - Things on the GPU: - Should get there as soon as possible (early)
  • 78. Part 2: What can possibly go wrong
  • 79. Few things in this process can go wrong - Allocations in onDraw() - A reminder from last week - Avoiding Redundant Work - Overdraw - ClipRect - QuickReject - Invalidations
  • 80. Allocating objects (new XXX()) might cause a GC (blocking) So you might drop a frame. Allocations in onDraw() But it goes much deeper. See Ian Ni-Lewis: https://youtu.be/HAK5acHQ53E
  • 81. Solution and Avoidance Solution: DON’T ALLOCATE OBJECTS IN onDraw() METHOD. Avoidance: USE LINT (built into the Studio) https://developer.android.com/studio/write/lint.html || Colt McAnlis: https://youtu.be/Z_huaXCsYyw STUDIO
  • 82. But wait - How can I know if frames gets dropped?
  • 83. The all-mighty LogCat I/Choreographer(1378): Skipped 55 frames! The application may be doing too much work on its main thread.
  • 84. But there’s a much cooler tool
  • 85.
  • 87. GPU Profiling Displays a graph for each visible app, showing how much time each frame took: The taller the bar, the longer it took to render The green line marks the 16 millisecond target. Every time a frame crosses it, your app is missing a frame. https://developer.android.com/studio/profile/dev-options-rendering.html
  • 89. https://developer.android.com/studio/profile/dev-options-rendering.html || Colt McAnlis https://youtu.be/VzYkVL1n4M8 OnDraw work Copy the list Execute glSwapBuffers
  • 93. This is where you tell them the biggest lie of the GPU profiler, Before moving to the next part BTW - Now for reals, Any questions?
  • 94. B The number of files that a given pixel is drawn in a frame. Overdraw Colt McAnlis: https://youtu.be/T52v50r-JfE
  • 95. What is Overdraw When a pixel is drawn multiple times - That’s overdraw. it might waste time and energy. When the GPU executes the display list, It can count how many times each pixel is drawn. Not like this
  • 98. There’s even a Code-Lab online: https://io2015codelabs.appspot.com/codelabs/android-performance-debug-gpu-overdraw
  • 100. Fixing Overdraw There are 2 common reasons for overdraw: - Redundant backgrounds / Redundant transparency - Wasteful onDraw - Things that aren’t visible at all gets drawn (not using quickReject) - Things that will be overdrawn gets drawn (not using clipRect) Colt McAnlis: https://youtu.be/vkTn3Ule4Ps
  • 101. QuickReject A method to tell if something can be not drawn at all. Call quickReject to see if you can skip drawing of things that will be off screen. REF: https://developer.android.com/reference/android/graphics/Canvas.html
  • 102. ClipRect ClipRect is a way to avoid OverDraw, By keeping your GPU from drawing pixels that you know that will be obscured by other stuff, you refrain from overdraw.
  • 104. ClipRect vs. QuickReject Method return type: Detects... Helps to... QuickReject boolean Fully invisible stuff Avoid redundant calls to drawXXX(), Keeping the drawlist short. ClipRect void Fully and Partially invisible stuff Avoid drawing pixels, but still executing the draw-list! boolean But is usually used as void
  • 108. Plus ça change, plus c'est la même chose When things change (text, color, size, padding, margin, etc.), A view notifies the system, by calling Invalidate - which will call the onDraw again, or requestLayout - which will call the entire process again.
  • 109. Simply Avoid calling invalidate unless you have to, And watch this: https://youtu.be/we6poP0kw6E
  • 110. The horrible things that we do with bitmaps are incredible, but there are ways to fix it! Bitmap Abuse D
  • 111. Bitmaps flow Nothing here is accurate, but as an overview, it’s OK. Image stored in the APK, or in media pack Image downloaded from internet, kept in device storage Image loaded into memory (to the Heap) Image loaded into GPU Content is drawn on screen
  • 112. Bitmaps flow Nothing here is accurate, but as an overview, it’s OK. Image stored in the APK, or in media pack Image downloaded from internet, kept in device storage Image loaded into memory (to the Heap) Image loaded into GPU Content is drawn on screen Uses PNG or JPEG formats, files are stored compressed Optimized by AAPT Uses one of 4 formats: ALPHA_8 1 byte per pixel, only alpha ARGB_4444 deprecated ARGB_8888 4 bytes per pixel, +alpha RGB_565 2 bytes per pixel, no alpha
  • 113. Bitmaps flow - Downloading Image stored in the APK, or in media pack Image loaded into memory (to the Heap) Image loaded into GPU Content is drawn on screen Image downloaded from internet, kept in device storage Which image format is downloaded? Which Quality? Which Size? Which network? WIFI? Metered? For how long are these images kept? Any size limits?
  • 114. Image downloaded from internet, kept in device storage Bitmaps flow - In Package Image loaded into memory (to the Heap) Image loaded into GPU Content is drawn on screen Which image format is stored? Which Quality? Which Size(s)? Which network? WIFI? Metered? → APK SIZE For how long are these images kept? → Forever Any size limits? → 100mb, but users will hate you! Image stored in the APK, or in media pack
  • 115. Image stored in the APK, or in media pack Image downloaded from internet, kept in device storage Bitmaps flow - In Memory Image loaded into GPU Content is drawn on screen What Pixel-Format is used? Does the image need to re-scale? How to scale the image efficiently? Does the image has enough room? Image loaded into memory (to the Heap)
  • 116. Image stored in the APK, or in media pack Image downloaded from internet, kept in device storage Bitmaps flow - In GPU Content is drawn on screen Image loaded into memory (to the Heap) How much time does it take to copy the image? Do you causing rendering to another buffer? Image loaded into GPU
  • 117. Why is it important? java.lang.OutofMemoryError: bitmap size exceeds VM budget. Nexus 5x takes pictures at 3840x2160 resolution, at 24-bit. That’s 33,177,600 bytes of data. Well, good-luck! https://developer.android.com/training/displaying-bitmaps/index.html
  • 118. Lucky us, We can optimize almost everything, And there’s A LOT of information out there
  • 119. Bitmaps flow - Optimize download Image stored in the APK, or in media pack Image loaded into memory (to the Heap) Image loaded into GPU Content is drawn on screen - Work with your server folks to provide you with smaller files and better formats - Use Cache (LRU Cache is great!) - Use an image handling library, like Glide or Picasso Image downloaded from internet, kept in device storage
  • 120. Image downloaded from internet, kept in device storage Bitmaps flow - Optimize APK size Image loaded into memory (to the Heap) Image loaded into GPU Content is drawn on screen - Choose the right format - Choose a good-enough quality - Add PNG/JPEG compression tools to your build process - Remove unused resources Image stored in the APK, or in media pack
  • 121. Image stored in the APK, or in media pack Image downloaded from internet, kept in device storage Bitmaps flow - Optimize loading & memory Image loaded into GPU Content is drawn on screen - Choose the right pixel format - Resize - Load on non-UI thread - Reuse memory - Use object pools if needed Image loaded into memory (to the Heap)
  • 122. Most Important Tip: Use a good image handling library and let’s check out *some* of the other things that we can do
  • 123. Trick #1: Load images efficiently Step 1: Don’t load the image at all, Only decode its size. BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), R.id.myimage, options); int imageHeight = options.outHeight; int imageWidth = options.outWidth; String imageType = options.outMimeType; https://developer.android.com/training/displaying-bitmaps/load-bitmap.html
  • 124. Trick #1: Load images efficiently Step 2: Don’t load the entire image, Sub-sample https://developer.android.com/training/displaying-bitmaps/load-bitmap.html
  • 125. Trick #2: Cache and off-load Use LRU-Cache to load bitmaps into memory and re-use them. This will help with list-views and similar, and help to limit the amount of memory used. Move things off the UI thread. Use AsyncTask, but with caution, and handle concurrency, recycling, and lifecycle. https://developer.android.com/training/displaying-bitmaps/process-bitmap.html https://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
  • 126. Trick #3: Re-use bitmaps, in pools - Supported from 3.0 (API 11) - if possible, use the same memory. - Works only on mutable bitmaps (BitmapFactory.Options) - Works only if image size is smaller than the buffer (since KitKat, 4.4) Works only if image sizes are exactly the same (before) - Not easy work Colt McAnlis: https://youtu.be/_ioFW3cyRV0 , and https://developer.android.com/training/displaying-bitmaps/manage-memory.html
  • 127. Trick #4: Be smarter than AAPT with PNGs AAPT optimizes PNG files - but uses only these 3 optimizations: - Is the image Grayscale - Is the image Transparent - Is the image cheap to index All loss-less optimizations. Apply a lossy optimization tool.
  • 128. Trick #4: Be smarter than AAPT with PNGs You will probably want some more info. PNG: How it works: https://medium.com/@duhroach/how-png-works- f1174e3cc7b7 Reducing size: https://medium.com/@duhroach/reducing-png-file-size- 8473480d0476 AAPT: https://medium.com/@duhroach/smaller-pngs-and-android-s-aapt-tool- 4ce38a24019d JPG: How it works: https://medium.freecodecamp.com/how-jpg-works- a4dbd2316f35 Reducing size: https://medium.com/@duhroach/reducing-jpg-file-size-
  • 129. From everything we learned, What you should remember Summary Σ
  • 130. Theory 1.Systematic Performance Improvement 2.Across-the-board impact 3.Preventative / Premature optimizations 4.The Getting-XML-to-Screen scheme Animate Measure Layout Inflate Draw Execute Sync
  • 132. Bitmap Abuse Use a good image handling library Know the theory Pick the right formats Try to reduce image size Try to reuse bitmaps if possible