TextBox.Text Leaking Memory in WPF Application

前端 未结 2 1384
再見小時候
再見小時候 2020-12-21 09:24

I have an application that does a large amount of number crunching on an array of numbers. I have it set to every 100,000 operations to display the status of those numbers.

相关标签:
2条回答
  • 2020-12-21 09:27

    +1 for Hans.

    Also, you're using StringBuilder. Awesome.

    However, you're concatenating FOUR strings for every item in arraystatus (which could be 100k for all I know) for each time your UI is updated, making StringBuilder pretty much pointless.

    Try

    s.Append(i.ToString());
    s.Append(":\t");
    s.Append(item.ToString());
    s.Append(i % 8 == 0 ? "\n" : "\t"));
    

    or, even better, try converting this to a single call of AppendFormat.

    0 讨论(0)
  • 2020-12-21 09:36

    From the documentation of the 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.

    You found that limit. Set it to a reasonably small value.

    It otherwise doesn't normally make a lot of sense to display logging information in a TextBox. It is really meant to allow the user to enter text. Perhaps TextBlock is a better choice.


    UPDATE: in .NET 4.5, the default of -1 was changed to 100 to avoid this kind of runaway memory usage.

    0 讨论(0)
提交回复
热议问题