检测iOS的APP 性能的一些方法

移动开发 iOS
首先如果遇到应用卡顿或者因为内存占用过多时一般使用Instruments里的来进行检测。但对于复杂情况可能就需要用到子线程监控主线程的方式来了,下面我对这些方法做些介绍。

[[183880]]

首先如果遇到应用卡顿或者因为内存占用过多时一般使用Instruments里的来进行检测。但对于复杂情况可能就需要用到子线程监控主线程的方式来了,下面我对这些方法做些介绍:

Time Profiler

可以查看多个线程里那些方法费时过多的方法。先将右侧Hide System Libraries打上勾,这样能够过滤信息。然后在Call Tree上会默认按照费时的线程进行排序,单个线程中会也会按照对应的费时方法排序,选择方法后能够通过右侧Heaviest Stack Trace里双击查看到具体的费时操作代码,从而能够有针对性的优化,而不需要在一些本来就不会怎么影响性能的地方过度优化。

Allocations

这里可以对每个动作的前后进行Generations,对比内存的增加,查看使内存增加的具体的方法和代码所在位置。具体操作是在右侧Generation Analysis里点击Mark Generation,这样会产生一个Generation,切换到其他页面或一段时间产生了另外一个事件时再点Mark Generation来产生一个新的Generation,这样反复,生成多个Generation,查看这几个Generation会看到Growth的大小,如果太大可以点进去查看相应占用较大的线程里右侧Heaviest Stack Trace里查看对应的代码块,然后进行相应的处理。

Leak

可以在上面区域的Leaks部分看到对应的时间点产生的溢出,选择后在下面区域的Statistics>Allocation Summary能够看到泄漏的对象,同样可以通过Stack Trace查看到具体对应的代码区域。

开发时需要注意如何避免一些性能问题

NSDateFormatter

通过Instruments的检测会发现创建NSDateFormatter或者设置NSDateFormatter的属性的耗时总是排在前面,如何处理这个问题呢,比较推荐的是添加属性或者创建静态变量,这样能够使得创建初始化这个次数降到***。还有就是可以直接用C,或者这个NSData的Category来解决https://github.com/samsoffes/sstoolkit/blob/master/SSToolkit/NSData%2BSSToolkitAdditions.m

UIImage

这里要主要是会影响内存的开销,需要权衡下imagedNamed和imageWithContentsOfFile,了解两者特性后,在只需要显示一次的图片用后者,这样会减少内存的消耗,但是页面显示会增加Image IO的消耗,这个需要注意下。由于imageWithContentsOfFile不缓存,所以需要在每次页面显示前加载一次,这个IO的操作也是需要考虑权衡的一个点。

页面加载

如果一个页面内容过多,view过多,这样将长页面中的需要滚动才能看到的那个部分视图内容通过开启新的线程同步的加载。

优化***加载时间

通过Time Profier可以查看到启动所占用的时间,如果太长可以通过Heaviest Stack Trace找到费时的方法进行改造。

监控卡顿的方法

还有种方法是在程序里去监控性能问题。可以先看看这个Demo,地址https://github.com/ming1016/DecoupleDemo。 这样在上线后可以通过这个程序将用户的卡顿操作记录下来,定时发到自己的服务器上,这样能够更大范围的收集性能问题。众所周知,用户层面感知的卡顿都是来自处理所有UI的主线程上,包括在主线程上进行的大计算,大量的IO操作,或者比较重的绘制工作。如何监控主线程呢,首先需要知道的是主线程和其它线程一样都是靠NSRunLoop来驱动的。可以先看看CFRunLoopRun的大概的逻辑

 

  1. int32_t __CFRunLoopRun() 
  2.  
  3.  
  4.     __CFRunLoopDoObservers(KCFRunLoopEntry); 
  5.  
  6.     do 
  7.  
  8.     { 
  9.  
  10.         __CFRunLoopDoObservers(kCFRunLoopBeforeTimers); 
  11.  
  12.         __CFRunLoopDoObservers(kCFRunLoopBeforeSources); //这里开始到kCFRunLoopBeforeWaiting之间处理时间是感知卡顿的关键地方 
  13.  
  14.   
  15.  
  16.         __CFRunLoopDoBlocks(); 
  17.  
  18.         __CFRunLoopDoSource0(); //处理UI事件 
  19.  
  20.   
  21.  
  22.         //GCD dispatch main queue 
  23.  
  24.         CheckIfExistMessagesInMainDispatchQueue(); 
  25.  
  26.   
  27.  
  28.         //休眠前 
  29.  
  30.         __CFRunLoopDoObservers(kCFRunLoopBeforeWaiting); 
  31.  
  32.   
  33.  
  34.         //等待msg 
  35.  
  36.         mach_port_t wakeUpPort = SleepAndWaitForWakingUpPorts(); 
  37.  
  38.   
  39.  
  40.         //等待中 
  41.  
  42.   
  43.  
  44.         //休眠后,唤醒 
  45.  
  46.         __CFRunLoopDoObservers(kCFRunLoopAfterWaiting); 
  47.  
  48.   
  49.  
  50.         //定时器唤醒 
  51.  
  52.         if (wakeUpPort == timerPort) 
  53.  
  54.             __CFRunLoopDoTimers(); 
  55.  
  56.   
  57.  
  58.         //异步处理 
  59.  
  60.         else if (wakeUpPort == mainDispatchQueuePort) 
  61.  
  62.             __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__() 
  63.  
  64.   
  65.  
  66.         //UI,动画 
  67.  
  68.         else 
  69.  
  70.             __CFRunLoopDoSource1(); 
  71.  
  72.   
  73.  
  74.         //确保同步 
  75.  
  76.         __CFRunLoopDoBlocks(); 
  77.  
  78.   
  79.  
  80.     } while (!stop && !timeout); 
  81.  
  82.   
  83.  
  84.     //退出RunLoop 
  85.  
  86.     __CFRunLoopDoObservers(CFRunLoopExit); 
  87.  

 

