There are fewer tutorials on iOS performance tuning, and the decision to write an iOS performance Tuning series focuses on several aspects of memory leaks, performance optimization, traffic, and power analysis.
Xcode has provided a very powerful performance tuning tool, combined with several third-party tools and some techniques, to optimize performance very simply.
The first writing is the simplest, analyze static analysis.
When iOS developers build or archive the app, there are a lot of compile warnings that are generated at compile time, and the process of static analysis is similar, and under the Xcode product menu, click Analyze to perform a static analysis of the app.
Analyze mainly analyzes the following four types of problems:
1, logic error: access to null pointers or uninitialized variables, etc.;
2, memory management error: such as memory leaks;
3. Declaration error: A variable that has never been used;
4. API call Error: The libraries and frameworks used are not included.
Analyze Memory leak Analysis:
Declaration errors, logical errors, API call errors are basically warnings at compile time, and the main advantage of analyze is static parsing of memory leaks and code logic errors.
For example, in an arc-enabled environment, enter the following code:
12345678910111213 |
//截取部分图像
+(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), although arc is opened, it still causes the Imageref object to leak.
Using Analyze for analysis, in the navigation bar Analyze Select Analyzer to view the analysis results:
Analyze has analyzed that the Imageref object has a memory leak, which is not found at compile time.
If you do not use arc, then analyze is more useful.
The other three types of analyze analysis can also be used, which is more explicit than the information given by the compiler.
Analyze Logic Error Monitoring:
This situation is also more difficult to find when Codereview, can use analyze.
As in the code, when tag is not equal to 1, 2, and 3, there will be a problem.
Analyze also gives an arrow tip: Len is a garbage value. It is recommended that the variables be initialized at the same time when declaring them.
iOS Performance Tuning series: Analyze Static analysis