[Android interview question-4] Android Memory Optimization

Source: Internet
Author: User

Question:Please briefly describe how you optimize the memory during Android development.

Analysis:As we all know, Android applications run in Java virtual machines, and garbage collection uses the GC mechanism in Java. The system schedules the whole process and developers cannot directly intervene, therefore, it is particularly important to properly use the memory during development.

1. Bitmap optimization:

In Android applications, bitmap's image resources consume the most memory. In the adnroid system, when reading bitmap, the size of the stack allocated to the virtual machine is only 8 Mb. If it exceeds the size, it will OOM (out of memory ).

A> you need to recycle bitmap in time. After using bitmap, run the following code:

if(bitmap && !bitmap.isRecycle()){   bitmap.recycle();   bitmap = null;}system.gc();

Note that the last sentence system. GC (); only requests the system to recycle garbage as soon as possible, and does not ensure that the system responds immediately.

Because the bitmap object is generated by calling the JNI interface, the JNI interface must also be called for release. Therefore, after loading a bitmap object to the memory, it contains two parts: Java and C. The GC mechanism of the system can only recycle the Java part of the memory, but cannot release the C part of the memory. The C part of the memory can only be released by calling the recycle () interface.

If you do not call the recycle () interface, is there a memory leak? No. When the application process is killed by the application itself or by the system, the memory of the whole process is released, of course, including the C part.

B> capture exceptions

Bitmap bitmap = null;try{   bitmap = BitmapFactory.decodeFile(path);}catch(OutOfMemoryError e){   // do something}if(!bitmap)   return default_bitmap;

In this way, when OOM occurs, the application will not crash, but a default image will be displayed. Note that the catch is an error, not an exception.

C> cache common bitmap objects

For images that need to be used repeatedly in an application, you can retain only one copy in the memory, that is, you can define them as global variables or static variables.

D> compress images
2. Use soft references and weak references

Java divides object references into four levels, so that the program can control the object lifecycle more flexibly. The four levels are from high to low: strong reference, soft reference, weak reference, and virtual reference.

A> strong references are common usage and will not be repeated.

B> soft reference. If an object has only soft reference, it is the same as a strong referenced object when the memory is sufficient. When the memory is insufficient, the system recycles it.

private Map<String , SoftReference<Bitmap>> imageCache = new HashMap<String, SoftReference<Bitmap>>;public void addBmToCache(String path){  Bitmap bm = BitmapFactory.decodeFile(path);  SoftReference<Bitmap> softbm = new SoftReference<Bitmap>(bm);  imageCache.put(path, softbm);}public Bitmap getBmByPath(String path){  SoftReference<Bitmap> softbm = imageCache.get(path);  if(softbm == null)     return null;  Bitmap bm = softbn.get();  return bm;}

C> weak references. If an object has only weak references, the object will be recycled no matter whether the memory is sufficient, once such objects are found during GC thread scanning.

The difference between soft reference and weak reference is that the life cycle of the weak reference is shorter and may be recycled at any time. Soft reference is recycled only when the memory is insufficient.

D> virtual references are similar to virtual ones. They are mainly used to track memory reclaim and must be used together with the reference queue.

Due to the limited level of the author, it is inevitable that there will be errors in the train of thought or code provided for each interview question. Readers should also criticize and correct the question. In addition, I am very grateful to welcome readers to offer more and better interview questions. If you have any comments or suggestions, please refer to the comments.
The blogger Xu fanglei has copyright to this blog article. For Network reprint, please specify the source http://blog.csdn.net/shishengshi. To organize the publications, contact the author.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.