Click here to Skip to main content

Eligible Contests

Tagged as

Stats

23.4K views
16 bookmarked

Window Form Controls v/s WPF Controls Memory Comparision

10 Oct 2015CPOL4 min read
The article is about window form controls and wpf controls memory management. The difference between them and internals of how they are loaded.
   

Introduction

This is the analysis of the Windows form controls and WPF controls and how they consume the memory of the system. It dives deep into the objects being created and memory footprint consumed by both the application types.

Background

Windows forms have been there from ages to develop application on windows platform.  In 2007, WPF was introduced as an replacement to Windows Form application with new enhancements. This is the comparison of Windows Form application with a WPF application on the basis of memory utilization.

Using the code

To compare both the Application Types we would require the applications to be created with similar controls .

For this example, i have taken TextBox control. Reason for choosing TextBox controls is because in

Windows Form Application TextBox Class is inheriting from System.Windows.Forms.Control in (System.Windows.Forms.dll)

WPF Application TextBox Class is directly inheriting from System.Windows.Controls.Primitives.Control (PresentationFramework.dll)  

Due to this the TextBox control in WPF does not inherit from ContentControl thus avoiding an extra overhead of object creation.

Arguably, We could have used Windows form Label with TextBlock but lets use TextBox since almost all real world applications will use it somewhere or the other. 

To begin with we will create 1000 TextBox objects on windows form and WPF application and render them on screen. We will measure the memory utilization and check into how many objects created and memory utilized to do the same.

Lets create a a simple windows form application and add below code into it.

C#
using System.Drawing;
using System.Windows.Forms; 

namespace WindowsFormsPerformance
{
    public partial class WindowsForm : Form
    {
        private System.Windows.Forms.TextBox textbox1;
        int Iteration = 1000;

        public WindowsForm()
        {
            InitializeComponent();
            CreateMultipleTextControls();
        }

        public void CreateMultipleTextControls()
        {

            for (int i = 0; i < Iteration; i++)
            {
                this.textbox1 = new System.Windows.Forms.TextBox();
                this.textbox1.Location = new System.Drawing.Point(10, 10);
                this.textbox1.Name = "textbox" + i;
                this.textbox1.Size = new System.Drawing.Size(150, 150);
                this.textbox1.BackColor = Color.Blue;
                this.textbox1.TabIndex = 0;
                this.textbox1.Text = "textbox";
                this.Controls.Add(textbox1);
            }
        }
    }
} 

Lets create a a simple WPF application and add below code into it.

C#
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WPFWinPerformance
{
    public partial class MainWindow : Window
    {
        int Iteration = 1000;
        private TextBox textbox1;
   
        public MainWindow()
        {
            InitializeComponent();
            CreateMultipleTextControls();
        }

        public void CreateMultipleTextControls()
        {
            for (int i = 0; i < Iteration; i++)
            {
                this.textbox1 = new TextBox();
                this.textbox1.Name = "textbox" + i;
                this.textbox1.TabIndex = 0;
                this.textbox1.Text = "Text";
                this.textbox1.Width = 150;
                this.textbox1.Height = 150;
                this.textbox1.Background = new SolidColorBrush(Colors.DarkBlue);
                this.textbox1.Margin = new Thickness(10, 10, 10, 10);
                this.Grid1.Children.Add(textbox1);

            }
        }
    }
}
The results are below when you run the application using Diagnostics Tool used in Visual Studio 2015
 
For memory usage there are 2 Snapshots taken for checking the memory usage 
First Snapshot after 
C#
InitializeComponent();
Second Snapshot after 
C#
CreateMultipleTextControls();

1) Windows Application  

Windows Summary: Here in first snapshot 1247 objects were created and application takes 94.35 KB size
In second snapshot when 1000 Text Boxes were created, the rendered application had created 14327 objects and application size was 654.77 KB. 

Image 1

2) WPF Application

WPF Summary: Here in first snapshot 12702 objects were created and application takes 595.80 KB size
In second snapshot when 1000 Text Boxes were created, the rendered application had created 4,04,324 objects and  application size was 17,689.31 KB.

Image 2

So definitely, WPF application takes a lot of memory as compared to Windows Application. But that is understood that WPF gives much more flexibility than Windows in terms of look and feel of the controls also WPF renders control according to visual tree which is also an overhead. Leaving all those extra bits aside let check out what other properties are utilizing memory in the application. Digging in the objects created you can checkout the memory every object is consuming.

In the below table shows the windows forms application objects loaded in snapshot 1 and snapshot 2. It shows that windows forms on snapshot 1 hardly creates any heavy objects, the most heaviest being icon and after rendering 1000 textboxes the Textbox and PropertyStore takes maximum memory which is expected since Property Store instance created for every control in Windows Form.

Snapshot 1
Image 3
Snapshot 2
Image 4

In the below table shows the WPF application objects loaded in snapshot 1 and snapshot 2. It shows the WPF application has few objects created like single instances of all Dependency Properties in the application and other basic objects which are required for the application. But snapshot 2 is bit heavier since Textbox class internally needs ScrollViewer, Rectangle, Border, Vertical/Horizontal Scrollbar to be created, this shoots up the application size. It also has Dependency Properties being created but due to single instance of Dependency properties for same type they have a smaller footprint unlike the one in Windows form where PropertyStore got shot up when number of controls increased. 

Snapshot 1
Image 5
Snapshot 2
Image 6

Conclusion

The summary of the details:

Image 7

The above summary shows that WPF controls have much higher memory footprint than its counterpart Windows controls since WPF controls are desinged in different way and needs more smaller units initialized to create a control. But one thing to observe that WPF total load time is better than Win forms application. I am going to cover this in the next post as in why this has occured.

Windows Form CPU Load
Image 8
WPF CPU Load
Image 9

 

Coding is Simple

Image 10

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

yash soman
Technical Lead
India India
I have worked as a technical lead for MNC's. Currently, I conduct trainings on Microsoft Technologies at Ambrosia Knowledge Center. I am interested in learning new technologies,frameworks, designs etc. I like to distribute whatever knowledge i have so that more people can contribute and innovate things for our future. If you are facing any issues or problems and i could be help do feel free to connect me on my email link Send Mail.

My blog is Coding is Simple

Comments and Discussions

 
-- No messages could be retrieved (timeout) --