WPF optimization Experience & lt; 1 & gt;: wpf optimization experience

Source: Internet
Author: User

WPF optimization experience <1>: wpf optimization experience

Recently, we have reconstructed a Winform project that has been developed and maintained for five years. Considering the recent popularity of the user experience, we have adopted the WPF technology, we hope to have a brand new effect in terms of appearance and experience.

In the past, when using Winform, the memory was well controlled, and the software was about 100 M after one day. Now, using WPF, the memory reached M in ten minutes, while feeling the appetite of WPF, I am also thinking about how to reduce the memory usage.

First, write a test project and put two forms. Put a DataGrid In the first form, bind 200 pieces of data to it, and put two test buttons in the second form. click the button and click the code below.

// Open 20 test windows: List <TestWindow> windows = new List <TestWindow> (); private void button#click (object sender, RoutedEventArgs e) {for (int I = 0; I <20; I ++) {var window = new TestWindow (); windows. add (window); window. show () ;}// close the currently opened test window private void Button2_Click (object sender, RoutedEventArgs e) {foreach (var item in windows) {item. close () ;}// call the Garbage Collector to forcibly recycle available memory private void Button3_Click (object sender, RoutedEventArgs e) {GC. collect ();}

Now you are ready to start the test:

Click Button1 to open 20 test windows. The memory size reaches 126 mb. Click Button2 to close all test forms. The memory size is 110 MB, and 20 test forms are opened again. The memory size is 205 MB, close all forms, memory 185 M, garbage collection, memory 139 M

Slightly changed the program. When the test form is closed, the data context binding (DataContext) value is null to continue the test.

Click Button1 to open 20 test windows. The memory size reaches 123 mb. Click Button2 to close all test forms. The memory size is 110 MB, and 20 test forms are opened again. The memory size is 136 MB, close all forms, 120 MB memory, garbage collection, 55 MB memory

After multiple tests, the results are basically the same. To make a more intuitive comparison of the data, the data list is as follows:

  Program running Open 20 test forms Close 20 test forms Open 20 test forms again Close 20 test forms again Forced garbage collection
No processing is performed when the form is closed. 17 M 126 M 110 M 205 M 185 M 139 M
Assign a null value to DataContext when closing the form 17 M 123 M 110 M 136 M 120 M 55 M

So,There is only one conclusion. When we use MVVM mode to bind DataContext or directly bind a data source to the list control, it is best to assign a null value to the binding attribute when closing the form.

        protected override void OnClosed(EventArgs e)        {            base.OnClosed(e);            this.DataContext = null;        }

 

In the future, we will continue our blog posts and discuss with you the self-experienced WPF optimization solution.


What are the improvements to controls in WPF 4?

These new controls are 99% compatible with their corresponding controls in Silverlight in terms of API and behavior, allowing developers to reuse code between Silverlight and WPF implementations while optimizing the workflow, create a consistent experience across the client and web. It includes Windows 7 and Office skins, as well as all the standard Ribbon features familiar to end users, including tab and group, dynamically changing the size

How does WPF improve 3D rendering?

I. Graphic hardware acceleration
In wpf, graphics rendering is performed through the GPU of the graphics card. Generally, the WPF system defines three rendering layers: The rendering layer 0 and no graphic hardware acceleration. The DirectX version is lower than 7.0, and the rendering layer 1 is accelerated by some graphics hardware. The DirectX version level is higher than or equal to 7.0 and lower than 9.0. The video RAM must be greater than or equal to 30 MB. The number of multi-Texture unit units must be greater than or equal to 2 (graphics card AGP rate 2X );
Rendering Layer 2 most graphics Functions use graphical hardware acceleration. The DirectX version level is higher than or equal to 9.0. The video RAM must be greater than or equal to 120 MB. The number of multi-Texture unit units must be greater than or equal to 4 (8X, 16X ).
Ii. Use of 3D Model Materials
Generally, the use of 3D model materials usually uses a Brush, but the rendering speed of different Brush materials is also different. SolidColorBrush (directly add a solid color to the Model) linearGradientBrush is much faster than ImageBrush and VisualBrush. cached brushes are faster than non-cached brushes.
Iii. Model Creation
In 3D scenarios, we try to create less than 60.000 GeometryModel3D triangles, less than 20.000 vertices, and try to reuse models with little shape changes unless a new shape needs to be created, try to create a submodel in a viewport3D instead of creating multiple viewport3D.
4. anti-aliasing in 3d
3d anti-aliasing in wpf is only applicable to the vista system. If you want to implement anti-aliasing in the XP system, you can set the maximum multi-level sampling value HKEY_CURRENT_USER/SOFTWARE/Microsoft/aveon in the registry. the Graphics/MaxMultisampleType value type is DWORD. Generally, it is set to 4, which can be adjusted based on the performance of the video card. (However, it is recommended that you do not use this method. You can use the modified registry to implement anti-aliasing, but it may also cause new rendering problems, such: the 3D model is considerate and overlaps with other objects to generate a dotted line on the graphic edge. However, the dotted line problem can be solved by setting the pixel center instead of the graphics card, but it will also lead to other unpredictable unstable factors)
Therefore, you can flexibly choose the optimal setting method based on your actual presentation requirements for 3D rendering. The goal is to ensure stable, excellent performance, and good 3D rendering effect.

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.