Android Memory Optimization
OOM
Memory leakage causes many problems:
1: The program gets stuck and the response speed is slow (when the memory usage is high, JVM virtual opportunities frequently start GC)
2: disappears inexplicably
3: Direct crash
Problems with ANDROID memory
1: limited heap memory, originally only 16 M
2: memory size consumption varies depending on the device, operating system level, and size
3: The program cannot be directly controlled
4: supports background multi-task processing
5: running on a virtual machine
5R
1. Reckon (Computing)
First, you need to know the memory usage of your app.
2. Reduce)
Consume less resources
3. Reuse)
After the first use, try to use
5. Recycle (Recycle)
Reclaim Resources
4. Review (check)
Review and check your program to see what is unreasonable in the design or code.
1. Reckon (Computing)
First, you need to know the memory usage of your app.
2. Reduce)
Consume less resources
3. Reuse)
After the first use, try to use
5. Recycle (Recycle)
Reclaim Resources
4. Review (check)
Review and check your program to see what is unreasonable in the design or code.
Reduce:
Reduce refers to reducing and directly reducing memory usage. It is the most effective optimization method.
Bitmap:
Bitmap is a large memory consumer. The vast majority of OOM crashes are generated when Bitmap is operated. Let's take a look at several image processing methods.
Image Display:
We need to load the image size as needed
For example, the thumbnail is loaded only when the list is used for preview.
Only when you click a specific entry to view details, a fragement/activity dialog box is started to display the entire image.
1. Reckon (Computing)
First, you need to know the memory usage of your app.
2. Reduce)
Consume less resources
3. Reuse)
After the first use, try to use
5. Recycle (Recycle)
Reclaim Resources
4. Review (check)
Review and check your program to see what is unreasonable in the design or code.
Image Size:
Using ImageView directly shows that bitmap occupies a large amount of resources, especially when the image size is large, it may cause a crash.
UseBitmapFactory. OptionsSet inSampleSize to reduce requirements on system resources.
The property value inSampleSize indicates that the thumbnail size is one of several points of the original image size. If the value is 2, the width and height of the thumbnail are 1/2 of the original image, the image size is 1/4 of the original size.
1. Reckon (Computing)
First, you need to know the memory usage of your app.
2. Reduce)
Consume less resources
3. Reuse)
After the first use, try to use
5. Recycle (Recycle)
Reclaim Resources
4. Review (check)
Review and check your program to see what is unreasonable in the design or code.
Image pixels:
In Android, images have four attributes:
ALPHA_8:Each pixel occupies 1 byte of memory
ARGB_4444:Each pixel occupies 2 bytes of memory
ARGB_8888:Each pixel occupies 4 bytes of memory (default)
RGB_565:Each pixel occupies 2 bytes of memory
1. Reckon (Computing)
First, you need to know the memory usage of your app.
2. Reduce)
Consume less resources
3. Reuse)
After the first use, try to use
5. Recycle (Recycle)
Reclaim Resources
4. Review (check)
Review and check your program to see what is unreasonable in the design or code.
Image recycling:
After using Bitmap, you need to callBitmap. recycle ()To release the memory space occupied by Bitmap, instead of waiting for the Android system to release.
The following is a sample code snippet for releasing Bitmap.
[Java]View plaincopyprint?
- // First determine whether the data has been recycled
- If (bitmap! = Null &&! Bitmap. isRecycled ()){
- // Recycle and set it to null
- Bitmap. recycle ();
- Bitmap = null;
- }
- System. gc ()
1. Reckon (Computing)
First, you need to know the memory usage of your app.
2. Reduce)
Consume less resources
3. Reuse)
After the first use, try to use
5. Recycle (Recycle)
Reclaim Resources
4. Review (check)
Review and check your program to see what is unreasonable in the design or code.
When should I use soft references and weak references?
In my opinion, if you only want to avoid the occurrence of an OutOfMemory exception, you can use soft references. If you are more concerned about application performance and want to recycle objects that occupy a large amount of memory as soon as possible, you can use weak references.
In addition, you can determine whether the object is frequently used. If this object may be frequently used, try to use soft references. If this object is not used more likely, you can use weak references.
In addition, WeakHashMap is similar to the weak reference function. For a given key, WeakHashMap does not prevent the garbage collector from recycling the key. After the key is recycled, its entries are effectively removed from the ing. WeakHashMap implements this mechanism using ReferenceQueue.
1. Reckon (Computing)
First, you need to know the memory usage of your app.
2. Reduce)
Consume less resources
3. Reuse)
After the first use, try to use
5. Recycle (Recycle)
Reclaim Resources
4. Review (check)
Review and check your program to see what is unreasonable in the design or code.
Http://blog.csdn.net/a396901990/article/details/38707007
1. Reckon (Computing)
First, you need to know the memory usage of your app.
2. Reduce)
Consume less resources
3. Reuse)
After the first use, try to use
5. Recycle (Recycle)
Reclaim Resources
4. Review (check)
Review and check your program to see what is unreasonable in the design or code.