"Xamarin Poach Series: Performance Tuning for Applications"

Source: Internet
Author: User

Official tools: Web address: HTTP://PAN.BAIDU.COM/S/1PKGRSRP

Official: Https://download.xamarin.com/profiler/profiler-windows.msi

Xamarin Profiler, using this tool, helps us to tune software performance to find the bottleneck of the application.

A high memory-intensive code call is monitored. Quickly resolve code that affects program performance.

For the use of this tool, see:

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

A few suggestions for improving program performance:

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

A bad application, will be reflected in many aspects, such as: pull-down scrolling, high CPU consumption, battery consumption, high memory consumption and so on, resulting in unfriendly user interaction.

To improve the user experience, you must perform performance tuning for the programs you develop.

Performance Tuning steps:

1 using monitoring tools to identify areas of high memory footprint

2 using a real-machine test instead of an emulator

3 real-machine testing with different configurations of different models

4 before testing, close other applications to reduce unnecessary interference

Optimization Recommendations:

2 effects

IDisposable

interface for releasing unmanaged resources, IO streams, HTTP network streams, database read and write streams, and so on. Use the using () {} code block as much as possible using an instance of the class that implements this interface.

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 in the Try catch code block, be sure to release the resource that implements the IDisposable

3 unsubscribe from the event. We generally subscribe to the add operation, when used, minimize a listener multiple subscriptions, reduce the multicast delegate trigger calls.

As in the following code:

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;  }}

At the time of release, the subscription to the event is unsubscribed.

4 Note: The climax is coming.

In managed code, try not to use the nested nesting of classes of different types. Once nested, this interdependent instance is not reclaimed by the GC unless the two interdependent instances are forcibly destroyed. For example, the following code:

Class A relies on Class B, and in the same way, B has a. Once this nesting is formed, it is impossible for the GC to assume that two instances are not referenced and cannot be destroyed.

5 for time-consuming operations, use asynchronous methods to prevent blocking the UI. For this type of operation, use the Task async await with. NET 4.0 to encapsulate the asynchronous invocation.

6 using GC for forced garbage collection, Xamarin provides two types of garbage collector.

    • SGen –this is a generational garbage collector and are the default garbage collector on the Xamarin platform.
    • Boehm –this is a conservative, non-generational garbage collector. It's The default garbage collector used for Xamarin.ios applications, the use the Classic API.

SGen's garbage collection performance is very objective.

7 reducing the volume of application packages

Use Xamarin's link technology.

The linker provides three different settings to control its behavior:

    • Don ' t Link –no unused types and methods would be a removed by the linker. For performance reasons, the "the default setting for debug builds.
    • Link Framework sdks/sdk assemblies only –this setting would only reduce the size of those assemblies that is Shi Pped by Xamarin. User code would be unaffected.
    • Link All Assemblies –this are a more aggressive optimization that would target the SDK assemblies and user code. For bindings this would remove unused backing fields and make each instance (or bound objects) lighter, consuming less memo Ry.

Manual comparison, found that the link after the DLL volume than non-link to reduce a lot. The Xamarin team implements actions that remove program-agnostic APIs. Greatly reduces the volume of the package.

8 Optimizing Picture Resources

Because the picture resource is to be loaded into the memory, too much picture resources, may lead to high CPU consumption, memory is full, image and other materials compression processing, in the case of no distortion, no loss of resolution, the picture resource compression.

When you close the window, release the image resource.

9 reduce initialization of excessive resources when the program loads

Generally when the program starts, we turn on a transition animation effect, prompting the user program to start. When initialization is complete, enter the program. But not too time-consuming initialization, the feeling is the program suspended animation.

So when the program is active, if we load some local resources text or binary data or XML, and so on, as far as possible to load the key data is complete. Some network operations, and so on, if not necessary, do not proceed.

10 Reducing program client communication with the network

Some static data blocks, such as the questions in the category examination of the test Word library offline map packets and so on. If the update is not upgraded, these blocks are not updated frequently after the first load to the client. Every time you go to the network to retrieve, whether there are new packets, if any, prompting the user to update.

We can package this data into a specific text file XML file JSON data file in the DB file dat binary data file.

Caching these blocks as much as possible on the client side can greatly improve the user experience. Instead of having to download a large number of packets every time from the server side.

Of course, for some online video IM communication to monitor the stock financial reference, the real-time information requirements are higher. We do not cache, in improving the quality of data communication, do optimization.

"Xamarin Poach Series: Performance Tuning for Applications"

Related Article

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.