8th Android Anomaly and performance optimization related interview questions

Source: Internet
Author: User

1, ANR abnormal interview questions explained

A) what is ANR?

Application Unresponsive dialog box

b) The cause of the ANR?

Time-consuming operation in the main thread

c) What are those operations on Android in the main thread?

All life cycle callbacks for activity are performed on the main thread

Service defaults are performed on the main thread of the

The Broadcastreceiver Onreceiver callback is performed on the main thread of the

The Handlermessage,post (Runnable) of a looper that is not using a child thread is performed on the main thread of the handler

Asynctask In addition to Doinbackground, the others are executed in the main thread

D) How to resolve the ANR

Using Asynctask to process time-consuming IO operations

Use thread or handlerthread to increase priority

Use handler to handle time-consuming tasks for worker threads

Try to avoid time-consuming code in the OnCreate and onresume callbacks of the activity

2, Oom abnormal interview questions explained

A) what is Oom?

The memory that is currently occupied plus the memory resource we are requesting exceeds the maximum memory limit of the Dalvik virtual machine.

b) Some concepts that are easily confused

Memory overflow/memory jitter/memory leaks

Memory overflow: is Oom

Memory Jitter: A large number of objects are created in a short time and are immediately released

Memory leaks: When memory is no longer used by the program, freeing memory fails resulting in useless memory consumption

D) How to resolve Oom

1. About Bitmap optimization

Picture display (such as monitoring the ListView slide, stop loading a large image)

Free up memory in time

Bitmap The method is private, through the bitmapfactory generation bitmap to memory is implemented by JNI, the simple point is that there will be two parts of the area, part of the Java area, part C, but bitmap is allocated through Java, It is also recycled by the Java GC mechanism when not in use. corresponding to the C region can not be recovered in a timely manner, so the release of memory here is the release of the area of C (through

Recycle method, which invokes the method of JNI internally). If we don't release, we'll be released after the process dies.
Image compression
Inbitmap Properties (Image reuse)
Catching exceptions

2. Other methods

LISTVIEW:CONVERTVIEW/LRU (Level three cache)

Avoid performing object creation in the OnDraw method

Use multiple processes with caution

3, Bitmap interview questions explained

A) Recycle: Release bitmap memory will release the corresponding native memory, but not immediately released, but after the call can not use the bitmap, is irreversible. Unsolicited calls are not recommended by the authorities. The garbage collector proactively cleans up.

b) LRU: Level three cache, internal is implemented through map, which provides a put, get method to complete the cache add and get operations. When the cache is full, the LRU algorithm provides the trimtosize to delete the oldest or use the least cache object, adding a new cache object

c) Calculate Insamplesize:

d) Thumbnail image

    

/** * Get thumbnails * @param imagePath: File path * @param width: thumbnail width * @param height: thumbnail height * @return */Pub Lic static Bitmap Getimagethumbnail (String imagePath, int width, int height) {bitmapfactory.options Options = new        Bitmapfactory.options (); Options.Injustdecodebounds= true;        The role of Injustdecodebounds will be described below Bitmap Bitmap = Bitmapfactory.decodefile (ImagePath, Options); int h = options.outheight;//get picture height int w = options.outwidth;//get picture width int scalewidth = w/width; Calculates the width of the zoom ratio int scaleheight = H/height;        Calculate a height scale greater than int scale = 1;//Initial zoom ratio if (ScaleWidth < ScaleHeight) {//select appropriate zoom ratio scale = ScaleWidth;        } else {scale = ScaleHeight;        } if (scale <= 0) {//Determines whether the scaling ratio conforms to the conditional be = 1;        } options.insamplesize = scale;                Re-read into the picture, read the scaled bitmap, note this time to set Injustdecodebounds to false options.injustdecodebounds = false;        Bitmap = Bitmapfactory.decodefile (ImagePath, Options); Use Thumbnailutils to create thumbnails, where you specify which bitmap object to scale bitmap = Thumbnailutils.extractthumbnail (bitmap, Width, Height,thumbna        Ilutils.options_recycle_input);    return bitmap; }

e) Level Three cache

Network, local, memory level three cache for reduced traffic usage

4, the UI Kaka interview questions explained

A) The principle of the UI lag

60fps-16ms

Overdraw over drawing

b) Cause analysis of the UI lag

1. Artificially taking a slight time-consuming action in the UI thread, causing the UI line to incurring

2. Layout layouts are too complex to render within 16ms

3. Excessive number of animations performed at the same time, resulting in heavy load on CPU and GPU

Over-rendering of 4.View, causing some pixels to be drawn multiple times within the same frame, overloading the CPU and GPU

5.View frequent triggering of measure, layout, resulting in measure, layout accumulated time-consuming and the entire view of the frequent re-rendering

6. Memory frequently triggers too many GC, causing temporary blocking of render operations

7. Slow loading and execution due to redundant resources and logic

8.ANR

c) UI Lag summary

1. Layout optimization

2. List and adapter optimization

3. Memory allocation optimizations such as backgrounds and pictures

4. Avoid ANR

4. Memory leaks

A) Java Memory leak basics

1. Allocation strategy for Java memory

Static storage (method area): Mainly holds static data, global static data, and constants. This block exists when the program is compiled, and it is present during the entire run of the program.

Stack area: When a method is executed, local variables in the method body, including the underlying data types, references to objects, are created on the stack, and the memory held by those local variables is automatically freed at the end of the method execution. Because the stack memory allocation operation is built into the processor's instruction set, it is highly efficient, but the allocated memory capacity is limited.

Heap area: Also known as dynamic memory allocation, usually refers to the program when it runs directly new memory, that is, an instance of the object. The Java garbage collector will be responsible for recycling this part if it is not in use.

2. How to manage memory in Java

          

3. Memory leaks in Java

A memory leak refers to a useless object (an object that is no longer being used) that keeps memory or useless objects from being released in a timely manner, resulting in a waste of memory space called a memory leak

b) Android memory leak

8th Android Anomaly and performance optimization related interview questions

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.