【Xamarin挖墙脚系列:应用的性能调优】

官方提供的工具:网盘地址:http://pan.baidu.com/s/1pKgrsrp数据库

官方下载地址:https://download.xamarin.com/profiler/profiler-windows.msijson

Xamarin Profiler,使用此工具,帮助咱们进行软件性能的调优,找到应用的瓶颈。windows

内存占用较高的代码调用进行监视。快速解决影响程序性能的代码。缓存

 

关于此工具的使用,请参见:服务器

https://developer.xamarin.com/guides/cross-platform/deployment,_testing,_and_metrics/网络

 

关于程序性能提高的几条建议:app

官方文档:https://developer.xamarin.com/guides/cross-platform/deployment,_testing,_and_metrics/memory_perf_best_practices/less

一个差劲儿的应用,会体如今不少方面,如:下拉滚动卡顿,CPU占用极高,耗费电池电量,内存占用很高等等,从而形成的用户交互不友好。异步

为了提高用户体验,必须对开发的程序进行性能调优。async

性能调优的步骤:

1 使用监视工具找出内存占用较高的代码区域

2 使用真机测试,而不是用模拟器

3 使用多种不一样型号 不一样配置的真机测试

4 测试前,关闭其余应用,减小没必要要的干扰

 

优化建议:

1 使用

IDisposable 

接口,进行释放非托管的资源,IO流,HTTP网络流,数据库读写流等。使用实现了此接口的类的实例的操做,尽可能使用Using (){}代码块。

public void ReadText (string filename)
{
  ...
  string text;
  using (StreamReader reader = new StreamReader (filename)) {
    text = reader.ReadToEnd ();
  }
  ...
}public void ReadText (string filename)
{
  ...
  string text;
  using (StreamReader reader = new StreamReader (filename)) {
    text = reader.ReadToEnd ();
  }
  ...
}

  

2 在try catch 代码块中,必定要对实现了IDisposable的资源进行释放

3 对事件进行退订操做。咱们通常对实现  进行 订阅 add 操做,在使用的时候 ,尽可能减小一个监听的屡次订阅,减小多播委托的触发调用。

以下面的代码:

public class Publisher
{
  public event EventHandler MyEvent;

  public void OnMyEventFires ()
  {
    if (MyEvent != null) {
      MyEvent (this, EventArgs.Empty);
    }
  }
}

public class Subscriber : IDisposable
{
  readonly Publisher publisher;

  public Subscriber (Publisher publish)
  {
    publisher = publish;
    publisher.MyEvent += OnMyEventFires;
  }

  void OnMyEventFires (object sender, EventArgs e)
  {
    Debug.WriteLine ("The publisher notified the subscriber of an event");
  }

  public void Dispose ()
  {
    publisher.MyEvent -= OnMyEventFires;
  }
}

  在释放的时候,就退订了事件的订阅。

 

 

4 注意:高潮来了

在托管代码中,尽可能不要使用不一样类型的类的相互嵌套。一旦出现嵌套,那么除非强制销毁两个相互依赖的实例,不然,这相互依赖的实例不会被GC回收掉。好比如下的代码:

 

class  A 依赖class B ,一样,B中有A。这种嵌套一旦造成,就没法让GC觉得两个实例是没有被引用,从而没法销毁。

5 对耗时操做,使用异步方法,防止阻塞UI。对这类操做,使用.net 4.0后的 Task async await 搭配,封装异步的调用。

 

6   使用GC进行强制垃圾回收,Xamarin提供了两种垃圾回收器。

 

  • SGen – This is a generational garbage collector and is the default garbage collector on the Xamarin platform.
  • Boehm – This is a conservative, non-generational garbage collector. It is the default garbage collector used for Xamarin.iOS applications that use the Classic API.

Sgen的垃圾回收性能效果是很客观的。

 

7 减小应用程序包的体积

使用Xamarin的 Link技术。

The linker provides three different settings to control its behavior:

  • Don’t Link – No unused types and methods will be removed by the linker. For performance reasons, this is the default setting for debug builds.
  • Link Framework SDKs/SDK Assemblies Only – This setting will only reduce the size of those assemblies that are shipped by Xamarin. User code will be unaffected.
  • Link All Assemblies – This is a more aggressive optimization that will target the SDK assemblies and user code. For bindings this will remove unused backing fields and make each instance (or bound objects) lighter, consuming less memory.

本身手工对比,发现,Link后的dll体积比非Link的减小许多。Xamarin团队,实现了将跟程序无关的API进行移除的操做。大大减小了程序包的体积。

8 优化图片资源

由于图片资源是要被加载进入内存中的,过多的图片资源,可能致使CPU占用较高,内存爆满,对图片等素材进行压缩处理,在不失真,不损失分辨率的状况下,对图片资源进行压缩。

在关闭窗口的时候,释放掉图片资源。

 

9 减小在程序加载的时候初始化过多的资源

通常在程序启动的时候,咱们开启一个过渡动画效果,提示用户程序启动中。等初始化完毕后,进入程序。可是不要过于耗时的初始化,感受是程序假死。

因此在程序处于激活的时候,咱们若是加载一些本地资源 文本 或者二进制数据 或者xml等,尽可能把关键数据加载完毕,便可。一些网络操做等,若是没有必要,就不要进行。

10 减小程序客户端跟网络的通讯

一些静态数据块,好比类别 科目 考试的试题  单词库 离线地图数据包等等。若是不是升级更新,那么这些数据块在第一次加载到客户端后,不用常常更新。每次去网络检索下,是否有新的数据包,有的话,提示用户更新。

咱们能够将这些数据  打包成特定的文本文件 xml文件 json数据文件  db 文件 dat二进制数据文件。

在客户端尽量多的缓存这些数据块,会极大的提升用户体验。而不是每次都要从服务器端,下载大量的数据包。

固然,针对一些在线视频 IM通讯  监测 股票金融的引用,对实时信息要求较高的。咱们就不要缓存,在提升数据通讯质量上,作优化。

相关文章
相关标签/搜索