前段時間參與了一個WPF編寫的項目,在該項目中有這樣一個場景:在程序運行過程當中須要動態地產生大量文本信息,並追加WPF界面上的一個TextBox的Text中進行顯示。編寫完以後,運行該項目的程序,發如今產生大量信息以後,發現系統變慢了,打開任務管理器才發現,該項目的程序佔用了將近1.5G的內存(天啊!!!這不是通常的耗內存啊!!!)。後來經過查資料和探索才發現了WPF的TextBox在追加Text顯示文本時會形成內存泄露。下面經過一個小Demo程序來展現一下這個內存泄露。測試
個人Demo程序很簡單,就是在界面上顯示一個TextBox和一個Button,點擊Button後就從0到9999進行for循環並將這些數字追加的TextBox的Text中進行顯示。代碼以下,this
<Window x:Class="TextBoxMemoryLeak.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="測試TextBox內存泄露" Height="350" Width="525" WindowStartupLocation="CenterScreen"> <Grid Margin="5"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="35" /> </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="開 始" Margin="5,4,5,4" Width="65" 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>
/// 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++)
{
//使用此語句進行Textbox的追加會形成內存泄露
//this.tbOutput.Text += string.Format("{0}\n", i);
//使用此語句進行Textbox的追加可避免內存泄露
this.tbOutput.AppendText(string.Format("{0}\n", i));
}
this.btnStart.IsEnabled = true;
}
}
}spa
界面以下所示:調試
![](http://static.javashuo.com/static/loading.gif)
內存泄露狀況orm
最初咱們採用的是TextBox的Text追加方式以下xml
this.tbOutput.Text += string.Format("{0}\n", i);htm
構建,啓動調試後,咱們查看任務管理器,此時所佔內存只有16M,blog
![](http://static.javashuo.com/static/loading.gif)
點擊【開始】按鈕以後,等到從0輸出到9999以後,咱們再查看任務管理器,發現此時所佔的內存飆到了600+M,內存
![](http://static.javashuo.com/static/loading.gif)
若此時再點擊【開始】按鈕,等循環結束,發現所佔內存飆到了900+M,
![](http://static.javashuo.com/static/loading.gif)
再點擊【開始】按鈕的話,就要發生OutOfMemory異常的。當咱們將循環改成從0到19999時,第一次點擊【開始】按鈕,個人機器就發生OutOfMemory異常了。
避免內存泄露的狀況
將TextBox的Text追加方式改成下面語句
this.tbOutput.AppendText(string.Format("{0}\n", i));
構建,啓動調試,而後點擊界面的【開始】按鈕,等循環結束,咱們查看任務管理器,測試Demo程序只佔了29M內存(此時是從0到19999的循環)。
![](http://static.javashuo.com/static/loading.gif)
本文來自lienhua34博客,原文地址:http://www.cnblogs.com/lienhua34/archive/2013/02/19/2917600.html