SlideShare une entreprise Scribd logo
1  sur  1
Télécharger pour lire hors ligne
Memory
memory leaks
memory churns
Best practices
Don't use a bigger primitive type if you don't
really need it: never use long, float, or double if
you can represent the number with an integer.
Data types
Autoboxing
should be avoided as much as possible
Sparse array family
The number of objects you are dealing with is below a thousand and you are
not going to do a lot of additions and deletions
You are using collections of maps with few items, but lots of iterations
SparseArray
LongSparseArray
SparseIntArray
SparseLongArray
SparseBooleanArray
ArrayMap
Array & Collections
Array
Enhanced for loop syntax is
the fastest way
Enum
static final integer values
Constants
should be static and final
memory savings
avoid initialization in the Java compiler <clinit> method. Object management
Create fewer temporary objects, as they are often garbage collected,
avoid instantiating unnecessary objects, as they are expensive for memory
and computational performance.
StringBuffer
StringBuilder
thread safe
To avoid this unnecessary waste of memory, if you know an estimation
of the string capacity you are dealing with
work on character arrays
String concatenation
Local variables
extract that object from the method
only instantiated once and it's available throughout the life cycle of the class object
Streams
incorrect closeTWR
Memory patterns
The object pool pattern
AsyncTask worker thread
RecyclerView recycled views
limit garbage collection
avoid memory churns
The FlyWeight pattern
expensive creation objects
reduce the load into memory by saving the state shared by all of the objects
Android component leaks
Activities
There is a strong reference between the activity and every
single contained view
static classes
static fields with activity dependencies
singletons
keyword this inside the activity code
strong reference to an object with a longer lifetime.
Context.getApplicationContext()
Non-static inner classes
set the inner class as a static one
provide the reference to that
use a weaker reference to achieve cleaner memory management
Singletons
use a WeakReference inside singleton
Anonymous inner classes
Handlers
Services
handler.postDelayed
anonymous inner classes, let's export that class, setting it as static
handler.removeCallbacksAndMessages(null);
Use IntentService every time you can because of the automatic nalization and because this way you don't risk creating memory leaks with services.
make sure that the Service is finished as soon as it completes its task.
The memory API
Never use the largeHeap attribute inside the Android manifest file to avoid OutOfMemoryError
ComponentCallback2
@Override
public void onTrimMemory(int level) {
switch (level) {
case TRIM_MEMORY_COMPLETE:
//app invisible - mem low - lru bottom
case TRIM_MEMORY_MODERATE:
//app invisible - mem low - lru medium
case TRIM_MEMORY_BACKGROUND:
//app invisible - mem low - lru top
case TRIM_MEMORY_UI_HIDDEN:
//app invisible - lru top
case TRIM_MEMORY_RUNNING_CRITICAL:
//app visible - mem critical - lru top
case TRIM_MEMORY_RUNNING_LOW:
//app visible - mem low - lru top
case TRIM_MEMORY_RUNNING_MODERATE:
//app visible - mem moderate - lru top
break;
}
}
onTrimMemory
LogCat
if the behavior of our application is correct.
Dalvik
D/dalvikvm: <GcReason> <AmountFreed>, <HeapStats>, <ExternalMemoryStats>, <PauseTime>
GC_CONCURRENT: It follows the GC event when the heap needs to be cleared.
GC_FOR_MALLOC: It follows the request of allocation of new memory, but there is not enough space to do it.
GC_HPROF_DUMP_HEAP: It follows a debug request to profile the heap. We will see what this means in the following pages.
GC_EXPLICIT: It follows a forced explicit request of System.gc() that, as we mentioned, should be avoided.
GC_EXTERNAL_ALLOC: It follows a request for external memory. This can happen only on devices lower or equal to Android Gingerbread (API Level 10), because in those devices, memory has different entries, but for later devices the memory is handled in the heap as a whole.
D/dalvikvm(9932): GC_CONCURRENT freed 1394K, 14% free 32193K/37262K, external 18524K/24185K, paused 2ms
ART
I/art: <GcReason> <GcName> <ObjectsFreed>(<SizeFreed>) AllocSpace Objects, <LargeObjectsFreed>(<LargeObjectSizeFreed>) <HeapStats> LOS objects, <PauseTimes>
Concurrent: It follows a concurrent GC event. This kind of event is executed in a different thread from the allocating one, so this one doesn't force the other application threads to stop, including the UI thread.
Alloc: It follows the request for the allocation of new memory, but there is not enough space to do it. This time, all the application threads are blocked until the garbage collection ends.
Explicit: It follows a forced explicit request of System.gc() that should be avoided for ART as well as for Dalvik.
NativeAlloc: It follows the request for memory by native allocations.
CollectorTransition: It follows the garbage collector switch on low memory devices.
HomogenousSpaceCompact: It follows the need of the system to reduce memory usage and to defragment the heap.
DisableMovingGc: It follows the collection block after a call to a particular internal method, called GetPrimitiveArrayCritical.
HeapTrim: It follows the collection block because a heap trim isn't finished.
I/art : Explicit concurrent mark sweep GC freed 125742(6MB) AllocSpace objects, 34(576KB) LOS objects, 22% free, 25MB/32MB, paused 1.621ms total 73.285ms
The ActivityManager API
setWatchHeapLimit
clearWatchHeapLimit
https://developer.android.com/studio/profile/allocation-tracker-walkthru.html
https://developer.android.com/studio/profile/heap-viewer-walkthru.html
Getting a sense of how your app allocates and frees memory.
Identifying memory leaks
call stack
size allocating code
types of objects
how many
sizes
https://developer.android.com/studio/profile/am-memory.html
slowness might be related to excessive garbage collection events
crashes may be related to running out of memory
Memory Monitor
Heap Viewer
Allocation Tracker
compare the heap dumps
creates multiple times but doesn’t destroy
growing memory leak
large arrays
https://developer.android.com/studio/profile/investigate-ram.html
https://developer.android.com/studio/profile/am-allocation.html
object types
Analyzer Tasks
leaked activities
duplicate strings
View
List
for > while > Iterator
private void iteratorCycle(List<String> list) {
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String stemp = iterator.next();
}
}
private void whileCycle(List<String> list) {
int j = 0;
while (j < list.size()) {
String stemp = (String) list.get(j);
j++;
}
}
private void forCycle(List<String> list) {
for (int i = 0; i < list.size(); i++) {
String stemp = (String) list.get(i);
}
}
private void classicCycle(Dummy[] dummies) {
int sum = 0;
for (int i = 0; i < dummies.length; ++i) {
sum += dummies[i].dummy;
}
}
private void fasterCycle(Dummy[] dummies) {
int sum = 0;
int len = dummies.length;
for (int i = 0; i < len; ++i) {
sum += dummies[i].dummy;
}
}
private void enhancedCycle(Dummy[] dummies) {
int sum = 0;
for (Dummy a : dummies) {
sum += a.dummy;
}
}
java.lang.Byte
java.lang.Short
java.lang.Integer
java.lang.Long
java.lang.Float
java.lang.Double
java.lang.Boolean
java.lang.Character
byte: 8 bits
short: 16 bits
int: 32 bits
long: 64 bits
float: 32 bits
double: 64 bits
boolean: 8 bits, but it depends on the virtual machine
char: 16 bits
CPU calculation performance cost memory saving
HashMap
memory wasteCPU operations faster
avoid autoboxing be careful autoboxing
Compaction Resize
define type compile check
use annotation to have a limited number of values
new too many object
Good:
Bad:
Fix:
Basic
Iterate speed
Flow
Key
Runnable

