Silverlight memory release (performance optimization)

Source: Internet
Author: User
Tags silverlight

 

1. msdn performance prompt:

(1) The presentation Performance of the plug-in varies with the complexity of the specified host parameters and content. We recommend that youEnableframeratecounterSet property to true. Some settings in the browser status bar to display the rendered Silverlight contentFrames per second(FPS), so that you can fine-tune the application. The format of the frame rate counter is as follows:

FPS: currentframerate/maxframerate

Currentframerate is the optimal frame rate to be applied according to the conditions in the plug-in environment and the report of the plug-in when there is no frame rate. You can use the framerate initialization parameter to configure maxframerate. The default value of maxframerate is 24. Each number indicates the number of frames per second (FPS. These values are interpreted as indicating that any number lower than this value is the actually displayed frame rate.

(2) Transparent background for the Silverlight plug-in will have a great impact on the performance, so we should avoid using this function as much as possible.

(3) If you set the cachemode of the uielement, the rendering operations in rendertransform and opacity changes will be performed on the graphics processing unit (GPU) (if available ). It is important to set cachemode only for objects that are being converted or whose opacity changes are applied, because if this is not the case, the objects will not be able to benefit from the dual cache, and the performance will be slower than the cache time when you do not perform the changes. You can set the cachemode attribute for the uielement to cache the element (and its sub-elements) as a bitmap. However, this operation may reduce performance in some cases.

(4) The windowless attribute is set to true only when necessary. In windowless mode, performance is severely affected (for example, tearing in an animation ). For this reason, it is not recommended to play any media in windowless mode.

(5) If you only want to make an object visible or invisible, use the visibility attribute instead of the opacity attribute. Opacity causes high overhead because it still performs hit tests and technical presentation on the object. Setting visibility to collapsed can avoid these overhead.

(6) Avoid explicitly setting the width and height of the mediaelement object, but display the inherent size of the media element. If you want to change the display size of an element, you 'd better use the Media Encoding tool to recode it to the desired size.

(7) do not set the width and height attributes of the path object. Setting these attributes will lead to additional stretching, which will affect performance. The path object's explicitly set coordinates should be used to control its shape and position. The path will have a "natural" width and height.

(8) When the program code is running, the plug-in stops painting. Generally, If you execute a small number of tasks in the event processing program, this will not be a problem. However, if the application needs to execute a large amount of CPU resources on the programming thread, we recommend that you split the task into smaller tasks. In this way, the rendering will be able to maintain the required frame rate.

(9 ),Use stretch = "fill" to present a large number of images"You can set the stretch attribute to fill instead of other values including none to achieve better performance. When stretch = fill is used, no potential clips with additional edges are added. All types of stretching except fill require calculation layout (for example, calculation center ). Unless you want to present many images, you may not notice the difference.

The above is Microsoft's construction of processing performance. The following is a friend of Alibaba Cloud's friends. I personally think it is useful. Here I will share with you:

1) use lightweight controls whenever possible

This is extremely evident in WPF. The three controls, formattedtext, textblock, and label, are the most memorable ones. The performance consumption from left to right is more, A friend who is often used to arbitrary drag-and-control is dragging a familiar label when he first comes into contact with WPF. formattedtext will not be mentioned, and textblock has almost the same functions as label, therefore, if you have read this article, you should try to select a lightweight control in WPF development. As Silverlight is a subset of WPF, almost all its controls are optimized and optimized. Therefore, you only need to pay special attention to the choice of layout controls; canvas is a layout control with good performance and ease of use. It is strongly recommended in this article.

2) do not touch the opacity attribute of an object during animation.

For example, if an object moves horizontally, you add a mouse hover event to it, and the event content changes its opacity attribute to translucent, however, when you run the program, you will find that when you move the mouse over the moving object, it will cause the movement of the object to be temporarily insmooth (similar to frame jumping ), this is because directly modifying the opacity attribute of an element forces the system to create a temporary surface. This process consumes a certain amount of performance. Remember.

3) use the compositiontarget. Rendering event to start a frame-by-frame animation (loop)

There are multiple ways to implement animation. If you need to implement a high-density screen-based animation, we recommend that you use the compositiontarget. Rendering event. Note that the following is the key. In Silverlight, You need to strictly limit the maxframerate parameter. It can adjust the image update rate based on the customer's computer configuration and time-to-time performance consumption, it is equivalent to your animation machine nanny. My personal suggestion is to set maxframerate in the range of 45-60, so that it is closest to the refreshing rate of your LCD display (some people say that setting it to 1000 will make the animation extremely smooth; yes, your goal is to achieve it, but it will lead to your CPU crazy operation, I think this is not the result you want), in the busy hours will not overconsume performance, it also gives you a clear and smooth visual experience.

4) Try to use various acceleration and caching Methods

In WPF, you can reduce the image quality in exchange for performance improvement; enablegpuacceleration can be started in Silverlight and then GPU acceleration is supported by setting cachemode = "bitmapcache" for each uelement object (this method is currently in version 3.0 and is highly controversial ).

The WPF/Silverlight application uses a large number of graphics, media, and animations to give users a better interactive experience. Currently, the 1 GB memory configured on the PC is low-end, therefore, you may consider using space for time to cache these resources as much as possible. Each call first retrieves whether the resources exist in the cache before reloading. This provides a fast and reliable channel for frequent Resource Material switching, consuming only a little bit of idle memory.

5) when appropriate, using association attributes will surprise you.

Dependencyobject is the most efficient task, whether it is WPF or Silverlight. It defines the associated attributes with the callback method and notifies the interface only when the associated attributes are modified, this is the best way to use associated attributes. However, the overall performance will be greatly reduced if you use the associated attributes improperly or excessively. In fact, some common object attributes can be replaced by CLR attributes, this is to extract the performance principle from the details.

6) Think of backgroundworker as your right arm

Backgroundworker is a great control that exists in the winform age. It works with the dispatcher on the interface to perform cross-thread background asynchronous delegation, which is a processing method with both performance and friendly experience. You may consider the backgroundworker as your right arm when dealing with a huge amount of data. You will find that the process is as simple as that.

 

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.