SlideShare ist ein Scribd-Unternehmen logo
1 von 1
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Ähnlich wie performance optimization: Memory

Java Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and TuningJava Garbage Collection, Monitoring, and Tuning
Java Garbage Collection, Monitoring, and Tuning
Carol McDonald
 
Mobile Developer Summit 2012, Pune
Mobile Developer Summit 2012, PuneMobile Developer Summit 2012, Pune
Mobile Developer Summit 2012, Pune
Bhuvan Khanna
 
Android programming -_pushing_the_limits
Android programming -_pushing_the_limitsAndroid programming -_pushing_the_limits
Android programming -_pushing_the_limits
Droidcon Berlin
 

Ähnlich wie 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...
 

Mehr von 晓东 杜 (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
 

Kürzlich hochgeladen

+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
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Kürzlich hochgeladen (20)

Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
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 🔝✔️✔️
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
+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...
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
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
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 

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