Click here to Skip to main content
15,038,642 members
Articles / Desktop Programming / System
Article
Posted 10 Oct 2015

Tagged as

Stats

23.7K views
16 bookmarked

Window Form Controls v/s WPF Controls Memory Comparision

Rate me:
Please Sign up or sign in to vote.
4.96/5 (26 votes)
10 Oct 2015CPOL4 min read
WinForm controls and WPF controls memory management and internals of how they are loaded
The article is about Windows 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 application types.

Background

Windows Forms have been around since ages to develop applications 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. The 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 let's 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 are created and memory utilized to do the same.

Let's create a a simple Windows Form application and add the 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);
            }
        }
    }
} 

Let's create a a simple WPF application and add the 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 two snapshots taken for checking the memory usage.

First snapshot after:

C#
InitializeComponent();

Second snapshot after:

C#
CreateMultipleTextControls();

1) Windows Application

Windows Summary: Here, in the first snapshot, 1247 objects were created and the application takes 94.35 KB size.

In the 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 the first snapshot, 12702 objects were created and application takes 595.80 KB size.

In the 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 up 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 below images show 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 heaviest being icon and after rendering 1000 textboxes, the Textbox and PropertyStore take maximum memory which is expected since Property Store instance created for every control in Windows Form.

Image 3

Snapshot 1

Image 4

Snapshot 2

The below images show 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 a 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.

Image 5

Snapshot 1

Image 6

Snapshot 2

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 designed in a different way and need more smaller units initialized to create a control. But one thing to observe is that WPF total load time is better than Win forms application. I am going to cover this in the next post as to why this has occurred.

Image 8

Windows Form CPU Load

Image 9

WPF CPU Load

Coding is Simple

Image 10

History

  • 10th October, 2015: Initial version

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

 
PraiseGood analysis, Thank you. My rating is 5 out of 5 Pin
VR Karthikeyan7-Sep-16 18:28
professionalVR Karthikeyan7-Sep-16 18:28 
Praisevery nice 5+ Pin
RhishikeshLathe28-Oct-15 5:46
professionalRhishikeshLathe28-Oct-15 5:46 
GeneralRe: very nice 5+ Pin
yash soman28-Oct-15 8:02
professionalyash soman28-Oct-15 8:02 
QuestionSuperb 5 Pin
Shivprasad koirala28-Oct-15 2:30
MemberShivprasad koirala28-Oct-15 2:30 
AnswerRe: Superb 5 Pin
yash soman28-Oct-15 8:03
professionalyash soman28-Oct-15 8:03 
QuestionTypo Pin
FantasticFiasco25-Oct-15 21:16
MemberFantasticFiasco25-Oct-15 21:16 
AnswerRe: Typo Pin
yash soman25-Oct-15 21:43
professionalyash soman25-Oct-15 21:43 
QuestionHave you consider to post this as a tip? Pin
Nelek22-Oct-15 23:15
protectorNelek22-Oct-15 23:15 
GeneralMy vote of 5 Pin
Andres Cassagnes16-Oct-15 10:26
MemberAndres Cassagnes16-Oct-15 10:26 
GeneralRe: My vote of 5 Pin
yash soman17-Oct-15 1:48
professionalyash soman17-Oct-15 1:48 
QuestionPhoto Pin
Herilane12-Oct-15 15:57
professionalHerilane12-Oct-15 15:57 
AnswerRe: Photo Pin
yash soman12-Oct-15 18:24
professionalyash soman12-Oct-15 18:24 
QuestionNice one. +5 from me Pin
Praveen Kumar Katiyar11-Oct-15 21:22
professionalPraveen Kumar Katiyar11-Oct-15 21:22 
AnswerRe: Nice one. +5 from me Pin
yash soman12-Oct-15 4:48
professionalyash soman12-Oct-15 4:48 
GeneralMy vote of 5 Pin
Santhakumar M11-Oct-15 7:04
professionalSanthakumar M11-Oct-15 7:04 
GeneralRe: My vote of 5 Pin
yash soman11-Oct-15 7:16
professionalyash soman11-Oct-15 7:16 
GeneralMy vote of 5 Pin
Steffen Ploetz11-Oct-15 2:12
professionalSteffen Ploetz11-Oct-15 2:12 
GeneralRe: My vote of 5 Pin
yash soman11-Oct-15 2:22
professionalyash soman11-Oct-15 2:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.