Contenu connexe

Similaire à performance optimization: Memory

Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android ApplicationsLokesh Ponnada
 
Efficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsEfficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsPhillip Koza
 
Java Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningJava Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningCarol McDonald
 
ACADGILD:: ANDROID LESSON-How to analyze &amp; manage memory on android like ...
ACADGILD:: ANDROID LESSON-How to analyze &amp; manage memory on android like ...ACADGILD:: ANDROID LESSON-How to analyze &amp; manage memory on android like ...
ACADGILD:: ANDROID LESSON-How to analyze &amp; manage memory on android like ...Padma shree. T
 
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
Performance Tuning -  Memory leaks, Thread deadlocks, JDK toolsPerformance Tuning -  Memory leaks, Thread deadlocks, JDK tools
Performance Tuning - Memory leaks, Thread deadlocks, JDK toolsHaribabu Nandyal Padmanaban
 
Java programing considering performance
Java programing considering performanceJava programing considering performance
Java programing considering performanceRoger Xia
 
Mobile Developer Summit 2012, Pune
Mobile Developer Summit 2012, PuneMobile Developer Summit 2012, Pune
Mobile Developer Summit 2012, PuneBhuvan Khanna
 
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Maarten Balliauw
 
Android programming -_pushing_the_limits
Android programming -_pushing_the_limitsAndroid programming -_pushing_the_limits
Android programming -_pushing_the_limitsDroidcon Berlin
 
