One, memory leak

  • A memory leak is when an object or variable is not released after it has been used, and the object continues to occupy that memory until the application stops.

Second, the cause analysis of memory leak

  • In the current ARC model of memory management, the root cause of memory leaks is the existence of circular references in the code, causing some memory not to be freed, which causes the dealloc() method not to be called. The main reasons may fall into the following categories:

(1) The infinite loop generated by the animation effect

(2) Strong references between multiple objects

(3) The use of timer NSTimer

(4) Strong references to the code block block

(5) The delegate is not decorated with the correct method

  • Solution:

(1) Remove animation effects when the view controller is about to disappear;

(2) Weak references are used between objects;

(3) Stop the timing effects when the view controller is about to disappear;

(4) Use of weak references in code blocks;

(5) Try to use assign or weak;

  • It is important to note that not all blocks need weak to break a circular reference, and if self does not hold a block it will not cause a circular reference.

3. Find leakage points (two tools)

  1. Analyze
  • Name: Static analysis tool
  • Search: This can be started using the Product ->Analyze menu item
  • Shortcut: CMD+ Shift + B
  • Analyze mainly analyzes the following four problems:
    1. Logic error: accessing null pointer, uninitialized variable, etc.
    2. Memory management errors, such as memory leaks;
    3. Declaration error: variable never used;
    4. Api call error: no library or framework used.
  1. Instruments-Leaks
  • Scientific name: dynamic analysis tool
  • Search for the Product ->Profile menu item to start
  • Shortcut key: CMD + I
  • It has a number of trace modules that dynamically analyze and track memory, CPU, and file systems.

4. Analyze use method

  • In the Analyze static analysis result, there are always ICONS

    All possible leaks found by the tools.

  • Note: Here use Analyze to find the leak point, called “suspect leak point”. It is called “suspect leak points” because they are not necessarily Leaks, and determining whether they are Leaks is also determined through the Leaks and Allocations tracking templates in the Instruments dynamic analysis tool. Analyze Static analysis is only a theoretical prediction process.

Leaks use method

  • 1. Interface description

In Instruments, while the Leaks template is selected, the Allocations template is also added by default. Basically all memory analysis uses the Allocations template, which monitors memory Allocations.

① Select the Allocations template,(Figure 1 region). Region 3 on the right shows a line chart of memory usage over time, and region 4 shows details about memory usage, as well as object Allocations.

2. Click on the Leaks template (2 area in the picture) to see the memory Leaks. If there is a red X in region 3, there is a memory leak, and region 4 shows the leaked object.

  • 2. Use Leaks for monitoring

Click on the leaked objects to see their memory location, size of bytes, owning frame, and response method. Open the extension view to see the trace stack information on the right

  • 3. Analysis of monitoring results: