Yonatan Levin
Android
Academy
How to create great apps
that also working well
OutOfMemory - Bitmap memory usage
Image 1080X1920pixels, 124KB .png compressed
Bitmap size = Width * Height * depth(4 bytes) = 8 MB!!!!
Reduce the size before loading it to the memory.
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;
OutOfMemory - Garbage collector
A large number of small allocations can also cause heap
fragmentation
List<Object> mTempObjects = new ArrayList<Object>();
for(int i=0; i<10000; i++){
mTempObjects.add(new Object());
}
OutOfMemory - Garbage collector
for(int i=0; i<mTempObjects.size(); i++){
Object c = mTempObjects.get(i);
Log.d(TAG, "Object data:" + c.getValue());
}
10,000 references waiting to be collected by GC
OutOfMemory - Garbage collector
“To improve is to change; to be perfect is to change often.” – Winston Churchill
Object c;
for(int i=0; i<mTempObjects.size(); i++){
c = mTempObjects.get(i);
Log.d(TAG, "Object data:" + c.getValue());
}
OutOfMemory - Garbage collector
Reuse of the same object
private StringBuilder buildSomething(StringBuilder sb){
sb.append(readString(fileStream));
sb.append(readOrderDetails(AnotheFileStream));
return sb;
}
No short term temporary object for String
Memory overhead
Object with just one int take 16 bytes at minimum
Class Integer {
private int value;
}
Object overhead (8 bytes ) + Overhead of dlmalloc(8 bytes) + data (n
bytes) = >16 bytes
Memory overhead
class HashMap$HashMapEntry<K,V> {
K key;
V value;
int hash;
HashMapEntry<K,V> next;
}
Total: 8 bytes (Object) + 8(dlmalloc) + 4*4(members) = 32 bytes
Primitive Type vs Object
Integer (16 bytes) vs int(4 bytes)
Boolean(16 bytes) vs boolean(4 bytes)
or
bit-field(1 bit) //even better!
Enums vs Ints
Very simple - don’t use Enums
public final static enum Things {
THING_1,
THING_2,
}; == + 1,112 bytes
Enums vs Ints
Use final static variable:
public static int THING_1 = 1;
public static int THING_2 = 2;
------------------------
128 bytes
Discover by yourself
import java.lang.instrument.Instrumentation;
public class ObjectSizeFetcher {
private static Instrumentation instrumentation;
public static void premain(String args, Instrumentation inst) { instrumentation = inst; }
public static long getObjectSize(Object o) { return instrumentation.getObjectSize(o); } }
Use getObjectSize:
public class C { private int x; private int y;
public static void main(String [] args) {
System.out.println(ObjectSizeFetcher.getObjectSize(new C())); } }
Anonymous and Inner Classes
~500 bytes of code
button.setOnClickListener(new Runnable() {
public void run(){
//do some stuff
}
});
unregister as soon as possible
Service
- Very usefull, but overused.
- Bind to Lifecycle
- Stop it when no need.
- Use IntentService instead (will stop when job is done)
- Release memory when your
activity no longer visible
- Don’t hold direct references.
Use WeakReference
- Stop all your handlers,
threads in onPause/onStop
- Configuration changes
Memory Leak
Proguard
Tool to shrink, optimize and obfuscate the
code.
Very usefull, hard to configure.
levinyon@gmail.com
https://plus.google.com/+YonatanLevin

More Related Content

PDF
Åsted .Net (CSI .Net)
PPTX
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
PPT
Scientific Computing with Python Webinar March 19: 3D Visualization with Mayavi
PPTX
Introduction to numpy
PPTX
PDF
Python tour
KEY
Numpy Talk at SIAM
PDF
Plotting data with python and pylab
Åsted .Net (CSI .Net)
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
Scientific Computing with Python Webinar March 19: 3D Visualization with Mayavi
Introduction to numpy
Python tour
Numpy Talk at SIAM
Plotting data with python and pylab

What's hot (20)

