Related knowledge:
Art:android introduced an art (Android runtime) from 4.4, and the runtime used to manage the application was Dalvik. Art adds pre-compiled, optimized GC (such as parallel processing, compressed GC time, GC processing recently allocated object faster ... )
Memory Leak: Memory is not being used, but GC is not recognized. Causes the memory to persist and cannot be freed.
Memory,gc,performance Relationship:
Simply put, memory will be separated by GC, which is divided into different spaces. Depending on the type of memory allocation (type of allocation) and the optimization of the future GC, memory is allocated in different space. Of course also depending on the Android runtime type.
Space has a certain capacity, and when the capacity becomes larger, a GC is needed to make room for future object allocations. Depending on the Android runtime version, the GC behaves differently. GC Events in Dalvik will stop running the world event, meaning that any code that is running will stop until the GC runs out.
Problem:
1. Each frame we said earlier is 16ms. You GC several times, will cause the picture is not smooth.
2. The program processing process may force the GC to be more frequent or make the GC last longer. For example, assigning a large object to a long cycle of code will contaminate many memory heaps, causing the GC to become more frequent. There is also memory leak (memory leak), which causes it to be leaked in the GC and cannot be freed. Let the GC occur more frequently.
Reasons for memory leaks:
Relatively easy to find: Circular references between useless objects. Difficult: holds the ClassLoader that loads the object. So use tools to assist.
Analysis Tools:
Use the Memorymonitor to determine the initial, and then use Heapviewer to locate the class, with allocation tracer to locate the code. The first one in Android studio,6 Android This tab memory, the latter two tools are in DDMS.
Memorymonitor:
1 Data analysis:
a) gray is the remaining assignable memory, and blue is the allocated memory.
b) Each spike is where GC time occurs.
If a large number of spikes (such as) occur in the short term. When a large number of GC events occur, a memory leak can occur.
As you can see in the green box, multiple spikes (that is, GC events) appear within a short event, indicating that there was a large number of object allocations during that time, and that the GC did not successfully release memory. And more GC events will be the Kaka interface.
But then the blue area increases, but the gray area remains less. The system adds the total memory allocation to the app, but it still can't be satisfied.
Locate a memory leak
Heapviewer:
You can see the allocated objects and allocate space. How much is left after a GC and who hasn't been taken off by GC. The location memory leaks onto an object.
To use: Click on the upper-left cylinder (with green inside) to Update the heap, then click Cause GC on the tab on the right Heap.
Allocationtracer:
can be used to monitor the allocation of a large number of classes in that method. locating memory leaks to code
In general, when the discovery program slows down, or crashes, use Memorymonitor to monitor the app for memory leaks and OOM.
Then, use Heapviewer to see which class, objects are allocating too much space on the heap.
Finally, we use Allocationtracer to locate the code.
Android performance optimization Google course translation one: Memory