Designing and coding Series 40 Java apps for high performance
Designing and coding Series 40 Java apps for high performanceDesigning and coding Series 40 Java apps for high performance
Designing and coding Series 40 Java apps for high performanceMicrosoft Mobile Developer
 
Garbage collection
Garbage collectionGarbage collection
Garbage collectionSomya Bagai
 
DotNetFest - Let’s refresh our memory! Memory management in .NET
DotNetFest - Let’s refresh our memory! Memory management in .NETDotNetFest - Let’s refresh our memory! Memory management in .NET
DotNetFest - Let’s refresh our memory! Memory management in .NETMaarten Balliauw
 
Memory Leaks on Android
Memory Leaks on AndroidMemory Leaks on Android
Memory Leaks on AndroidOmri Erez
 
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...Maarten Balliauw
 
Exploring .NET memory management - JetBrains webinar
Exploring .NET memory management - JetBrains webinarExploring .NET memory management - JetBrains webinar
Exploring .NET memory management - JetBrains webinarMaarten Balliauw
 
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management....NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...NETFest
 

Similaire à performance optimization: Memory (20)

Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android Applications
 
Efficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java ApplicationsEfficient Memory and Thread Management in Highly Parallel Java Applications
Efficient Memory and Thread Management in Highly Parallel Java Applications
 
Java Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningJava Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and Tuning
 
ACADGILD:: ANDROID LESSON-How to analyze &amp; manage memory on android like ...
ACADGILD:: ANDROID LESSON-How to analyze &amp; manage memory on android like ...ACADGILD:: ANDROID LESSON-How to analyze &amp; manage memory on android like ...
ACADGILD:: ANDROID LESSON-How to analyze &amp; manage memory on android like ...
 
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
Performance Tuning -  Memory leaks, Thread deadlocks, JDK toolsPerformance Tuning -  Memory leaks, Thread deadlocks, JDK tools
Performance Tuning - Memory leaks, Thread deadlocks, JDK tools
 
Java programing considering performance
Java programing considering performanceJava programing considering performance
Java programing considering performance
 
Mobile Developer Summit 2012, Pune
Mobile Developer Summit 2012, PuneMobile Developer Summit 2012, Pune
Mobile Developer Summit 2012, Pune
 
Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)Exploring .NET memory management (iSense)
Exploring .NET memory management (iSense)
 
Android programming -_pushing_the_limits
Android programming -_pushing_the_limitsAndroid programming -_pushing_the_limits
Android programming -_pushing_the_limits
 
Designing and coding Series 40 Java apps for high performance
Designing and coding Series 40 Java apps for high performanceDesigning and coding Series 40 Java apps for high performance
Designing and coding Series 40 Java apps for high performance
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
 
DotNetFest - Let’s refresh our memory! Memory management in .NET
DotNetFest - Let’s refresh our memory! Memory management in .NETDotNetFest - Let’s refresh our memory! Memory management in .NET
DotNetFest - Let’s refresh our memory! Memory management in .NET
 
Memory Leak In java
Memory Leak In javaMemory Leak In java
Memory Leak In java
 
Memory Leaks on Android
Memory Leaks on AndroidMemory Leaks on Android
Memory Leaks on Android
 
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
JetBrains Day Seoul - Exploring .NET’s memory management – a trip down memory...
 
C# Unit 2 notes
C# Unit 2 notesC# Unit 2 notes
C# Unit 2 notes
 
Code Quality Management iOS
Code Quality Management iOSCode Quality Management iOS
Code Quality Management iOS
 
Android Memory Management
Android Memory ManagementAndroid Memory Management
Android Memory Management
 
Exploring .NET memory management - JetBrains webinar
Exploring .NET memory management - JetBrains webinarExploring .NET memory management - JetBrains webinar
Exploring .NET memory management - JetBrains webinar
 
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management....NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
.NET Fest 2018. Maarten Balliauw. Let’s refresh our memory! Memory management...
 

Plus de 晓东 杜

Stability issues of user space
Stability issues of user spaceStability issues of user space
Stability issues of user space晓东 杜
 
performance optimization: UI
performance optimization: UIperformance optimization: UI
performance optimization: UI晓东 杜
 
