iOS development-instruments Performance Tuning

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Performance is an important part of Apple auditing, CPU, memory, graphics, storage space and network performance are all important evaluations and components of the Application. Whether as a personal application developer or a developer of the enterprise, all need to follow a principle is to stand in the User's perspective to think about problems, such as common adaptation problems, not because of different models resulting in the final UI effect difference is very large, the user in the process of using the best not to show obvious lag phenomenon, Or more serious is the app flash, the result of the flash back is the application is deleted, this is a more serious problem. Instruments is one of the most powerful performance debugging tools available today, helping us to address the performance issues that exist with our Applications. Instruments is included in the memory management work, I/O and the network in the past, This article is mainly about the allocation and time Profle.</span></p></p><span style="font-family: ‘Microsoft YaHei‘;"><span style="font-family: ‘Microsoft YaHei‘;">Allocations (memory Allocation)</span></span><p><p><span style="font-family: ‘Microsoft YaHei‘;">Before debugging, let's look at one:</span></p></p><p><p></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">is the dynamic text output in the view, paste the code below you will find that the beginning is good to run to the slow and the snail has a spell, do not run too long, otherwise the memory is not enough, the code is as Follows:</span></p></p><pre class="brush:csharp;gutter:true;">@implementation fetextview-(id) initwithframe: (cgrect) frame text: (nsstring *) text {self = [super initwithframe:frame]; If (self) {[nstimer scheduledtimerwithtimeinterval:0.01 target:self selector: @selector (appendnextcharacter) US Erinfo:nil repeats:yes]; _text = [text copy]; Self.backgroundcolor = [uicolor whitecolor]; } return self;} Original Address: http://www.cnblogs.com/xiaofeixiang-(void) appendnextcharacter {for (nsuinteger i = 0; i <= self.index; i++) {if (i < Self.text.length) {UILabel *label = [[UILabel alloc] init]; Label.text = [self.text Substringwithrange:nsmakerange (i,1)]; Label.opaque = NO; [label sizetofit]; CGRect frame = label.frame; Frame.origin = [self originatindex:i fontSize:label.font.pointSize]; label.frame=frame; [self addsubview:label]; }} self.index++;} -(cgpoint) originatindex: (NSUInteger) index fontSize: (cgfloat) fontSize {if (index = = 0) {return cgpointzero; } else {cgpoint origin = [self originatindex:index-1 fontsize:fontsize]; NSString * prevcharacter = [self.text Substringwithrange:nsmakerange (index-1,1)]; Cgsize prevcharactersize = [prevcharacter sizewithattributes:@{nsfontattributename: [uifont systemfontofsize:fontsize] }]; Origin.x + = prevcharactersize.width; If (origin.x > cgrectgetwidth (self.bounds)) {origin.x = 0; ORIGIN.Y + = prevcharactersize.height; } return origin; }} @end</pre><p><p><span style="font-family: ‘Microsoft YaHei‘;">The calling code for the main view controller:</span></p></p><pre class="brush:csharp;gutter:true;"><pre class="brush:csharp;gutter:true;"> NSString * path = [[nsbundle mainbundle] pathforresource:@ "flyelephant" oftype:@ "txt"]; Fetextview *textview = [[fetextview alloc] initwithframe:cgrectmake (0, +, cgrectgetwidth (self.view.bounds), Cgrectgetheight (SELF.VIEW.BOUNDS)) text:[nsstring stringwithcontentsoffile:path encoding: Nsutf8stringencoding error:nil]]; [self.view addsubview:textview];</pre></pre><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">This time we can perform memory analysis via allocation, switch Xcode to release status, and find allocations through Product→profile (cmd+i):</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;"></span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">1. The red button is to stop and start the application, do not understand the pause, objective-c all the objects are allocated on the heap, remember to check all heap allocations:</span></p></p><p><p></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">2. Click on the all Heap Allocation and tick the call Tree without viewing the System's library of Functions:</span></p></p><p><p></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">3. The specific method occupies the memory, can be opened step by step, the effect is as Follows:</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;"></span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">The above is a regular use of allocations, there are several options in the box for the second picture to Explain:</span></p></p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;"><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">separate by thread: each thread should be considered separately, taking into account the presence of GCD in the application;</span></span><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;"><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Invert call Tree: from the down trace stack, which means that the method you see in the table will have been sampled from frame No. 0, taking advantage of the advanced post-out features of the stack, we can see the recently called function at the top of the stack;</span></span><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;"><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Hide System Libraries: Checking this will show the App's code, which is very useful;</span></span><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;"><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">Flatten recursion: Recursive function, one entry per stack trace;</span></span><span style="font-family: ‘Microsoft YaHei‘;">time <span style="font-family: ‘Microsoft YaHei‘;">Profiler</span></span><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">The time Profiler is below the Allocations:</span></p></p><p><p></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">1.Allocations can better view the occupied memory, time Profiler can better see the times:</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;"></span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">2. Click the Originatindex method that consumes the most time, the effect is as Follows:</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;"></span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;">3. To see the percentage of elapsed time for each line of code, click the Xcode icon to see the code in Xcode:</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘; font-size: 14px;"></span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘;">This is probably the case with the basic user of time profiler, about development we may also use the leak memory leak tool, The basic method is similar, about the memory leak includes two kinds of leaks:</span></p></p><p><p><span style="font-family: ‘Microsoft YaHei‘;">The first memory leak is that the generated instance object is not freed after it is exhausted, which causes the memory to not be reused;</span></p></p><span style="font-family: ‘Microsoft YaHei‘;">the <span style="font-family: ‘Microsoft YaHei‘;">second memory leak occurs when the memory continues to be allocated to form a circular reference and never has the opportunity to be released;</span></span><p><p>iOS development-instruments Performance Tuning</p></p></span>

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.