Ask a questionAsk a question
 

AnswerTextBox: Major memory leak in WPF

  • Monday, February 11, 2008 11:39 AMPeter Huber Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Andre from Microsoft wrote in December 2007:

    We recently found out about a couple memory leaks.

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2511874&SiteID=1&pageid=0

     

    Sadly, there is another, major one: The WPF Textbox is leaking memory everytime one changes the Text property.

     

    You can try it like this:

     

    Put a textbox into a WPF window

    Continue to change the TEXT property

    See in the perfomancemonitor how the memory consumption grows and grows and grows ...

     

    Here is the code:

     

    Code Snippet

    <Window x:Class="WpfApplication1.Window1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Window1" Height="Auto" Width="Auto"

        SizeToContent="WidthAndHeight" WindowStyle="ToolWindow">

      <StackPanel Orientation="Vertical">

        <Label>TextBox</< FONT>Label>

        <TextBox x:Name="textBox"/>

    </< FONT>  </STACKPANEL< FONT>>

    </< FONT></WINDOW< FONT>>

     

     

     

     

    Code Snippet

    using System.Windows;

     

    namespace WpfApplication1 {

     

      public partial class Window1: Window {

     

        public Window1() {

          InitializeComponent();

          this.Loaded += Window1_Loaded;

        }

     

        void Window1_Loaded(object sender, RoutedEventArgs e) {

          bool isToggle = false;

          while (true) {

            if (isToggle) {

              textBox.Text = "a";

              isToggle = false;

            } else {

              textBox.Text = "b";

              isToggle = true;

            }

          }

        }

      }

    }

     

    To prove that the problem is caused by the Text property, change the above code like this

     

    Code Snippet

            if (isToggle) {

              textBox.Text = "a";

              isToggle = false;

            } else {

              textBox.Text = "a";

              isToggle = true;

            }

     

    The performance monitor shows now that there is no memory increase anymore.

     

    Comment

    If only .NET and Visual Studio would provide better tools to test and analyze memory leaks, I am sure Microsoft would create fewer of them. Somehow Microsoft seems to think that Garbage Collection is a miracle weapon and makes it behave like a black box. But unfortunately, we developers have plenty of possibilities to leak memory, but only stone age tools to investigate them.

Answers

  • Wednesday, February 13, 2008 1:43 AMLesterLobo - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    i could repro this issue.. we are tracking this as a bug..

     

    Thanks for reporting.

     

     

    replied on the wrong thread

  • Wednesday, February 13, 2008 7:33 PMIfeanyi EcheruoMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    I've confirmed this is the designed behavior of TextBox.

    We allocate for the undo stack whenever text changes.

    One of the ways this behavior can be turned of is to set the TextBox.UndoLimit to a low number;

     

    --Ifeanyi Echeruo [MSFT]