Embedded Android
Embedded AndroidEmbedded Android
Embedded Android晓东 杜
 
Openwrt wireless
Openwrt wirelessOpenwrt wireless
Openwrt wireless晓东 杜
 
Openwrt startup
Openwrt startupOpenwrt startup
Openwrt startup晓东 杜
 
Openwrt frontend backend
Openwrt frontend backendOpenwrt frontend backend
Openwrt frontend backend晓东 杜
 
DevOps at DUDU
DevOps at DUDUDevOps at DUDU
DevOps at DUDU晓东 杜
 

Plus de 晓东 杜 (7)

Stability issues of user space
Stability issues of user spaceStability issues of user space
Stability issues of user space
 
performance optimization: UI
performance optimization: UIperformance optimization: UI
performance optimization: UI
 
Embedded Android
Embedded AndroidEmbedded Android
Embedded Android
 
Openwrt wireless
Openwrt wirelessOpenwrt wireless
Openwrt wireless
 
Openwrt startup
Openwrt startupOpenwrt startup
Openwrt startup
 
Openwrt frontend backend
Openwrt frontend backendOpenwrt frontend backend
Openwrt frontend backend
 
DevOps at DUDU
DevOps at DUDUDevOps at DUDU
DevOps at DUDU
 

Dernier

W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
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 ...OnePlan Solutions
 
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-...Steffen Staab
 
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 AIABDERRAOUF MEHENNI
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
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 Modelsaagamshah0812
 
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 serviceanilsa9823
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
+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
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
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 PrecisionSolGuruz
 
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.pdfkalichargn70th171
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
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 ApplicationsAlberto González Trastoy
 
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.pdfkalichargn70th171
 

Dernier (20)

W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
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 ...
 
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-...
 
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
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
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
 
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
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
+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...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
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
 
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
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
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
 
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
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
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
 
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
 

