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

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

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

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

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

 

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

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

 

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

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

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

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

性能调优的步骤:

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通信  监测 股票金融的引用,对实时信息要求较高的。我们就不要缓存,在提高数据通信质量上,做优化。

 

posted @ 2016-02-14 13:40  特洛伊-Micro  阅读(2974)  评论(0编辑  收藏  举报