All Replies

  • Monday, February 11, 2008 11:51 AMJeremiah Morrill Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Sounds like a good opportunity to try stepping into the .NET source code...

     

    http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx

     

    -Jer

  • Wednesday, February 13, 2008 1:16 AMPeter Huber Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I wonder if anyone can confirm my findings ? I can hardly believe that Microsoft didn't notice such a fundamental memory leak. On the other hand, my test code is trivial and the memory consumption goes straight through the roof.

     

  • Wednesday, February 13, 2008 1:35 AMIfeanyi EcheruoMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I tested your code with a minor modification

    textBox.UndoLimit = 0;

     

    I think the leak was TextBox building an undo stack for all the text changes.

     

    Code below

     

    Code Snippet

    namespace WpfApplication1 {
        using System;
        using System.Windows;
        using System.Windows.Controls;
     
        public class Program {
            static TextBox textBox;
            [STAThread]
            static void Main() {
                Application app = new Application();
                Window w = new Window();
                    textBox = new TextBox();

                    textBox.UndoLimit = 0;
                w.Loaded += Window1_Loaded;
                w.Content = textBox;
                w.Show();
                app.Run();
            }
           
            static void Window1_Loaded(object sender, RoutedEventArgs e) {
                bool isToggle = false;
                while (true) {
                    if (isToggle) {
                        textBox.Text = "a";
                        isToggle = false;
                    } else {
                        textBox.Text = "b";
                        isToggle = true;
                    }
                }
            }
        }
    }

     

     

    --Ifeanyi Echeruo [MSFT]

  • Wednesday, February 13, 2008 1:43 AMLesterLobo - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    i could repro this issue.. we are tracking this as a bug..

     

    Thanks for reporting.

     

     

    replied on the wrong thread

  • Wednesday, February 13, 2008 7:33 PMIfeanyi EcheruoMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    I've confirmed this is the designed behavior of TextBox.

    We allocate for the undo stack whenever text changes.

    One of the ways this behavior can be turned of is to set the TextBox.UndoLimit to a low number;

     

    --Ifeanyi Echeruo [MSFT]

  • Friday, February 15, 2008 12:49 AMPeter Huber Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    True, to use an unlimited amount of memory is by design, as the help says:

     

    TextBoxBase.UndoLimit Property:The number of actions stored in the undo queue. The default is –1, which means the undo queue is limited to the memory that is available.

     

    But I wonder how many developers are aware of that behavior. In our case, the application is supposed to run for a year or longer and we update the textbox several times per second to display measurement results. To us, the behavior of the textbox with default properties certainly looked like a memory leak.

     

    Thanks for the fast support.

  • Friday, February 15, 2008 6:39 PMIfeanyi EcheruoMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Your scenario is one we expected to be addressed by TextBlock or Label.

    TextBox pulls in extra editing services like IME, spell checking and undo.

     

    Is there a specific reason you are using TextBox instead?

     

    --Ifeanyi Echeruo [MSFT]

  • Saturday, February 16, 2008 3:10 AMPeter Huber Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    When we realised that there is a "memory leak" in the text box, but we didn't know how to stop it, we replaced the textboxes with labels. But the textbox has some advantages, like when they stop the measurement, they can select the text in the text box and copy it, which is not possible with a label.

     

    Actually, we face this situation quiet often, that we display something to the user, like an error message. We don't want him to change what we display, but he should be able to select the text and copy it. But most of the time Windows Forms and WPF don't allow the user to select and copy text from the GUI, as opposed to a Web-Page.

     

    How many times did I hava to retype Windows OS error messages just to get support.

     

  • Saturday, February 16, 2008 9:31 AMMarlon GrechMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    This is really interesting.... I created a blog post for it

    http://marlongrech.wordpress.com/2008/02/16/wpf-textbox-memory-leak/

  • Friday, February 29, 2008 10:44 PMKofoed Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    How can I (or what tool can I use) to track or measure performance like this using VS 2008?

     

  • Friday, February 29, 2008 11:18 PMIfeanyi EcheruoMSFTUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    There are performance tools that come with some SKU's of Visual Studio

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=339481&SiteID=1

    Has a bunch of links to get you started.

     

    There are a couple more standalone tools from MS

    http://search.live.com/results.aspx?q=clrprofiler

     

    There are also a few very good 3rd party tools

    http://search.live.com/results.aspx?q=.net+memory+profiler

    http://search.live.com/results.aspx?q=.net+performance+tools

     

    --Ifeanyi Echeruo [MSFT]

  • Saturday, March 01, 2008 5:23 AMAtul GuptaMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    my two cents. Would be good to set this in application resources - http://infosysblogs.com/microsoft/2008/03/wpf_textbox_memory_leak_issue_1.html
  • Friday, October 03, 2008 9:46 PMjofrpl Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     atul's solution will not work... local style will override style's from app.resources
  • Monday, March 30, 2009 2:23 PMavitheblender Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    to jofrpl:atul's solution works.