WPF: Beware of a TextBox that consumes too much memory

Source: Internet
Author: User

The problem stems from this article: the WPF textbox produces a memory leak.

The whole question is this, the article author demonstrates using code like the following to continually assign values to a TextBox control like WPF:

for (int i = 0; i < 10000; i++)

{

//TBX is a textbox variable on the interface

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

}

Then there is a problem with the program consuming too much memory.

It was soon noted in the comments in that article that this had nothing to do with WPF, because the frequent concatenation of strings would result in too many duplicate string objects, which would consume too much memory even if they were not displayed on a TextBox control.

But the original author also said in his reply that he did the relevant tests, but did not appear to occupy a lot of memory.

The final question is nothing.

I did the next Test, one of which is to keep stitching strings and displaying them in a TextBox:

Let's take a little digital 1万来 to do the example.

for (int i = 0; i < 10000; i++)

{

//TBX is a textbox variable on the interface

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

}

The Task Manager displays 55.5 MB of memory after the program runs. (Environment:. NET 4.5/debug Compilation/64-bit)

Then test another case: stitching the string first and then displaying it in the TextBox:

var str = String.Empty;

Let's take a little digital 1万来 to do the example.

for (int i = 0; i < 10000; i++)

{

//TBX is a textbox variable on the interface

str + = String.Format ("{0}\n", I);

}

Tbx. Text = str;

Displays 24.1 MB after running.

If you delete the last sentence, it is not displayed in the textbox at all. After the run is 23.4 MB.

Obviously, the whole problem is really related to WPF, but not because of the string concatenation that the original author believes. The real problem is the Undolimit property of the TextBox (or, more accurately, its parent: textboxbase). This means that the textbox will accumulate too many undo project items because of frequent changes to the values.

This part of the project takes up too much memory space.

The values in. NET 3.5 and 4.0:textboxbase.undolimit default to-1. Represents an infinite number of undo lists if memory is sufficient. (This is a bit scary)

In. NET 4.5: The value of Textboxbase.undolimit is 100 by default.

We can set the Textboxbase.undolimit to 0 (or set the isundoenabled to false), that is, the command textbox does not support the Undo function. Run the code for the first time again:

No revocation

Tbx. Undolimit = 0;

for (int i = 0; i < 10000; i++)

{

//TBX is a textbox variable on the interface

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

}

After running, Task Manager displays 29.4 MB (a few seconds later becomes a MB). Instead of setting undolimit, which is the default value of 100 in. NET 4.5, memory usage can soar to 55.5 MB. It may be larger under non-. NET 3.5 or 4 because there is no limit to the default.

So the solution to the problem is:

Properly set up WPF Textbox.undolimit (in particular, the. NET 3.5/4.0 environment, the default value of 1 is horrible). Of course, this is only applied when the TextBox value is set frequently, and without such a situation, there is no need to worry. Also note that if you want to do frequent string concatenation, use Textboxbase.appendtext or StringBuilder.

This article from Liu Yuan Blog, the original address: http://www.cnblogs.com/mgen/archive/2013/02/24/2924558.html

WPF: Beware of a TextBox that consumes too much memory

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.