Mobile terminal (Android, iOS) memory leak detection method

1. Memory leak

  • A memory leak refers to the fact that the dynamically allocated memory in the program is not released after use. Its general manifestation is that the longer the program runs, the more memory it takes up, and eventually all memory is used up, and the entire system crashes. A piece of memory requested by the program, and there is no pointer pointing to it, then this piece of memory is leaked.

2. Memory leak detection

  • During the development process, memory leaks are generally difficult to locate and troubleshoot, especially on the Android platform.
  • For iOS and Android cross-platform public code or library parts, you can first check on the iOS side by using Xcode’s Instrument tool to accurately locate the memory leak, and then push back to the Android platform after repairing.
  • But if only the memory leak of the Android platform layer (such as the Java layer), this method is powerless.

3. Use Xcode's built-in tool Instruments for memory leak detection

Xcode's Instruments provides Leaks and Allocations tools to allow us to accurately locate memory leaks

①As shown in the figure, open the top toolbar of the project and click Xcode – Open Developer Tool – Instruments
Please add a picture description
②Open Instruments and select the Leaks tool to open
Please add a picture description
③After opening the Leaks tool, the functions of the top buttons are start/end detection, pause detection, select detection equipment, Select the detected App
Please add a picture description
④ to start running, select the one on the left Leaks, and you will see the time period of the memory leak on the right. Below you can see the specific memory leak stack

  • The red ❌ means that a new memory leak was detected during this period
  • A gray ➖ indicates that no new leaks were detected during this period (but previous leaks were not resolved)
  • The green ✅ means that there is no memory leak at all
    Please add a picture description
    ⑤ Set the scope and method of stack grabbing as needed
  • Separate By Thread: Stack backtracking is separated by thread
  • Invert Call Tree: Flip the order of the call stack (look back from the leak or look from the top call to the leak)
  • Hide System Libraries: Hide the part of the system library in the stack
  • Flatten Recursion: recursive calls are merged into one
    Please add a picture description
    ⑥ Double-click here to jump to the source code of the problem
    Please add a picture description
    Please add a picture description

Guess you like

Origin blog.csdn.net/q345911572/article/details/127399947