performance optimization: Memory

  • 1. Memory memory leaks memory churns Best practices Don't use a bigger primitive type if you don't really need it: never use long, float, or double if you can represent the number with an integer. Data types Autoboxing should be avoided as much as possible Sparse array family The number of objects you are dealing with is below a thousand and you are not going to do a lot of additions and deletions You are using collections of maps with few items, but lots of iterations SparseArray LongSparseArray SparseIntArray SparseLongArray SparseBooleanArray ArrayMap Array & Collections Array Enhanced for loop syntax is the fastest way Enum static final integer values Constants should be static and final memory savings avoid initialization in the Java compiler <clinit> method. Object management Create fewer temporary objects, as they are often garbage collected, avoid instantiating unnecessary objects, as they are expensive for memory and computational performance. StringBuffer StringBuilder thread safe To avoid this unnecessary waste of memory, if you know an estimation of the string capacity you are dealing with work on character arrays String concatenation Local variables extract that object from the method only instantiated once and it's available throughout the life cycle of the class object Streams incorrect closeTWR Memory patterns The object pool pattern AsyncTask worker thread RecyclerView recycled views limit garbage collection avoid memory churns The FlyWeight pattern expensive creation objects reduce the load into memory by saving the state shared by all of the objects Android component leaks Activities There is a strong reference between the activity and every single contained view static classes static fields with activity dependencies singletons keyword this inside the activity code strong reference to an object with a longer lifetime. Context.getApplicationContext() Non-static inner classes set the inner class as a static one provide the reference to that use a weaker reference to achieve cleaner memory management Singletons use a WeakReference inside singleton Anonymous inner classes Handlers Services handler.postDelayed anonymous inner classes, let's export that class, setting it as static handler.removeCallbacksAndMessages(null); Use IntentService every time you can because of the automatic nalization and because this way you don't risk creating memory leaks with services. make sure that the Service is finished as soon as it completes its task. The memory API Never use the largeHeap attribute inside the Android manifest file to avoid OutOfMemoryError ComponentCallback2 @Override public void onTrimMemory(int level) { switch (level) { case TRIM_MEMORY_COMPLETE: //app invisible - mem low - lru bottom case TRIM_MEMORY_MODERATE: //app invisible - mem low - lru medium case TRIM_MEMORY_BACKGROUND: //app invisible - mem low - lru top case TRIM_MEMORY_UI_HIDDEN: //app invisible - lru top case TRIM_MEMORY_RUNNING_CRITICAL: //app visible - mem critical - lru top case TRIM_MEMORY_RUNNING_LOW: //app visible - mem low - lru top case TRIM_MEMORY_RUNNING_MODERATE: //app visible - mem moderate - lru top break; } } onTrimMemory LogCat if the behavior of our application is correct. Dalvik D/dalvikvm: <GcReason> <AmountFreed>, <HeapStats>, <ExternalMemoryStats>, <PauseTime> GC_CONCURRENT: It follows the GC event when the heap needs to be cleared. GC_FOR_MALLOC: It follows the request of allocation of new memory, but there is not enough space to do it. GC_HPROF_DUMP_HEAP: It follows a debug request to profile the heap. We will see what this means in the following pages. GC_EXPLICIT: It follows a forced explicit request of System.gc() that, as we mentioned, should be avoided. GC_EXTERNAL_ALLOC: It follows a request for external memory. This can happen only on devices lower or equal to Android Gingerbread (API Level 10), because in those devices, memory has different entries, but for later devices the memory is handled in the heap as a whole. D/dalvikvm(9932): GC_CONCURRENT freed 1394K, 14% free 32193K/37262K, external 18524K/24185K, paused 2ms ART I/art: <GcReason> <GcName> <ObjectsFreed>(<SizeFreed>) AllocSpace Objects, <LargeObjectsFreed>(<LargeObjectSizeFreed>) <HeapStats> LOS objects, <PauseTimes> Concurrent: It follows a concurrent GC event. This kind of event is executed in a different thread from the allocating one, so this one doesn't force the other application threads to stop, including the UI thread. Alloc: It follows the request for the allocation of new memory, but there is not enough space to do it. This time, all the application threads are blocked until the garbage collection ends. Explicit: It follows a forced explicit request of System.gc() that should be avoided for ART as well as for Dalvik. NativeAlloc: It follows the request for memory by native allocations. CollectorTransition: It follows the garbage collector switch on low memory devices. HomogenousSpaceCompact: It follows the need of the system to reduce memory usage and to defragment the heap. DisableMovingGc: It follows the collection block after a call to a particular internal method, called GetPrimitiveArrayCritical. HeapTrim: It follows the collection block because a heap trim isn't finished. I/art : Explicit concurrent mark sweep GC freed 125742(6MB) AllocSpace objects, 34(576KB) LOS objects, 22% free, 25MB/32MB, paused 1.621ms total 73.285ms The ActivityManager API setWatchHeapLimit clearWatchHeapLimit https://developer.android.com/studio/profile/allocation-tracker-walkthru.html https://developer.android.com/studio/profile/heap-viewer-walkthru.html Getting a sense of how your app allocates and frees memory. Identifying memory leaks call stack size allocating code types of objects how many sizes https://developer.android.com/studio/profile/am-memory.html slowness might be related to excessive garbage collection events crashes may be related to running out of memory Memory Monitor Heap Viewer Allocation Tracker compare the heap dumps creates multiple times but doesn’t destroy growing memory leak large arrays https://developer.android.com/studio/profile/investigate-ram.html https://developer.android.com/studio/profile/am-allocation.html object types Analyzer Tasks leaked activities duplicate strings View List for > while > Iterator private void iteratorCycle(List<String> list) { Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) { String stemp = iterator.next(); } } private void whileCycle(List<String> list) { int j = 0; while (j < list.size()) { String stemp = (String) list.get(j); j++; } } private void forCycle(List<String> list) { for (int i = 0; i < list.size(); i++) { String stemp = (String) list.get(i); } } private void classicCycle(Dummy[] dummies) { int sum = 0; for (int i = 0; i < dummies.length; ++i) { sum += dummies[i].dummy; } } private void fasterCycle(Dummy[] dummies) { int sum = 0; int len = dummies.length; for (int i = 0; i < len; ++i) { sum += dummies[i].dummy; } } private void enhancedCycle(Dummy[] dummies) { int sum = 0; for (Dummy a : dummies) { sum += a.dummy; } } java.lang.Byte java.lang.Short java.lang.Integer java.lang.Long java.lang.Float java.lang.Double java.lang.Boolean java.lang.Character byte: 8 bits short: 16 bits int: 32 bits long: 64 bits float: 32 bits double: 64 bits boolean: 8 bits, but it depends on the virtual machine char: 16 bits CPU calculation performance cost memory saving HashMap memory wasteCPU operations faster avoid autoboxing be careful autoboxing Compaction Resize define type compile check use annotation to have a limited number of values new too many object Good: Bad: Fix: Basic Iterate speed Flow Key Runnable