Android performance optimization-memory optimization

Preface

The system allocates a certain amount of memory for each application. The specific amount of memory allocated depends on the configuration of the device hardware. This memory resource is limited. If the program does not manage the memory properly, it will eventually cause the program to be abnormal.

The memory referred to here is the size allocated to us by RAM

The specific manifestations of memory problems are as follows:

  • Memory jitter : The real-time data of the memory graphics is jagged. The entire process frequently creates and destroys reclaimed objects, which may eventually cause the UI to freeze, and in severe cases, it may also cause memory overflow
  • Memory leak : Unused objects are not recycled, and the real-time data of the memory graph is gradually increasing. The available memory will become less and less. In the end, frequent GC will cause UI freezes, even memory leaks OOM, and program crashes. .
  • Memory overflow : When the sum of used memory and newly applied memory is greater than the system allocated memory, OOM will occur. OOM is the most serious memory problem, and the entire program crashes and exits the application.

As long as the problem of memory jitter and memory leak is handled, the problem of memory overflow is very small, unless the operation of creating a large number of objects for a long time cannot be released, such as deserialization and parsing into a large number of entity classes, recursion has no end conditions, etc. .

To do memory optimization, we must understand the following points

  • Which objects can be recycled
  • When will it be recycled
  • How was it recycled

This requires knowledge of Java memory allocation, garbage collection algorithms and Android memory mechanisms, etc.

Android memory optimization-summary

Insert picture description here

Android memory optimization-MAT usage

Insert picture description here

Android memory optimization-LeakCanary2.x usage and partial source code analysis

Insert picture description here

Summary

Guess you like

Origin blog.csdn.net/hzw2017/article/details/112723636