IOS Performance Tuning series: use Instruments to dynamically Analyze memory leaks and tune instruments

Source: Internet
Author: User

IOS Performance Tuning series: use Instruments to dynamically Analyze memory leaks and tune instruments

Hardware wide: The second article in the IOS Performance Tuning series, which is continuously updated.

The first article introduces Analyze's static analysis of the App, which can detect memory leaks in the App. If some memory leaks cannot be solved through static analysis, it can be found through dynamic analysis, the analysis is more targeted.

This article introduces the powerful analysis tool Instruments provided by XCode. Memory analysis is only a function of Instruments. Other functions will be introduced later.

Use Instruments to dynamically Analyze memory leakage

The Leaks function in Instruments is mainly used to Analyze memory Leaks. The example of Memory leakage in IOS Performance Tuning series: Analyze static analysis is also used:

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

Comment out the CGImageRelease (imageRef) line. Even if the ARC is enabled, it will still cause memory leakage (Arc is only for NSObject ).

Use Leaks to start dynamic analysis, and click the XCode Product menu Profile to start Instruments:

When you select Leaks, the Leaks tool and IOS simulator are automatically started:

After Leaks is started, recording starts. With the operation on the App running the simulator, you can view the memory usage in Leaks.

NOTE: If your project uses ARC, As you enable or disable the view continuously, the memory may continue to rise, but this does not necessarily indicate Memory leakage, the timing of ARC release is not fixed.

There are two columns at the top of Leaks: Allocations and Leaks. The curve on the right shows the memory allocation and Memory leakage curves.

Click Leaks in the second column for Memory Leak analysis. The Leaks debugging option appears in the lower left corner:

We recommend that you set the Interval of Snapshot Interval to 10 seconds. If you select Automatic Snapshotting, Leaks automatically performs memory capture analysis.

You can click Snapshot Now for manual capture before and after you suspect there is a memory leak.

The following is a view of the + (UIImage *) getSubImage :( unsigned long) ulUserHeader function in my App. Memory leakage can be found:

The Leaked Object table shows the memory leakage type, quantity, and memory space.

Click a specific memory leakage object, and a possible location will appear in the Detail window on the right. The black Avatar represents the most likely location.

Leaks has successfully found the [CMTool getSubImage:] function:

Dynamic Analysis of Memory leakage

 

If you are familiar with Leaks, you will be able to determine more accurately the memory leakage. You can use Snapshot Now to manually capture the leakage.

If the device performs well at the beginning, you can set the automatic capture interval to 5 seconds.

When using ARC projects, memory leaks are generally caused by malloc, custom structures, and resources. Pay more attention to these issues for analysis.

Causes of Memory leakage after ARC is enabled

If ARC is enabled, there will not be any memory problems. Apple's famous saying is: ARC is only for NSObject.

In IOS, the memory allocated by malloc is not processed by ARC and must be processed by itself.

CGImageRef in this example is also an Image pointer, and ARC will not process it.

Record, for better yourself!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.