IOS App Performance Optimization

Source: Internet
Author: User
Tags uikit

While the iphone is getting better, its functionality is becoming more complex, and performance has always been one of the core concerns of mobile development. We say an app performance is good, not simply refers to the feeling of running fast, but should be the application starts quickly, the UI feedback response timely, the list scrolling operation is smooth, the memory use is reasonable, of course, not casually crash. Engineers in the development of applications in addition to the design to avoid performance "pit", in the actual encounter "pit" should also be able to quickly locate the reason. Positioning performance Problems The reason is, of course, impossible to guess, and the logical approach is to use tool measurements to assess the most rewarding problem points and then optimize them.

Start time

Resume 10 seconds Suspend 10 seconds Quit 6 seconds background Task 10 minutes

123 
CfabsolutetimeStartTime;IntMain(Intargc,char **argv) { starttime = cfabsolutetimegetcurrent();

Added in Application:didfinishlaunchingwithoptions:

dispatch_async(dispatch_get_main_queue(), ^{    NSLog(@”Lauched in %f seconds.”,  (CFAbsoluteTimeGetCurrent() – StartTime)); });可能你会觉得为什么这样可拿到系统启动的时间,因为这个dispatch_async中提交的工作会在app主线程启动后的下一个run lopp中运行,此时app已经完成了载入并且将要显示第一帧画面,也就是系统会运行到`-[UIApplication _reportAppLaunchFinished]`之前。是用Instruments工具Time Profiler跑的调用栈,Instruments的使用方法建议看WWDC中与performance相关的[session录像](https://developer.apple.com/videos/wwdc),文字写起来太单薄不够直观哈。

The system callback was completed before [UIApplication _reportapplaunchfinished] application:didFinishLaunchingWithOptions .

WWDC Session 235):

2) Uikit initialization: If the app's root View controller is implemented by Xib, it will also be initialized at startup.

application:didfinishlaunchingwithoptions

-[uiapplication _resportapplaunchfinished] Call CA::Transaction::commit to implement the first frame of the painting. If your program starts very slowly, the first thing you can do is to put the action that is not related to the display of the first screen to execute afterwards; If you load the first screen with the Xib file, the view layer in the Xib file should be flat, not too many layers.

How can you make users feel that your app is responding quickly? Of course, the actions triggered by the app user can be immediately responded to, that is, the user event can be processed by the main thread's run loop in a timely manner. What is a run loop? You can imagine a select multiplexing that handles events. The run loop in the main thread is, of course, primarily designed to handle user-generated events, such as clicking, scrolling, and so on. In the future, we'll talk about the confusing stuff of run loop.

Memory problems have always been a persistent problem with iOS apps, and the program has exploded. Because the iOS system does not have swap files (know why not?) Left out of suspense), the read-only data (such as the code page) is removed from memory when it is out of memory, read as memory from disk when needed, and the read-write data is not moved out of memory by the system, but if the memory used reaches a threshold, the system sends a notification and callbacks let you apply the release object to reclaim memory, and if you still can't reduce memory usage, the system shuts down the app directly. Especially after iOS 5.0, if your app receives memory warning, then the brain is placed on the chopping board like any other app, and is likely to be killed at any time, not to say that it will kill the app in the background first.

+[nsobject Alloc]/malloc), there will be more places to use memory, such as code and global data (text and data), line stacks, images, layer backing store for view, and so on. So dealing with memory problems is not simply about applying as little memory as possible when we're developing apps.

Another serious memory usage problem is referencing the memory that has already been freed, directly causing the app to crash, and allocation has an option to enable Nszombie detection to be annotated when the app uses the memory that has already been freed, and to display the call stack information that occurred incorrectly. This provides the most direct help to solve the problem, but the drawback is that you must be able to reproduce the exec_bad_access error.

Graphical performance has a direct impact on the user experience, and the core animation tool in instruments is used to measure the graphics performance on a physical machine and to determine the graphics performance of the application by the size of the refresh frequency of the view. For example, when a complex list is scrolled, its refresh rate should try to approach 60fps to make the user feel fluent, and from this figure it can be calculated that the longest response time of the run loop should be 16 milliseconds.

Instruments can display the blended layers blended layer (labeled in red) on the physical machine, blended layer is because they are transparent (Transparent), The system needs to mix the view and the lower view when rendering these view (blend) to calculate the actual color of the pixel, if the blended layer is many, then you can scroll the list without a smooth effect.

[Uicolor Clearcolor], to know that the background color is clear color that is the big enemy of graphics performance, basically means that the blended layer is not able to run, why? Think for yourself:)

Many view layers are rendered very high due to shadow, mask, and gradient, so Uikit provides APIs to cache these Layer:[layer Setshouldrasterize:yes], These layers are cached as bitmap bitmaps for rendering use, and if they are invalidated, they are discarded bitmap regenerated. The benefit of layer rasterization rasterization is that it has less impact on refresh rates, and the downside is that the bitmap cache requires memory to be erased, and when the layer needs to be scaled, additional calculations are made to the bitmap after the deletion. When this option is used, if the layer of the rasterized is invalidated, it will be labeled red if it is effectively labeled green. When the application of the test flashes a red callout layer frequently, it indicates that the rasterization effect on the layer does little.

Misaligned image indicates that the points to be drawn cannot be mapped directly to pixels on the frequency screen, and the system needs to do anti-aliasing anti-aliasing on neighboring pixels, increasing the graphics burden, which is usually caused by the recalculation and setting of some view frame.

(4) Color offscreen-rendered Yellow

Designing for Ios:graphics & Performance A great introduction to Offsreen and graphics performance, (5) Color Copied Images Copied image option to annotate when the app is drawn by the core Animation copy of the picture, labeled Blue-green. Although I ran into it, my personal feelings had little impact on graphics performance. (6) Color Immediately,flash Updated regions, color OpenGL Fast Path Blue Color Immediately option indicates instruments when doing color-flush operation Cancels the 10 millisecond delay. The Flash Updated regions option is used to indicate in red the layers that are drawn on the screen using GPU calculations. The Color OpenGL Fast Path blue option is used to mark the content that is drawn on the screen by OpenGL compositor in blue. These three options are less useful for the analysis of graphics performance, and are usually used as a reference only.

If you need to analyze the file and network I/O for your app, you can use the three instruments tools system Usage, file activity, and networks.

The tool file activity can only be run in the emulator, so data collection may not be very accurate. It also gives detailed information about the file attributes, size, loading time, etc. that are read, and is suitable for use with system usage.

There is a lot of knowledge about iOS app performance, which is just the tip of the iceberg, with a focus on recommending WWDC's session.

    • 238:ios app Performance:graphics and animations
    • 242:ios app Performance: Memory
    • 409:learning Instruments
    • 706:networking Best practices
    • 514:opengl ES Tools and techniques
    • 506:optimizing 2D Graphics and Animatio N Performance
    • 601:optimizing Web Content in Uiwebviews and Websites on ios
    • 225:up and running:making a great impression with every Launch

    • 105:polishing Your app:tips and tricks to improve the responsiveness and performance
    • 121:understanding UIKit Rendering
    • 131 Performance optimization on iphone OS
    • 308:blocks and Grand Central Dispatch in practice
    • 323:introducing Automatic Reference Counting
    • 312:ios Performance and Power optimization with Instruments

http://oleb.net/blog/2011/11/ios5-tech-talk-michael-jurewitz-on-performance-measurement/

Http://robots.thoughtbot.com/post/36591648724/designing-for-ios-graphics-performance

http://www.touchwonders.com/en/how-to-make-your-apps-feel-responsive-and-fast-part-2/

IOS App Performance Optimization

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.