The problem arises from this article: Memory leakage caused by WPF textbox.
The problem is as follows: The author of this article demonstrates that the following code is used to assign values to textbox controls like WPF:
For (INT I = 0; I <10000; I ++)
{
// Tbx is the textbox variable on the interface.
Tbx. Text + = string. Format ("{0} \ n", I );
}
Then, the program occupies too much memory.
Someone soon pointed out in the comment in that article that this has nothing to do with WPF, because frequent concatenation of strings produces too many repeated string objects, even if it is not displayed on the Textbox Control, it will also occupy too much memory.
However, in his reply, the author of the original article mentioned that he has performed relevant tests, but it does not occupy a lot of memory.
The final problem is gone.
I did the following test: one is to continuously splice strings and display them in textbox:
// Let's take a small number 10 thousand as an example.
For (INT I = 0; I <10000; I ++)
{
// Tbx is the textbox variable on the interface.
Tbx. Text + = string. Format ("{0} \ n", I );
}
After the program runs, the task manager displays 55.5 MB of memory. (Environment:. Net 4.5/debug compilation/64-bit)
Then test another case: concatenate a string and display it in textbox:
VaR STR = string. empty;
// Let's take a small number 10 thousand as an example.
For (INT I = 0; I <10000; I ++)
{
// Tbx is the textbox variable on the interface.
STR + = string. Format ("{0} \ n", I );
}
Tbx. Text = STR;
24.1 MB is displayed after running.
If you delete the last sentence, it is not displayed in textbox at all. It is 23.4 MB after running.
Obviously, the whole problem is indeed related to WPF, but it is not caused by the concatenation of strings in the original article. The real problem is the undolimit attribute of Textbox (more accurately its parent class: textboxbase. That is to say, textbox will pile up too many undo project items due to frequent changes. Just like the Undo list of Visual Studio:
This part of the project will occupy too much memory space.
In. Net 3.5 and 4.0: textboxbase. undolimit, the default value is-1. This means that if the memory is sufficient, the Undo list will be infinitely large. (This is a bit scary)
In. Net 4.5: the default value of textboxbase. undolimit is 100.
Let's set textboxbase. undolimit to 0 (or set isundoenabled to false), that is, the command textbox does not support the revocation function. Run the first code again:
// Undo prohibition
Tbx. undolimit = 0;
For (INT I = 0; I <10000; I ++)
{
// Tbx is the textbox variable on the interface.
Tbx. Text + = string. Format ("{0} \ n", I );
}
After running, the task manager displays 29.4 MB (24 MB after several seconds ). If undolimit is not set, that is, the default value of. Net 4.5 is 100, the memory usage will soar to 55.5 MB. In. Net 3.5 or 4, it may be larger, because there is no limit by default.
The solution to the problem is:
Set textbox. undolimit for WPF as appropriate (especially in. Net 3.5/4.0 environments, the default value-1 is terrible ). Of course, this only applies when the textbox value is frequently set. If this is not the case, do not worry. In addition, use textboxbase. appendtext or stringbuilder to splice strings frequently.