Windows Form Controls V/S WPF Controls Memory Comparision

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 a 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.

Let's create a a simple windows form application and add the following code into it. 

  1. using System.Drawing;  
  2. using System.Windows.Forms;  
  3.   
  4. namespace WindowsFormsPerformance  
  5. {  
  6.     public partial class WindowsForm : Form  
  7.     {  
  8.   
  9.          
  10.         private System.Windows.Forms.TextBox textbox1;  
  11.         int Iteration = 1000;  
  12.   
  13.         public WindowsForm()  
  14.         {  
  15.             InitializeComponent();  
  16.             CreateMultipleTextControls();  
  17.         }  
  18.   
  19.         public void CreateMultipleTextControls()  
  20.         {  
  21.   
  22.             for (int i = 0; i < Iteration; i++)  
  23.             {  
  24.                 this.textbox1 = new System.Windows.Forms.TextBox();  
  25.                 this.textbox1.Location = new System.Drawing.Point(10, 10);  
  26.                 this.textbox1.Name = "textbox" + i;  
  27.                 this.textbox1.Size = new System.Drawing.Size(150, 150);  
  28.                 this.textbox1.BackColor = Color.Blue;  
  29.                 this.textbox1.TabIndex = 0;  
  30.                 this.textbox1.Text = "textbox";  
  31.                 this.Controls.Add(textbox1);  
  32.             }  
  33.   
  34.   
  35.   
  36.         }  
  37.   
  38.   
  39.     }  
  40. }  
Let's create a simple WPF application and add the following code into it. 
  1. using System.Windows;  
  2. using System.Windows.Controls;  
  3. using System.Windows.Media;  
  4.   
  5. namespace WPFWinPerformance  
  6. {  
  7.     /// <summary>  
  8.     /// Interaction logic for MainWindow.xaml  
  9.     /// </summary>  
  10.     public partial class MainWindow : Window  
  11.     {  
  12.         int Iteration = 1000;  
  13.         private TextBox textbox1;  
  14.      
  15.         public MainWindow()  
  16.         {  
  17.             InitializeComponent()             
  18.             CreateMultipleTextControls();  
  19.         }  
  20.   
  21.         public void CreateMultipleTextControls()  
  22.         {  
  23.             for (int i = 0; i < Iteration; i++)  
  24.             {  
  25.                 this.textbox1 = new TextBox();  
  26.                 this.textbox1.Name = "label" + i;  
  27.                 this.textbox1.TabIndex = 0;  
  28.                 this.textbox1.Text = "Text";  
  29.                 this.textbox1.Width = 150;  
  30.                 this.textbox1.Height = 150;  
  31.                 this.textbox1.Background = new SolidColorBrush(Colors.DarkBlue);  
  32.                 this.textbox1.Margin = new Thickness(10, 10, 10, 10);  
  33.                 this.Grid1.Children.Add(textbox1);  
  34.   
  35.             }  
  36.   
  37.         }  
  38.   
  39.     }  
  40. }   
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
  1. InitializeComponent();    
Second Snapshot after 
  1. 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.


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.


So definitely, WPF application takes a lot of memory as compared to Windows Application. But it 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's 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.

The following 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

Object type

Snapshot 2

table

The following 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 a 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

see detail

Snapshot 2

see table

Conclusion

The summary of the details:

 

The above summary shows that WPF controls have much higher memory footprint than its counterpart Windows controls since WPF controls are designed in a 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 occurred.


Windows Form CPU Load

Windows Form CPU Load

WPF CPU Load

Coding is Simple


Up Next
    Ebook Download
    View all
    Learn
    View all