WPF textbox creates a memory leak

Source: Internet
Author: User

The previous time was involved in a WPF-written project in which there was a scenario in which a large amount of text information was dynamically generated during the program's operation and displayed in the text of a TextBox appended to the WPF interface. After writing, the program that runs the project finds that after generating a lot of information, the system slows down and the task Manager is opened to discover that the program takes up nearly 1.5G of memory (oh, my god!!! This is not a normal consumption of memory

  

The previous time was involved in a WPF-written project in which there was a scenario in which a large amount of text information was dynamically generated during the program's operation and displayed in the text of a TextBox appended to the WPF interface. After writing, the program that runs the project finds that after generating a lot of information, the system slows down and the task Manager is opened to discover that the program takes up nearly 1.5G of memory (oh, my god!!! This is not the usual memory-consuming AH!!!). It was later discovered through data and exploration that WPF's TextBox causes a memory leak when appending text to the display. Below is a small demo program to show this memory leak.

My demo program is simple, which is to display a TextBox and a button on the interface, click on the button and then make a for loop from 0 to 9999 and display the text of the textbox appended with those numbers. Code as follows,

<window x:class= "Textboxmemoryleak.mainwindow"        xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/ Presentation "        xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "        title=" test textbox memory Leak "height=" 350 " Width= "525"        windowstartuplocation= "Centerscreen" >    <grid margin= "5" >        < grid.rowdefinitions>            <rowdefinition height= "*"/>            <rowdefinition height= "/> </"        grid.rowdefinitions>        <dockpanel grid.row= "0" >            <textbox name= "Tboutput" isreadonly= "True" verticalscrollbarvisibility= "Auto"/>        </DockPanel>        <stackpanel grid.row= "1"                    flowdirection= "RightToLeft"                    orientation= "Horizontal" >            <button name= "btnstart" content= "Start" Margin = "5,4,5,4" width= "click=" btnStart_Click "/>        </StackPanel>    </Grid></Window>

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Windows;
Using System.Windows.Controls;
Using System.Windows.Data;
Using System.Windows.Documents;
Using System.Windows.Input;
Using System.Windows.Media;
Using System.Windows.Media.Imaging;
Using System.Windows.Navigation;
Using System.Windows.Shapes;

Namespace Textboxmemoryleak
{
<summary>
The interactive logic of MainWindow.xaml
</summary>
public partial class Mainwindow:window
{
Public MainWindow ()
{
InitializeComponent ();
}

private void btnStart_Click (object sender, RoutedEventArgs e)
{
this.btnStart.IsEnabled = false;
This.tbOutput.Text = "";

for (int i = 0; i < 10000; i++)
{
Using this statement to append a TextBox can cause a memory leak
This.tbOutput.Text + = string. Format ("{0}\n", I);

Use this statement to append a textbox to avoid a memory leak
This.tbOutput.AppendText (String. Format ("{0}\n", i));
}

This.btnStart.IsEnabled = true;
}
}
}

The interface looks like this:

  

Memory leak condition

Initially we used the TextBox text append method as follows

This.tbOutput.Text + = string. Format ("{0}\n", I);

Build, start debugging, we look at task Manager, at this time the memory occupied only 16M,

  

After clicking on the "Start" button, after waiting for output from 0 to 9999, we look at the task manager and find that the memory at this time is 600+m.

  

If you click on the "Start" button at this point, and so on, and so on, found that the occupied memory 900+m to the

  

If you click on the "Start" button, OutOfMemory will occur. When we change the loop from 0 to 19999, the first time you click on the "Start" button, my machine is outofmemory abnormal.

Situations where memory leaks are avoided

Change the textbox's text append mode to the following statement

This.tbOutput.AppendText (String. Format ("{0}\n", i));

Build, start debugging, then click on the "Start" button of the interface, and so on the end of the loop, we look at Task Manager, test demo program only accounted for 29M of memory (this time is from 0 to 19999 of the loop).

  

This article from Lienhua34 Blog, the original address: http://www.cnblogs.com/lienhua34/archive/2013/02/19/2917600.html

WPF textbox creates a memory leak

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.