PPTX
Introduction to numpy Session 1
PPT
Python Training Tutorial for Frreshers
PPTX
Introduction to matplotlib
PDF
Introduction to NumPy (PyData SV 2013)
KEY
NumPy/SciPy Statistics
PPTX
PDF
Effective Numerical Computation in NumPy and SciPy
PPTX
Why learn Internals?
PDF
Biopython: Overview, State of the Art and Outlook
PDF
Python update in 2018 #ll2018jp
PDF
Intoduction to numpy
PDF
Class 8b: Numpy & Matplotlib
PDF
TensorFlow example for AI Ukraine2016
PDF
NumPy Refresher
PPTX
3. basic data structures(2)
PDF
Python NumPy Tutorial | NumPy Array | Edureka
PPTX
Python GC
PDF
Garbage collector in python
PDF
Scientific Computing with Python - NumPy | WeiYuan
PPTX
Coding convention
Introduction to numpy Session 1
Python Training Tutorial for Frreshers
Introduction to matplotlib
Introduction to NumPy (PyData SV 2013)
NumPy/SciPy Statistics
Effective Numerical Computation in NumPy and SciPy
Why learn Internals?
Biopython: Overview, State of the Art and Outlook
Python update in 2018 #ll2018jp
Intoduction to numpy
Class 8b: Numpy & Matplotlib
TensorFlow example for AI Ukraine2016
NumPy Refresher
3. basic data structures(2)
Python NumPy Tutorial | NumPy Array | Edureka
Python GC
Garbage collector in python
Scientific Computing with Python - NumPy | WeiYuan
Coding convention
Ad

Similar to Performance (20)

PPT
Lo Mejor Del Pdc2008 El Futrode C#
PDF
Cvpr2010 open source vision software, intro and training part-iii introduct...
PPT
First kinectslides
PPT
cs247 slides
PDF
This is a java lab assignment. I have added the first part java re.pdf
PDF
Lezione03
PDF
Lezione03
PPT
devLink - What's New in C# 4?
PPTX
#OOP_D_ITS - 2nd - C++ Getting Started
PDF
An Introduction to Programming in Java: Arrays
PDF
OrderTest.javapublic class OrderTest {       Get an arra.pdf
PDF
IoT Best practices
PPTX
IAT334-Lab02-ArraysPImage.pptx
PDF
Look Mommy, No GC! (TechDays NL 2017)
DOCX
JAVA - Design a data structure IntSet that can hold a set of integers-.docx
PDF
A Gentle Introduction to Coding ... with Python
PDF
Ds lab handouts
PPTX
Andromance - Android Performance
PDF
Building android apps with kotlin
DOCX
do it in eclips and make sure it compile Goals1)Be able to.docx
Lo Mejor Del Pdc2008 El Futrode C#
Cvpr2010 open source vision software, intro and training part-iii introduct...
First kinectslides
cs247 slides
This is a java lab assignment. I have added the first part java re.pdf
Lezione03
Lezione03
devLink - What's New in C# 4?
#OOP_D_ITS - 2nd - C++ Getting Started
An Introduction to Programming in Java: Arrays
OrderTest.javapublic class OrderTest {       Get an arra.pdf
IoT Best practices
IAT334-Lab02-ArraysPImage.pptx
Look Mommy, No GC! (TechDays NL 2017)
JAVA - Design a data structure IntSet that can hold a set of integers-.docx
A Gentle Introduction to Coding ... with Python
Ds lab handouts
Andromance - Android Performance
Building android apps with kotlin
do it in eclips and make sure it compile Goals1)Be able to.docx
Ad

More from Yonatan Levin (15)

PDF
Knock, knock, who is there? Doze.
PDF
Android Performance #4: Network
PDF
Performance #1: Memory
PDF
A friend in need - A JS indeed
PDF
Mobile UI: Fruit or Delicious sweets
PPTX
Ipc: aidl sexy, not a curse
PPTX
IPC: AIDL is sexy, not a curse
PPTX
How to create Great App
PPTX
Mobile world
PPTX
Data binding
PPTX
What's new in android M(6.0)
PPTX
IPC: AIDL is not a curse
PDF
Fragments, the love story
PPTX
Lecture #3: Android Academy Study Jam
PDF
Lecture #1 intro,setup, new project, sunshine
Knock, knock, who is there? Doze.
Android Performance #4: Network
Performance #1: Memory
A friend in need - A JS indeed
Mobile UI: Fruit or Delicious sweets
Ipc: aidl sexy, not a curse
IPC: AIDL is sexy, not a curse
How to create Great App
Mobile world
Data binding
What's new in android M(6.0)
IPC: AIDL is not a curse
Fragments, the love story
Lecture #3: Android Academy Study Jam
Lecture #1 intro,setup, new project, sunshine

Recently uploaded (20)

PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PPTX
Information Storage and Retrieval Techniques Unit III
PPTX
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
PPTX
Current and future trends in Computer Vision.pptx
PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PPTX
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
PPT
Total quality management ppt for engineering students
PDF
Abrasive, erosive and cavitation wear.pdf
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PPTX
introduction to high performance computing
PPTX
ASME PCC-02 TRAINING -DESKTOP-NLE5HNP.pptx
PDF
Visual Aids for Exploratory Data Analysis.pdf
PDF
Design Guidelines and solutions for Plastics parts
PDF
August -2025_Top10 Read_Articles_ijait.pdf
PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PPTX
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
PPTX
Software Engineering and software moduleing
PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Information Storage and Retrieval Techniques Unit III
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
Current and future trends in Computer Vision.pptx
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
Total quality management ppt for engineering students
Abrasive, erosive and cavitation wear.pdf
Categorization of Factors Affecting Classification Algorithms Selection
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
introduction to high performance computing
ASME PCC-02 TRAINING -DESKTOP-NLE5HNP.pptx
Visual Aids for Exploratory Data Analysis.pdf
Design Guidelines and solutions for Plastics parts
August -2025_Top10 Read_Articles_ijait.pdf
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
Software Engineering and software moduleing
distributed database system" (DDBS) is often used to refer to both the distri...
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF

Performance

  • 2. How to create great apps that also working well
  • 3. OutOfMemory - Bitmap memory usage Image 1080X1920pixels, 124KB .png compressed Bitmap size = Width * Height * depth(4 bytes) = 8 MB!!!! Reduce the size before loading it to the memory. 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;
  • 4. OutOfMemory - Garbage collector A large number of small allocations can also cause heap fragmentation List<Object> mTempObjects = new ArrayList<Object>(); for(int i=0; i<10000; i++){ mTempObjects.add(new Object()); }
  • 5. OutOfMemory - Garbage collector for(int i=0; i<mTempObjects.size(); i++){ Object c = mTempObjects.get(i); Log.d(TAG, "Object data:" + c.getValue()); } 10,000 references waiting to be collected by GC
  • 6. OutOfMemory - Garbage collector “To improve is to change; to be perfect is to change often.” – Winston Churchill Object c; for(int i=0; i<mTempObjects.size(); i++){ c = mTempObjects.get(i); Log.d(TAG, "Object data:" + c.getValue()); }
  • 7. OutOfMemory - Garbage collector Reuse of the same object private StringBuilder buildSomething(StringBuilder sb){ sb.append(readString(fileStream)); sb.append(readOrderDetails(AnotheFileStream)); return sb; } No short term temporary object for String
  • 8. Memory overhead Object with just one int take 16 bytes at minimum Class Integer { private int value; } Object overhead (8 bytes ) + Overhead of dlmalloc(8 bytes) + data (n bytes) = >16 bytes
  • 9. Memory overhead class HashMap$HashMapEntry<K,V> { K key; V value; int hash; HashMapEntry<K,V> next; } Total: 8 bytes (Object) + 8(dlmalloc) + 4*4(members) = 32 bytes
  • 10. Primitive Type vs Object Integer (16 bytes) vs int(4 bytes) Boolean(16 bytes) vs boolean(4 bytes) or bit-field(1 bit) //even better!
  • 11. Enums vs Ints Very simple - don’t use Enums public final static enum Things { THING_1, THING_2, }; == + 1,112 bytes
  • 12. Enums vs Ints Use final static variable: public static int THING_1 = 1; public static int THING_2 = 2; ------------------------ 128 bytes
  • 13. Discover by yourself import java.lang.instrument.Instrumentation; public class ObjectSizeFetcher { private static Instrumentation instrumentation; public static void premain(String args, Instrumentation inst) { instrumentation = inst; } public static long getObjectSize(Object o) { return instrumentation.getObjectSize(o); } } Use getObjectSize: public class C { private int x; private int y; public static void main(String [] args) { System.out.println(ObjectSizeFetcher.getObjectSize(new C())); } }
  • 14. Anonymous and Inner Classes ~500 bytes of code button.setOnClickListener(new Runnable() { public void run(){ //do some stuff } }); unregister as soon as possible
  • 15. Service - Very usefull, but overused. - Bind to Lifecycle - Stop it when no need. - Use IntentService instead (will stop when job is done)
  • 16. - Release memory when your activity no longer visible - Don’t hold direct references. Use WeakReference - Stop all your handlers, threads in onPause/onStop - Configuration changes Memory Leak
  • 17. Proguard Tool to shrink, optimize and obfuscate the code. Very usefull, hard to configure.

Editor's Notes

  • #4: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
  • #17: http://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html