In the eighth, third and second sections, I have analyzed Silverlight's related processing in terms of memory release, performance improvement and source protection. With the continuous deepening of the game tutorials, their own experience in the continuous accumulation, summary and sublimation; today, I have a deeper understanding of these 3 aspects. As a continuation of the first 3 articles, I will continue to dig deep into the Silverlight development techniques, demonstrating that the pursuit of technology is never halted.
i) Memory release
First of all, I want you to be acutely aware that Silverlight programs are managed, in addition to particularly obvious or intense needs (such as OpenFileDialog, SaveFileDialog, etc. that inherit from the IDisposable interface), Hope you do not easily intervene gc.collect (), this is the Microsoft design. NET's original intention, we can only and must do is: trust.
Second, all proficiency is required. NET development language friends once again confirm whether they have a deep understanding of the data type. In C #, for example, you should especially distinguish between value types and reference types, including their definition, composition, and principles, when writing code. Because value-type objects can be released quickly after they are assigned a value. In the case of struct (struct) and Class (Class), if inheritance is not required, a struct that uses a value type to interface will get a higher performance than class. Also, when writing inside a class, try not to use a private class object, and note that if a class is always referenced by another class, memory cannot be freed. This is often heard in conversations with other Silverlight developers: Storyboard consumes too much memory, storyboard performance is too low, storyboard too ... No words anymore. As the saying goes: Chengye, Shenya, these symptoms are the result of abuse of storyboard class. Here the "indiscriminate" contains two layers of meaning: 1, will not use, 2, excessive. To make the best of a good tool, you must be proficient in its principles and details. is storyboard accidentally put into the loop? Is there a delegate leak? Storyboard is the other object still referencing it after completed? Did storyboard forget to stop? Wait a minute. As well as DispatcherTimer, does Silverlight have to make the code more rigorous while providing a convenient tool for us to produce a variety of smooth animation effects?
Summary: Silverlight memory release finally have to look at themselves, simply say: As long as proficient enough, professional, there is no release of memory. The author is also working in this direction.
II) Performance Improvement
1 in the third section I mentioned the use of BackgroundWorker to perform relatively time-consuming operations in a background thread to reduce the burden on the Silverlight interface thread. In fact, we can also improve the flow of the screen in an asynchronous way, such as:
this.Dispatcher.BeginInvoke(() => {
//TODO…
});
Dispatcher is the core of the wpf/silverlight mechanism, and understanding it helps you write more high-performance code.
2 in animation and game development to minimize unnecessary rendering, redrawing (for example, before canvas.setleft to determine whether the Canvas.getleft is the same value); Hide the image carrier (obj) that is not in the screen. Visibility = visibility.collapsed), to cut the image, reduce the number of pictures in the form, to the same or similar pictures as much as possible to reuse, monitor the delegation of the subscription and unsubscribe (subscribe n times will perform n operations, If occurs in the cycle is extremely easy to cause the memory to leak even the program crashes, invites the beginner to realize this, and so on, although all is some details, but the effect is often very obvious.
3 Adjust the maximum fps,silverlight in the maxframerate can be dynamically adjusted by Application.Current.Host.Settings.MaxFrameRate, this technique in the game and animation design if you can apply Properly, sometimes it can make an unexpected effect.
4 There are roughly 5 kinds of methods used in Silverlight to produce animation: DispatcherTimer, Storyboard, Thread, compositiontarget.rendering, Storyboard as Trigger. 5 methods to create game Loop:which is the best? In this article, we discuss the performance and stability of the 5 methods in detail, and conclude that the personal experience is generally DispatcherTimer and storyboard occupy the position of TOP2. The following is the demo demo and its author's summary translation (the original in the demo link, only for everyone to reference, personal feeling some evaluation is not very realistic), for everyone to produce Silverlight animation to provide a better basis:
Method |
Advantages |
Disadvantages |
When is the right time to use? Recommendations |
When's the best time to stop? Use (recommended) |
DispatcherTimer |
Relatively stable |
The minimum recognition rate is approximately 15 milliseconds (personally understood as Too fast will cause a jump frame or invalid, that is, the limit is 15 Hao seconds) |
A slow moving object, Combined storyboard Control animations together |
In processing a mobile Very Fast object objects, Recommended Use Storyboard as Trigger Or CompositionTarget . Rendering increases the frame rate. |
Storyboard |
Smooth animation |
No complex motion effects, such as planets, The irregular path movement of games, etc. (This conclusion is too ... ^^ | |) |
The simple Move mode aspect |
If you want to pass more Background code to control animation, Suggest or use CompositionTarget . Rendering. (The advice is really dizzy ...) |
Thread |
Thread based, balanced per frame Run. It works better in multiple cores. |
Unstable at very short intervals (2 frames) |
Requires a lot of computing, CPU-consuming processing |
If your computer is only 1 CPUs, or need Faster animation effects, Or do you choose a different method? |
CompositionTarget . Rendering |
Very stable, guaranteed to run in place |
Unable to control the details because the frequency Is fixed, based on the screen refresh trigger, Formula: 1000/<frames-per-secspecified- In-html> [msec] |
Complex logic, as in some of the Games The logic of the loop (the exact words are not quite understood ...) |
Some trivial treatment, Like the clouds in the game Floating from left to right, it is recommended Use Storyboard instead. |
Storyboard as Trigger |
Similar to DispatcherTimer, run smoothly under 300FPS |
The same as DispatcherTimer. |
Similar DispatcherTimer |
and DispatcherTimer The same |
III) source File Protection
In the second section I analyzed several ways to protect the Silverlight source code, which is the final solution. However, with the increasingly extensive and rich application of Silverlight to enhance the user experience, we usually separate the images and other materials from the XAP compressed package, as a dynamic resource for the download on demand. However, this will directly lead to all material exposed to the temporary Internet files, the source is saved, but also leaked material resources, the results are so bad.
Solution: To download the picture, for example, at the end of the Silverlight game design Tutorial in the Nineth section I explained in detail the details of BitmapImage, Set the BitmapImage CreateOptions property to Bitmapcreateoptions.none, the picture will be divided into two copies, one in memory, one in the temporary Internet Files , and then using the block key file or folder page cache that is mentioned in the second section of the method, we will allow the user to never touch any picture footage in Silverlight, and all resource materials will remain in memory until the page closes once they are downloaded. However, there are side effects, no browser cache, every time to enter the game needs to be XAP and related resources, picture rendering has a slight delay. Of course, the increased server traffic load per 2, 300 K xap+ on-demand resources is a dime nine for the total disclosure of project resources.
The above is a personal experience in the development of Silverlight, hope to be helpful to everyone, if there is a wrong place also hope correct.