IOS performance tuning series: Analyze static analysis

At present, there are few tutorials on IOS performance optimization, so I decided to write an "IOS Performance Tuning Series", which mainly focuses on memory leaks, performance optimization, traffic and power analysis.

XCode already provides very powerful performance tuning tools, combined with several third-party tools and a few tricks, performance tuning is very simple.

The first one is to write the simplest one, Analyze static analysis.

I believe that IOS developers will generate a lot of compilation warnings when they build or archive the app. These warnings are generated during compilation. The process of static analysis is similar. Under the XCode Product menu, click Analyze to perform static analysis on the App.

Analyze mainly analyzes the following four problems:

1. Logical error: access to null pointer or uninitialized variable, etc.;

2. Memory management errors: such as memory leaks, etc.;

3. Declaration error: a variable that has never been used;

4. Api call error: The used libraries and frameworks are not included.


 

Analyze memory leak analysis:

Declaration errors, logic errors, and Api call errors will basically have warnings at compile time. The main advantage of Analyze is static analysis of memory leaks and code logic errors.

For example, in an environment where arc is enabled, enter the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
//截取部分图像
+(UIImage*)getSubImage:(unsigned  long )ulUserHeader
{
     UIImage * sourceImage = [UIImage imageNamed:@ "header.png" ];
     CGFloat height = sourceImage.size.height;
     CGRect rect = CGRectMake(0 + ulUserHeader*height, 0, height, height);
     
     CGImageRef imageRef = CGImageCreateWithImageInRect([sourceImage CGImage], rect);
     UIImage* smallImage = [UIImage imageWithCGImage:imageRef];
     //CGImageRelease(imageRef);
     
     return  smallImage;
}

Comment out the line CGImageRelease(imageRef) with a comment. Although arc is turned on, it will still cause the imageRef object to leak.

Use Analyze to perform analysis, and select Analyze in the navigation bar to view the analysis results:

Analyze has analyzed that the imageRef object has a memory leak, which cannot be found at compile time.

如果你没有使用ARC,那么Analyze更有用。

Analyze的其他三种分析也可以使用,相比编译器给出的信息更明确。


 

Analyze逻辑错误监测:

这种情况在codereview时也较难发现,可以借助Analyze。

如上代码,当Tag不等于1、2和3的时候,就会出现很问题了。

Analyze还给出了箭头提示:len is a garbage value。建议在声明变量时,同时进行初始化。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326685072&siteId=291194637
Recommended