根据这个RunLoop我们能够通过CFRunLoopObserverRef来度量。用GCD里的dispatch_semaphore_t开启一个新线程,设置一个极限值和出现次数的值,然后获取主线程上在kCFRunLoopBeforeSources到kCFRunLoopBeforeWaiting再到kCFRunLoopAfterWaiting两个状态之间的超过了极限值和出现次数的场景,将堆栈dump下来,***发到服务器做收集,通过堆栈能够找到对应出问题的那个方法。

  1. static void runLoopObserverCallBack(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info) 
  2.  
  3.  
  4.     MyClass *object = (__bridge MyClass*)info; 
  5.  
  6.     object->activity = activity; 
  7.  
  8.  
  9.   
  10.  
  11. static void runLoopObserverCallBack(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info){ 
  12.  
  13.     SMLagMonitor *lagMonitor = (__bridge SMLagMonitor*)info; 
  14.  
  15.     lagMonitor->runLoopActivity = activity; 
  16.  
  17.   
  18.  
  19.     dispatch_semaphore_t semaphore = lagMonitor->dispatchSemaphore; 
  20.  
  21.     dispatch_semaphore_signal(semaphore); 
  22.  
  23.  
  24.   
  25.  
  26. - (void)endMonitor { 
  27.  
  28.     if (!runLoopObserver) { 
  29.  
  30.         return
  31.  
  32.     } 
  33.  
  34.     CFRunLoopRemoveObserver(CFRunLoopGetMain(), runLoopObserver, kCFRunLoopCommonModes); 
  35.  
  36.     CFRelease(runLoopObserver); 
  37.  
  38.     runLoopObserver = NULL
  39.  
  40.  
  41.   
  42.  
  43. - (void)beginMonitor { 
  44.  
  45.     if (runLoopObserver) { 
  46.  
  47.         return
  48.  
  49.     } 
  50.  
  51.     dispatchSemaphore = dispatch_semaphore_create(0); //Dispatch Semaphore保证同步 
  52.  
  53.     //创建一个观察者 
  54.  
  55.     CFRunLoopObserverContext context = {0,(__bridge void*)self,NULL,NULL}; 
  56.  
  57.     runLoopObserver = CFRunLoopObserverCreate(kCFAllocatorDefault, 
  58.  
  59.                                               kCFRunLoopAllActivities, 
  60.  
  61.                                               YES, 
  62.  
  63.                                               0, 
  64.  
  65.                                               &runLoopObserverCallBack, 
  66.  
  67.                                               &context); 
  68.  
  69.     //将观察者添加到主线程runloop的common模式下的观察中 
  70.  
  71.     CFRunLoopAddObserver(CFRunLoopGetMain(), runLoopObserver, kCFRunLoopCommonModes); 
  72.  
  73.   
  74.  
  75.     //创建子线程监控 
  76.  
  77.     dispatch_async(dispatch_get_global_queue(0, 0), ^{ 
  78.  
  79.         //子线程开启一个持续的loop用来进行监控 
  80.  
  81.         while (YES) { 
  82.  
  83.             long semaphoreWait = dispatch_semaphore_wait(dispatchSemaphore, dispatch_time(DISPATCH_TIME_NOW, 30*NSEC_PER_MSEC)); 
  84.  
  85.             if (semaphoreWait != 0) { 
  86.  
  87.                 if (!runLoopObserver) { 
  88.  
  89.                     timeoutCount = 0; 
  90.  
  91.                     dispatchSemaphore = 0; 
  92.  
  93.                     runLoopActivity = 0; 
  94.  
  95.                     return
  96.  
  97.                 } 
  98.  
  99.                 //两个runloop的状态,BeforeSources和AfterWaiting这两个状态区间时间能够检测到是否卡顿 
  100.  
  101.                 if (runLoopActivity == kCFRunLoopBeforeSources || runLoopActivity == kCFRunLoopAfterWaiting) { 
  102.  
  103.                     //出现三次出结果 
  104.  
  105.                     if (++timeoutCount 3) { 
  106.  
  107.                         continue
  108.  
  109.                     } 
  110.  
  111.   
  112.  
  113.                     //将堆栈信息上报服务器的代码放到这里 
  114.  
  115.   
  116.  
  117.                 } //end activity 
  118.  
  119.             }// end semaphore wait 
  120.  
  121.             timeoutCount = 0; 
  122.  
  123.         }// end while 
  124.  
  125.     }); 
  126.  
  127.   
  128.  

 

有时候造成卡顿是因为数据异常,过多,或者过大造成的,亦或者是操作的异常出现的,这样的情况可能在平时日常开发测试中难以遇到,但是在真实的特别是用户受众广的情况下会有人出现,这样这种收集卡顿的方式还是有价值的。

堆栈dump的方法

***种是直接调用系统函数获取栈信息,这种方法只能够获得简单的信息,没法配合dSYM获得具体哪行代码出了问题,类型也有限。这种方法的主要思路是signal进行错误信号的获取。代码如下

  1. static int s_fatal_signals[] = { 
  2.  
  3.     SIGABRT, 
  4.  
  5.     SIGBUS, 
  6.  
  7.     SIGFPE, 
  8.  
  9.     SIGILL, 
  10.  
  11.     SIGSEGV, 
  12.  
  13.     SIGTRAP, 
  14.  
  15.     SIGTERM, 
  16.  
  17.     SIGKILL, 
  18.  
  19. }; 
  20.  
  21.   
  22.  
  23. static int s_fatal_signal_num = sizeof(s_fatal_signals) / sizeof(s_fatal_signals[0]); 
  24.  
  25.   
  26.  
  27. void UncaughtExceptionHandler(NSException *exception) { 
  28.  
  29.     NSArray *exceptionArray = [exception callStackSymbols]; //得到当前调用栈信息 
  30.  
  31.     NSString *exceptionReason = [exception reason];       //非常重要,就是崩溃的原因 
  32.  
  33.     NSString *exceptionName = [exception name];           //异常类型 
  34.  
  35.  
  36.   
  37.  
  38. void SignalHandler(int code) 
  39.  
  40.  
  41.     NSLog(@"signal handler = %d",code); 
  42.  
  43.  
  44.   
  45.  
  46. void InitCrashReport() 
  47.  
  48.  
  49.     //系统错误信号捕获 
  50.  
  51.     for (int i = 0; i signal(s_fatal_signals[i], SignalHandler); 
  52.  
  53.     } 
  54.  
  55.   
  56.  
  57.     //oc未捕获异常的捕获 
  58.  
  59.     NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler); 
  60.  
  61.  
  62. int main(int argc, char * argv[]) { 
  63.  
  64.     @autoreleasepool { 
  65.  
  66.         InitCrashReport(); 
  67.  
  68.         return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 
  69.  
  70.     } 
  71.  

 

使用PLCrashReporter的话出的报告看起来能够定位到问题代码的具体位置了。

  1. NSData *lagData = [[[PLCrashReporter alloc] 
  2.  
  3.                                           initWithConfiguration:[[PLCrashReporterConfig alloc] initWithSignalHandlerType:PLCrashReporterSignalHandlerTypeBSD symbolicationStrategy:PLCrashReporterSymbolicationStrategyAll]] generateLiveReport]; 
  4.  
  5. PLCrashReport *lagReport = [[PLCrashReport alloc] initWithData:lagData error:NULL]; 
  6.  
  7. NSString *lagReportString = [PLCrashReportTextFormatter stringValueForCrashReport:lagReport withTextFormat:PLCrashReportTextFormatiOS]; 
  8.  
  9. //将字符串上传服务器 
  10.  
  11. NSLog(@"lag happen, detail below: 
  12.  
  13. %@",lagReportString); 

 

测试Demo里堆栈中的内容,超过了微信正文字数,所以本文省略了 

责任编辑:庞桂玉 来源: iOS大全
相关推荐

2019-09-17 09:21:01

2018-06-14 09:35:35

2012-08-22 13:00:08

2015-03-16 10:00:40

iOSARCMRC

2011-03-11 09:27:11

Java性能监控

2009-06-18 13:42:48

Hibernate s

2021-06-10 10:02:19

优化缓存性能

2018-02-06 11:10:27

iOS开发Xcode快捷键

2015-07-28 14:39:02

IOS技巧

2013-08-27 13:24:46

App Store应用上传应用截图ASO应用商店优化

2011-08-31 10:54:25

Java性能

2012-06-15 09:41:40

Linux内核

2018-02-04 22:29:21

iOS开发

2012-12-24 14:51:02

iOS

2017-05-10 14:49:52

Kotlin语言Java

2023-09-04 16:55:18

2021-04-19 17:25:08

Kubernetes组件网络

2014-05-13 09:55:13

iOS开发工具

2015-07-28 14:52:35

IOS技巧

2009-08-27 10:06:15

Scala的构造方法
点赞
收藏

51CTO技术栈公众号